Package: marketanalysis
A fast, efficient Python library for generating market holidays based on countries’
security exchanges. It aims to make determining whether a specific date is a market
holiday as fast and flexible as possible.Example Usagefromdatetimeimportdatefrommarketanalysisimportmarketholidaysfrommarketanalysisimportmarkettradingdaysus_marketholidays=marketholidays.UnitedStates()# or:# us_marketholidays = marketholidays.US()# or:# us_marketholidays = marketholidays.CountryHoliday('US')# or, for specific prov / states:# us_marketholidays = marketholidays.CountryHoliday('US', prov=None, state='CA')date(2015,1,1)inus_marketholidays# Truedate(2015,1,2)inus_marketholidays# False# The Holiday class will also recognize strings of any format# and int/float representing a Unix timestamp'2014-01-01'inus_marketholidays# True'1/1/2014'inus_marketholidays# True1388597445inus_marketholidays# Trueus_marketholidays.get('2014-01-01')# "New Year's Day"us_marketholidays['2014-01-01':'2014-01-03']# [date(2014, 1, 1)]us_pr_marketholidays=marketholidays.UnitedStates()# or marketholidays.US(...), or marketholidays.CountryHoliday('US', state='PR')# some marketholidays are only present in parts of a country'2018-01-06'inus_marketholidays# False'2018-01-06'inus_pr_marketholidays# True# Easily create custom Holiday objects with your own dates instead# of using the pre-defined countries/states/provinces availablecustom_marketholidays=marketholidays.MarketHolidayBase()# Append custom holiday dates by passing:# 1) a dict with date/name key/value pairs,custom_marketholidays.append({"2015-01-01":"New Year's Day"})# 2) a list of dates (in any format: date, datetime, string, integer),custom_marketholidays.append(['2015-07-01','07/04/2015'])# 3) a single date itemcustom_marketholidays.append(date(2015,12,25))date(2015,1,1)incustom_marketholidays# Truedate(2015,1,2)incustom_marketholidays# False'12/25/2015'incustom_marketholidays# True# For more complex logic like 4th Monday of January, you can inherit the# HolidayBase class and define your own _populate(year) method. See below# documentation for examples.InstallThe latest stable version can always be installed or updated via pip:$pipinstallmarketholidays==0.1Clone repo with git bash:$gitclonehttps://github.com/OmoMicheal/marketanalysis.gitAvailable CountriesCountryISO codeSecurities/Exchanges AvailableCanadaCA/CANCanadian Securities ExchangeNigeriaNG/NGANigerian Stock Exchange, NSESouthAfricaZA/ZAFJohannesburg Stock ExchangeUnitedStatesUS/USANew York Stock Exchange (NYSE);
National Association of Securities
Dealers Automated Quotations (NASDAQ)APIclass marketholidays.MarketHolidayBase(years=[], expand=True, observed=True, prov=None, state=None)The base class used to create holiday country classes.Parameters:yearsAn iterable list of integers specifying the years that the Holiday object
should pre-generate. This would generally only be used if settingexpandto False. (Default: [])expandA boolean value which specifies whether or not to append marketholidays in new
years to the marketholidays object. (Default: True)observedA boolean value which when set to True will include the observed day of a
holiday that falls on a weekend, when appropriate. (Default: True)provA string specifying a province that has unique statutory marketholidays.
(Default: Canada=’ON’)stateA string specifying a state that has unique statutory marketholidays.
(Default: UnitedStates=None)Methods:get(key, default=None)Returns a string containing the name of the holiday(s) in datekey, which
can be of date, datetime, string, unicode, bytes, integer or float type. If
multiple marketholidays fall on the same date the names will be separated by
commasget(key, default=None)Returns a string containing the name of the holiday(s) in datekey, which
can be of date, datetime, string, unicode, bytes, integer or float type. If
multiple marketholidays fall on the same date the names will be separated by
commasget_list(key)Same asgetexcept returns alistof holiday names instead of a comma
separated stringget_named(name)Returns alistof marketholidays matching (even partially) the provided name
(case insensitive check)pop(key, default=None)Same asgetexcept the key is removed from the holiday objectpop_named(name)Same aspopbut takes the name of the holiday (or part of it) rather than
the dateupdate/appendAccepts dictionary of {date: name} pairs, a list of dates, or even singular
date/string/timestamp objects and adds them to the list of marketholidays# Simplest example possible>>>fromdatetimeimportdate>>>frommarketanalysisimportmarketholidays>>>date(2021,4,2)inmarketholidays.US()True>>date(2021,1,2)inmarketholidays.US()False# However, this is not efficient because it is initializing a new market holiday# object and generating a list of all the holidays in 2021 during each comparison.# It is more efficient to create the object only once>>>us_marketholidays=marketholidays.US()>>>date(2021,4,2)inus_marketholidaysTrue>>date(2021,1,2)inus_marketholidaysFalse# You can pick whichever you prefer of the following two.>>>marketholidays.UnitedStates()==marketholidays.US()True# Let's print out the market holidays in 2021 specific to NYSE and NASDAQ>>>fordate,nameinsorted(marketholidays.US(state='CA',years=2021).items()):>>>print(date,name)2021-01-01NewYear's Day2021-01-18MartinLutherKing,Jr.Day2021-02-15Presidents' Day2021-04-02GoodFriday2021-05-31MemorialDay2021-07-04IndependenceDay2021-07-05IndependenceDay(Observed)2021-09-06LabourDay2021-10-11Thanksgiving2021-12-24ChristmasDay(Observed)2021-12-25ChristmasDay2021-12-31NewYear's Day (Observed)frommarketholidaysimportNG>>>fordate,nameinsorted(NG(years=2021).items()):>>>print(date,name)2021-01-01NewYear's Day2021-04-02GoodFriday2021-04-05EasterMonday2021-05-01Workers’Day2021-05-03Workers’Day(Observed)2021-06-12DemocracyDay2021-06-14DemocracyDay(Observed)2021-10-01IndependenceDay2021-12-25ChristmasDay2021-12-26BoxingDay2021-12-27ChristmasDay(Observed)2021-12-31NewYear's Day (Observed)frommarketholidaysimportCA>>>fordate,nameinsorted(CA(years=2022).items()):>>>print(date,name)2021-12-31NewYear's Day (Observed)2022-01-01NewYear's Day2022-02-21FamilyDay2022-04-15GoodFriday2022-05-23VictoriaDay2022-07-01CanadaDay2022-08-01CivicHoliday2022-09-05LabourDay2022-10-10Thanksgiving2022-12-25ChristmasDay2022-12-26BoxingDay2022-12-27ChristmasDay(Observed)