pandas_market_calendars package

Submodules

pandas_market_calendars.calendar_utils module

Utilities to use with market_calendars

pandas_market_calendars.calendar_utils.convert_freq(index, frequency)

Converts a DateTimeIndex to a new lower frequency

Parameters:
  • index – DateTimeIndex
  • frequency – frequency string
Returns:

DateTimeIndex

pandas_market_calendars.calendar_utils.date_range(schedule, frequency, closed='right', force_close=True, **kwargs)

Given a schedule will return a DatetimeIndex will all of the valid datetime at the frequency given. The schedule values are assumed to be in UTC.

Parameters:
  • schedule – schedule DataFrame
  • frequency – frequency in standard string
  • closed – same meaning as pandas date_range. ‘right’ will exclude the first value and should be used when the results should only include the close for each bar.
  • force_close – if True then the close of the day will be included even if it does not fall on an even frequency. If False then the market close for the day may not be included in the results
  • kwargs – arguments that will be passed to the pandas date_time
Returns:

DatetimeIndex

pandas_market_calendars.calendar_utils.get_calendar(name, open_time=None, close_time=None)

Retrieves an instance of an MarketCalendar whose name is given.

Parameters:
  • name – The name of the MarketCalendar to be retrieved.
  • open_time – Market open time override as datetime.time object. If None then default is used.
  • close_time – Market close time override as datetime.time object. If None then default is used.
Returns:

MarketCalendar of the desired calendar.

pandas_market_calendars.calendar_utils.merge_schedules(schedules, how='outer')

Given a list of schedules will return a merged schedule. The merge method (how) will either return the superset of any datetime when any schedule is open (outer) or only the datetime where all markets are open (inner)

Parameters:
  • schedules – list of schedules
  • how – outer or inner
Returns:

schedule DataFrame

pandas_market_calendars.exchange_calendar_bmf module

class pandas_market_calendars.exchange_calendar_bmf.BMFExchangeCalendar(open_time=None, close_time=None)

Bases: pandas_market_calendars.market_calendar.MarketCalendar

Exchange calendar for BM&F BOVESPA

Open Time: 10:00 AM, Brazil/Sao Paulo Close Time: 4:00 PM, Brazil/Sao Paulo

Regularly-Observed Holidays: - Universal Confraternization (New year’s day, Jan 1) - Sao Paulo City Anniversary (Jan 25) - Carnaval Monday (48 days before Easter) - Carnaval Tuesday (47 days before Easter) - Passion of the Christ (Good Friday, 2 days before Easter) - Corpus Christi (60 days after Easter) - Tiradentes (April 21) - Labor day (May 1) - Constitutionalist Revolution (July 9 after 1997) - Independence Day (September 7) - Our Lady of Aparecida Feast (October 12) - All Souls’ Day (November 2) - Proclamation of the Republic (November 15) - Day of Black Awareness (November 20 after 2004) - Christmas (December 24 and 25) - Day before New Year’s Eve (December 30 if NYE falls on a Saturday) - New Year’s Eve (December 31)

close_time_default

Default close time for the market

Returns:time
name

Name of the market

Returns:string name
open_time_default

Default open time for the market

Returns:time
regular_holidays
Returns:pd.AbstractHolidayCalendar: a calendar containing the regular holidays for this calendar
special_opens

A list of special open times and corresponding AbstractHolidayCalendar.

Returns:List of (time, AbstractHolidayCalendar) tuples
tz

Time zone for the market.

Returns:timezone

pandas_market_calendars.exchange_calendar_cfe module

class pandas_market_calendars.exchange_calendar_cfe.CFEExchangeCalendar(open_time=None, close_time=None)

Bases: pandas_market_calendars.market_calendar.MarketCalendar

Exchange calendar for the CBOE Futures Exchange (CFE).

http://cfe.cboe.com/aboutcfe/expirationcalendar.aspx

Open Time: 8:30am, America/Chicago Close Time: 3:15pm, America/Chicago

(We are ignoring extended trading hours for now)

close_time_default

Default close time for the market

Returns:time
name

Name of the market

Returns:string name
open_time_default

Default open time for the market

Returns:time
regular_holidays
Returns:pd.AbstractHolidayCalendar: a calendar containing the regular holidays for this calendar
special_closes

A list of special close times and corresponding HolidayCalendars.

Returns:List of (time, AbstractHolidayCalendar) tuples
tz

Time zone for the market.

Returns:timezone

pandas_market_calendars.exchange_calendar_cme module

class pandas_market_calendars.exchange_calendar_cme.CMEExchangeCalendar(open_time=None, close_time=None)

Bases: pandas_market_calendars.market_calendar.MarketCalendar

Exchange calendar for CME

Open Time: 5:00 PM, America/Chicago Close Time: 5:00 PM, America/Chicago

Regularly-Observed Holidays: - New Years Day - Good Friday - Christmas

adhoc_holidays
Returns:list of ad-hoc holidays
close_time_default

Default close time for the market

Returns:time
name

Name of the market

Returns:string name
open_offset
Returns:open offset
open_time_default

Default open time for the market

Returns:time
regular_holidays
Returns:pd.AbstractHolidayCalendar: a calendar containing the regular holidays for this calendar
special_closes

A list of special close times and corresponding HolidayCalendars.

Returns:List of (time, AbstractHolidayCalendar) tuples
tz

Time zone for the market.

Returns:timezone

pandas_market_calendars.exchange_calendar_eurex module

class pandas_market_calendars.exchange_calendar_eurex.EUREXExchangeCalendar(open_time=None, close_time=None)

Bases: pandas_market_calendars.market_calendar.MarketCalendar

Exchange calendar for EUREX

close_time_default

Default close time for the market

Returns:time
name

Name of the market

Returns:string name
open_time_default

Default open time for the market

Returns:time
regular_holidays
Returns:pd.AbstractHolidayCalendar: a calendar containing the regular holidays for this calendar
special_closes

A list of special close times and corresponding HolidayCalendars.

Returns:List of (time, AbstractHolidayCalendar) tuples
tz

Time zone for the market.

Returns:timezone

pandas_market_calendars.exchange_calendar_ice module

class pandas_market_calendars.exchange_calendar_ice.ICEExchangeCalendar(open_time=None, close_time=None)

Bases: pandas_market_calendars.market_calendar.MarketCalendar

Exchange calendar for ICE US.

Open Time: 8pm, US/Eastern Close Time: 6pm, US/Eastern

https://www.theice.com/publicdocs/futures_us/ICE_Futures_US_Regular_Trading_Hours.pdf # noqa

adhoc_holidays
Returns:list of ad-hoc holidays
close_time_default

Default close time for the market

Returns:time
name

Name of the market

Returns:string name
open_offset
Returns:open offset
open_time_default

Default open time for the market

Returns:time
regular_holidays
Returns:pd.AbstractHolidayCalendar: a calendar containing the regular holidays for this calendar
special_closes

A list of special close times and corresponding HolidayCalendars.

Returns:List of (time, AbstractHolidayCalendar) tuples
tz

Time zone for the market.

Returns:timezone

pandas_market_calendars.exchange_calendar_jpx module

class pandas_market_calendars.exchange_calendar_jpx.JPXExchangeCalendar(open_time=None, close_time=None)

Bases: pandas_market_calendars.market_calendar.MarketCalendar

Exchange calendar for JPX

Open Time: 9:31 AM, Asia/Tokyo LUNCH BREAK :facepalm: : 11:30 AM - 12:30 PM Asia/Tokyo Close Time: 4:00 PM, Asia/Tokyo

close_time_default

Default close time for the market

Returns:time
lunch_end = datetime.time(12, 30)
lunch_start = datetime.time(11, 30)
name

Name of the market

Returns:string name
static open_at_time(schedule, timestamp, include_close=False)

To determine if a given timestamp is during an open time for the market.

Parameters:
  • schedule – schedule DataFrame
  • timestamp – the timestamp to check for
  • include_close – if False then the timestamp that equals the closing timestamp will return False and not be considered a valid open date and time. If True then it will be considered valid and return True. Use True if using bars and would like to include the last bar as a valid open date and time.
Returns:

True if the timestamp is a valid open date and time, False if not

open_time_default

Default open time for the market

Returns:time
regular_early_close = datetime.time(13, 0)
regular_holidays
Returns:pd.AbstractHolidayCalendar: a calendar containing the regular holidays for this calendar
tz

Time zone for the market.

Returns:timezone

pandas_market_calendars.exchange_calendar_lse module

class pandas_market_calendars.exchange_calendar_lse.LSEExchangeCalendar(open_time=None, close_time=None)

Bases: pandas_market_calendars.market_calendar.MarketCalendar

Exchange calendar for the London Stock Exchange

Open Time: 8:00 AM, GMT Close Time: 4:30 PM, GMT

Regularly-Observed Holidays: - New Years Day (observed on first business day on/after) - Good Friday - Easter Monday - Early May Bank Holiday (first Monday in May) - Spring Bank Holiday (last Monday in May) - Summer Bank Holiday (last Monday in August) - Christmas Day - Dec. 27th (if Christmas is on a weekend) - Boxing Day - Dec. 28th (if Boxing Day is on a weekend)

close_time_default

Default close time for the market

Returns:time
name

Name of the market

Returns:string name
open_time_default

Default open time for the market

Returns:time
regular_holidays
Returns:pd.AbstractHolidayCalendar: a calendar containing the regular holidays for this calendar
special_closes

A list of special close times and corresponding HolidayCalendars.

Returns:List of (time, AbstractHolidayCalendar) tuples
tz

Time zone for the market.

Returns:timezone

pandas_market_calendars.exchange_calendar_nyse module

class pandas_market_calendars.exchange_calendar_nyse.NYSEExchangeCalendar(open_time=None, close_time=None)

Bases: pandas_market_calendars.market_calendar.MarketCalendar

Exchange calendar for NYSE

Open Time: 9:31 AM, US/Eastern Close Time: 4:00 PM, US/Eastern

Regularly-Observed Holidays: - New Years Day (observed on monday when Jan 1 is a Sunday) - Martin Luther King Jr. Day (3rd Monday in January, only after 1998) - Lincoln’s Birthday (February 12th, only before 1954) - Washington’s Birthday (February 22nd, before 1971 with rule change in

  • Washington’s Birthday (aka President’s Day, 3rd Monday in February, after 1970)
  • Good Friday (two days before Easter Sunday)
  • Memorial Day (May 30th, before 1970, with rule change in 1964)
  • Memorial Day (last Monday in May, after 1970)
  • Independence Day (July 4th Sunday to Monday, before 1954)
  • Independence Day (observed on the nearest weekday to July 4th, after 1953)
  • Election Day (First Tuesday starting on November 2nd, between 1848 and 1967)
  • Election Day (Every four years, first Tuesday starting on November 2nd, between 1968 and 1980)
  • Veterans Day (November 11th, between 1934 and 1953)
  • Columbus Day (October 12th, before 1954)
  • Labor Day (first Monday in September)
  • Thanksgiving (last Thursday in November, before 1939)
  • Thanksgiving (second to last Thursday in November, between 1939 and 1941)
  • Thanksgiving (fourth Thursday in November, after 1941)
  • Christmas (December 25th, Sunday to Monday, before 1954)
  • Christmas (observed on nearest weekday to December 25, after 1953)

NOTE: The NYSE does not observe the following US Federal Holidays: - Columbus Day (after 1953) - Veterans Day (after 1953)

Regularly-Observed Early Closes: - July 3rd (Mondays, Tuesdays, and Thursdays, 1995 onward) - July 5th (Fridays, 1995 onward, except 2013) - Christmas Eve (except on Fridays, when the exchange is closed entirely) - Day After Thanksgiving (aka Black Friday, observed from 1992 onward)

NOTE: Until 1993, the standard early close time for the NYSE was 2:00 PM. From 1993 onward, it has been 1:00 PM.

Additional Irregularities: - Closed on 11/1/1929 and 11/29/1929 for backlog relief. - Closed between 3/6/1933 and 3/14/1933 due to bank holiday. - Closed on 8/15/1945 and 8/16/1945 following victory over Japan. - Closed on Christmas Eve in 1945 and 1946. - Closed on December 26th in 1958. - Closed the day before Memorial Day in 1961. - Closed on 11/25/1963 due to John F. Kennedy’s death. - Closed for Lincoln’s Birthday in 1968. - Closed a number of days between June 12th and December 24th in 1968

due to paperwork crisis.
  • Closed on 4/9/1968 due to Martin Luther King’s death.
  • Closed the day after Independence Day in 1968.
  • Closed on 2/10/1969 due to weather (snow).
  • Closed on 3/31/1969 due to Dwight D. Eisenhower’s death.
  • Closed on 7/21/1969 following the first lunar landing.
  • Closed on 12/28/1972 due to Harry S. Truman’s death.
  • Closed on 1/25/1973 due to Lyndon B. Johnson’s death.
  • Closed on 7/14/1977 due to New York City blackout.
  • Closed on 9/27/1985 due to Hurricane Gloria.
  • Closed on 4/27/1994 due to Richard Nixon’s death.
  • Closed from 9/11/2001 to 9/16/2001 due to terrorist attacks in NYC.
  • Closed on 6/11/2004 due to Ronald Reagan’s death.
  • Closed on 1/2/2007 due to Gerald Ford’s death.
  • Closed on 10/29/2012 and 10/30/2012 due to Hurricane Sandy.
  • Closed at 1:00 PM on Wednesday, July 3rd, 2013
  • Closed at 1:00 PM on Friday, December 31, 1999
  • Closed at 1:00 PM on Friday, December 26, 1997
  • Closed at 1:00 PM on Friday, December 26, 2003

NOTE: The exchange was not closed early on Friday December 26, 2008, nor was it closed on Friday December 26, 2014. The next Thursday Christmas will be in 2025. If someone is still maintaining this code in 2025, then we’ve done alright…and we should check if it’s a half day.

adhoc_holidays
Returns:list of ad-hoc holidays
close_time_default

Default close time for the market

Returns:time
name

Name of the market

Returns:string name
open_time_default

Default open time for the market

Returns:time
regular_early_close = datetime.time(13, 0)
regular_holidays
Returns:pd.AbstractHolidayCalendar: a calendar containing the regular holidays for this calendar
special_closes

A list of special close times and corresponding HolidayCalendars.

Returns:List of (time, AbstractHolidayCalendar) tuples
special_closes_adhoc
Returns:List of (time, DatetimeIndex) tuples that represent special closes that cannot be codified into rules.
tz

Time zone for the market.

Returns:timezone

pandas_market_calendars.exchange_calendar_tsx module

class pandas_market_calendars.exchange_calendar_tsx.TSXExchangeCalendar(open_time=None, close_time=None)

Bases: pandas_market_calendars.market_calendar.MarketCalendar

Exchange calendar for the Toronto Stock Exchange

Open Time: 9:30 AM, EST Close Time: 4:00 PM, EST

Regularly-Observed Holidays: - New Years Day (observed on first business day on/after) - Family Day (Third Monday in February after 2008) - Good Friday - Victoria Day (Monday before May 25th) - Canada Day (July 1st, observed first business day after) - Civic Holiday (First Monday in August) - Labor Day (First Monday in September) - Thanksgiving (Second Monday in October) - Christmas Day - Dec. 27th (if Christmas is on a weekend) - Boxing Day - Dec. 28th (if Boxing Day is on a weekend)

close_time_default

Default close time for the market

Returns:time
name

Name of the market

Returns:string name
open_time_default

Default open time for the market

Returns:time
regular_holidays
Returns:pd.AbstractHolidayCalendar: a calendar containing the regular holidays for this calendar
tz

Time zone for the market.

Returns:timezone

pandas_market_calendars.jpx_autumnal_equinox module

pandas_market_calendars.market_calendar module

class pandas_market_calendars.market_calendar.MarketCalendar(open_time=None, close_time=None)

Bases: object

An MarketCalendar represents the timing information of a single market or exchange. Unless otherwise noted all times are in UTC and use Pandas data structures.

adhoc_holidays
Returns:list of ad-hoc holidays
close_offset
Returns:close offset
close_time
Returns:close time
close_time_default

Default close time for the market

Returns:time
early_closes(schedule)

Get a DataFrame of the dates that are an early close.

Parameters:schedule – schedule DataFrame
Returns:schedule DataFrame with rows that are early closes
holidays()

Returns the complete CustomBusinessDay object of holidays that can be used in any Pandas function that take that input.

Returns:CustomBusinessDay object of holidays
name

Name of the market

Returns:string name
static open_at_time(schedule, timestamp, include_close=False)

To determine if a given timestamp is during an open time for the market.

Parameters:
  • schedule – schedule DataFrame
  • timestamp – the timestamp to check for
  • include_close – if False then the timestamp that equals the closing timestamp will return False and not be considered a valid open date and time. If True then it will be considered valid and return True. Use True if using bars and would like to include the last bar as a valid open date and time.
Returns:

True if the timestamp is a valid open date and time, False if not

open_offset
Returns:open offset
open_time
Returns:open time
open_time_default

Default open time for the market

Returns:time
regular_holidays
Returns:pd.AbstractHolidayCalendar: a calendar containing the regular holidays for this calendar
schedule(start_date, end_date)

Generates the schedule DataFrame. The resulting DataFrame will have all the valid business days as the index and columns for the market opening datetime (market_open) and closing datetime (market_close). All time zones are set to UTC. To convert to the local market time use pandas tz_convert and the self.tz to get the market time zone.

Parameters:
  • start_date – start date
  • end_date – end date
Returns:

schedule DataFrame

special_closes

A list of special close times and corresponding HolidayCalendars.

Returns:List of (time, AbstractHolidayCalendar) tuples
special_closes_adhoc
Returns:List of (time, DatetimeIndex) tuples that represent special closes that cannot be codified into rules.
special_opens

A list of special open times and corresponding AbstractHolidayCalendar.

Returns:List of (time, AbstractHolidayCalendar) tuples
special_opens_adhoc
Returns:List of (time, DatetimeIndex) tuples that represent special opens that cannot be codified into rules.
tz

Time zone for the market.

Returns:timezone
valid_days(start_date, end_date, tz='UTC')

Get a DatetimeIndex of valid open business days.

Parameters:
  • start_date – start date
  • end_date – end date
  • tz – time zone in either string or pytz.timezone
Returns:

DatetimeIndex of valid business days

pandas_market_calendars.market_calendar.clean_dates(start_date, end_date)

Strips the inputs of time and time zone information

Parameters:
  • start_date – start date
  • end_date – end date
Returns:

(start_date, end_date) with just date, no time and no time zone

pandas_market_calendars.market_calendar.days_at_time(days, t, tz, day_offset=0)

Create an index of days at time t, interpreted in timezone tz. The returned index is localized to UTC.

In the example below, the times switch from 13:45 to 12:45 UTC because March 13th is the daylight savings transition for US/Eastern. All the times are still 8:45 when interpreted in US/Eastern.

>>> import pandas as pd; import datetime; import pprint
>>> dts = pd.date_range('2016-03-12', '2016-03-14')
>>> dts_at_845 = days_at_time(dts, datetime.time(8, 45), 'US/Eastern')
>>> pprint.pprint([str(dt) for dt in dts_at_845])
['2016-03-12 13:45:00+00:00',
 '2016-03-13 12:45:00+00:00',
 '2016-03-14 12:45:00+00:00']
Parameters:
  • days – DatetimeIndex An index of dates (represented as midnight).
  • t – datetime.time The time to apply as an offset to each day in days.
  • tz – pytz.timezone The timezone to use to interpret t.
  • day_offset – int The number of days we want to offset @days by
Returns:

DatetimeIndex of date with the time t

pandas_market_calendars.market_calendar.holidays_at_time(calendar, start, end, time, tz)

pandas_market_calendars.us_holidays module

pandas_market_calendars.us_holidays.following_tuesday_every_four_years_observance(dt)
pandas_market_calendars.us_holidays.july_5th_holiday_observance(datetime_index)

Module contents

class pandas_market_calendars.MarketCalendar(open_time=None, close_time=None)

Bases: object

An MarketCalendar represents the timing information of a single market or exchange. Unless otherwise noted all times are in UTC and use Pandas data structures.

adhoc_holidays
Returns:list of ad-hoc holidays
close_offset
Returns:close offset
close_time
Returns:close time
close_time_default

Default close time for the market

Returns:time
early_closes(schedule)

Get a DataFrame of the dates that are an early close.

Parameters:schedule – schedule DataFrame
Returns:schedule DataFrame with rows that are early closes
holidays()

Returns the complete CustomBusinessDay object of holidays that can be used in any Pandas function that take that input.

Returns:CustomBusinessDay object of holidays
name

Name of the market

Returns:string name
static open_at_time(schedule, timestamp, include_close=False)

To determine if a given timestamp is during an open time for the market.

Parameters:
  • schedule – schedule DataFrame
  • timestamp – the timestamp to check for
  • include_close – if False then the timestamp that equals the closing timestamp will return False and not be considered a valid open date and time. If True then it will be considered valid and return True. Use True if using bars and would like to include the last bar as a valid open date and time.
Returns:

True if the timestamp is a valid open date and time, False if not

open_offset
Returns:open offset
open_time
Returns:open time
open_time_default

Default open time for the market

Returns:time
regular_holidays
Returns:pd.AbstractHolidayCalendar: a calendar containing the regular holidays for this calendar
schedule(start_date, end_date)

Generates the schedule DataFrame. The resulting DataFrame will have all the valid business days as the index and columns for the market opening datetime (market_open) and closing datetime (market_close). All time zones are set to UTC. To convert to the local market time use pandas tz_convert and the self.tz to get the market time zone.

Parameters:
  • start_date – start date
  • end_date – end date
Returns:

schedule DataFrame

special_closes

A list of special close times and corresponding HolidayCalendars.

Returns:List of (time, AbstractHolidayCalendar) tuples
special_closes_adhoc
Returns:List of (time, DatetimeIndex) tuples that represent special closes that cannot be codified into rules.
special_opens

A list of special open times and corresponding AbstractHolidayCalendar.

Returns:List of (time, AbstractHolidayCalendar) tuples
special_opens_adhoc
Returns:List of (time, DatetimeIndex) tuples that represent special opens that cannot be codified into rules.
tz

Time zone for the market.

Returns:timezone
valid_days(start_date, end_date, tz='UTC')

Get a DatetimeIndex of valid open business days.

Parameters:
  • start_date – start date
  • end_date – end date
  • tz – time zone in either string or pytz.timezone
Returns:

DatetimeIndex of valid business days

pandas_market_calendars.get_calendar(name, open_time=None, close_time=None)

Retrieves an instance of an MarketCalendar whose name is given.

Parameters:
  • name – The name of the MarketCalendar to be retrieved.
  • open_time – Market open time override as datetime.time object. If None then default is used.
  • close_time – Market close time override as datetime.time object. If None then default is used.
Returns:

MarketCalendar of the desired calendar.

pandas_market_calendars.merge_schedules(schedules, how='outer')

Given a list of schedules will return a merged schedule. The merge method (how) will either return the superset of any datetime when any schedule is open (outer) or only the datetime where all markets are open (inner)

Parameters:
  • schedules – list of schedules
  • how – outer or inner
Returns:

schedule DataFrame

pandas_market_calendars.date_range(schedule, frequency, closed='right', force_close=True, **kwargs)

Given a schedule will return a DatetimeIndex will all of the valid datetime at the frequency given. The schedule values are assumed to be in UTC.

Parameters:
  • schedule – schedule DataFrame
  • frequency – frequency in standard string
  • closed – same meaning as pandas date_range. ‘right’ will exclude the first value and should be used when the results should only include the close for each bar.
  • force_close – if True then the close of the day will be included even if it does not fall on an even frequency. If False then the market close for the day may not be included in the results
  • kwargs – arguments that will be passed to the pandas date_time
Returns:

DatetimeIndex

pandas_market_calendars.convert_freq(index, frequency)

Converts a DateTimeIndex to a new lower frequency

Parameters:
  • index – DateTimeIndex
  • frequency – frequency string
Returns:

DateTimeIndex