Home

Awesome

IMPORTANT NOTE

This package is currently unmaintained as the sponsor, quantopian, is going through corporate changes. As such there is a fork of this project that will receive more active maintenance, https://github.com/gerrymanoim/trading_calendars, and the actively developed and maintained alternative implimentation here: https://github.com/rsheftel/pandas_market_calendars . The process to merge these implementations will continue in those two respective repos.

trading_calendars

CI PyPI version Conda version

A Python library of exchange calendars, frequently used with Zipline.

Installation

$ pip install trading-calendars

Quick Start

import trading_calendars as tc
import pandas as pd
import pytz

Get all registered calendars with get_calendar_names:

>>> tc.get_calendar_names()[:5]
['XPHS', 'FWB', 'CFE', 'CMES', 'XSGO']

Get a calendar with get_calendar:

>>> xnys = tc.get_calendar("XNYS")

Working with sessions:

>>> xnys.is_session(pd.Timestamp("2020-01-01"))
False

>>> xnys.next_open(pd.Timestamp("2020-01-01"))
Timestamp('2020-01-02 14:31:00+0000', tz='UTC')

>>> pd.Timestamp("2020-01-01", tz=pytz.UTC)+xnys.day
Timestamp('2020-01-02 00:00:00+0000', tz='UTC')

>>> xnys.previous_close(pd.Timestamp("2020-01-01"))
Timestamp('2019-12-31 21:00:00+0000', tz='UTC')

>>> xnys.sessions_in_range(
>>>     pd.Timestamp("2020-01-01", tz=pytz.UTC),
>>>     pd.Timestamp("2020-01-10", tz=pytz.UTC)
>>> )
DatetimeIndex(['2020-01-02 00:00:00+00:00', '2020-01-03 00:00:00+00:00',
                '2020-01-06 00:00:00+00:00', '2020-01-07 00:00:00+00:00',
                '2020-01-08 00:00:00+00:00', '2020-01-09 00:00:00+00:00',
                '2020-01-10 00:00:00+00:00'],
                dtype='datetime64[ns, UTC]', freq='C')

>>> xnys.sessions_window(
>>>     pd.Timestamp("2020-01-02", tz=pytz.UTC),
>>>     7
>>> )
DatetimeIndex(['2020-01-02 00:00:00+00:00', '2020-01-03 00:00:00+00:00',
                '2020-01-06 00:00:00+00:00', '2020-01-07 00:00:00+00:00',
                '2020-01-08 00:00:00+00:00', '2020-01-09 00:00:00+00:00',
                '2020-01-10 00:00:00+00:00', '2020-01-13 00:00:00+00:00'],
                dtype='datetime64[ns, UTC]', freq='C')

NOTE: see the TradingCalendar class for more advanced usage.

Trading calendars also supports command line usage, printing a unix-cal like calendar indicating which days are trading sessions or holidays.

tcal XNYS 2020
                                        2020
        January                        February                        March
Su  Mo  Tu  We  Th  Fr  Sa     Su  Mo  Tu  We  Th  Fr  Sa     Su  Mo  Tu  We  Th  Fr  Sa
            [ 1]  2   3 [ 4]                           [ 1]
[ 5]  6   7   8   9  10 [11]   [ 2]  3   4   5   6   7 [ 8]   [ 1]  2   3   4   5   6 [ 7]
[12] 13  14  15  16  17 [18]   [ 9] 10  11  12  13  14 [15]   [ 8]  9  10  11  12  13 [14]
[19][20] 21  22  23  24 [25]   [16][17] 18  19  20  21 [22]   [15] 16  17  18  19  20 [21]
[26] 27  28  29  30  31        [23] 24  25  26  27  28 [29]   [22] 23  24  25  26  27 [28]
                                                            [29] 30  31

        April                           May                            June
Su  Mo  Tu  We  Th  Fr  Sa     Su  Mo  Tu  We  Th  Fr  Sa     Su  Mo  Tu  We  Th  Fr  Sa
            1   2   3 [ 4]                         1 [ 2]         1   2   3   4   5 [ 6]
[ 5]  6   7   8   9 [10][11]   [ 3]  4   5   6   7   8 [ 9]   [ 7]  8   9  10  11  12 [13]
[12] 13  14  15  16  17 [18]   [10] 11  12  13  14  15 [16]   [14] 15  16  17  18  19 [20]
[19] 20  21  22  23  24 [25]   [17] 18  19  20  21  22 [23]   [21] 22  23  24  25  26 [27]
[26] 27  28  29  30            [24][25] 26  27  28  29 [30]   [28] 29  30
                               [31]

            July                          August                       September
Su  Mo  Tu  We  Th  Fr  Sa     Su  Mo  Tu  We  Th  Fr  Sa     Su  Mo  Tu  We  Th  Fr  Sa
            1   2 [ 3][ 4]                           [ 1]             1   2   3   4 [ 5]
[ 5]  6   7   8   9  10 [11]   [ 2]  3   4   5   6   7 [ 8]   [ 6][ 7]  8   9  10  11 [12]
[12] 13  14  15  16  17 [18]   [ 9] 10  11  12  13  14 [15]   [13] 14  15  16  17  18 [19]
[19] 20  21  22  23  24 [25]   [16] 17  18  19  20  21 [22]   [20] 21  22  23  24  25 [26]
[26] 27  28  29  30  31        [23] 24  25  26  27  28 [29]   [27] 28  29  30
                               [30] 31

        October                        November                       December
Su  Mo  Tu  We  Th  Fr  Sa     Su  Mo  Tu  We  Th  Fr  Sa     Su  Mo  Tu  We  Th  Fr  Sa
                1   2 [ 3]                                            1   2   3   4 [ 5]
[ 4]  5   6   7   8   9 [10]   [ 1]  2   3   4   5   6 [ 7]   [ 6]  7   8   9  10  11 [12]
[11] 12  13  14  15  16 [17]   [ 8]  9  10  11  12  13 [14]   [13] 14  15  16  17  18 [19]
[18] 19  20  21  22  23 [24]   [15] 16  17  18  19  20 [21]   [20] 21  22  23  24 [25][26]
[25] 26  27  28  29  30 [31]   [22] 23  24  25 [26] 27 [28]   [27] 28  29  30  31
                               [29] 30
tcal XNYS 1 2020
        January 2020
Su  Mo  Tu  We  Th  Fr  Sa
            [ 1]  2   3 [ 4]
[ 5]  6   7   8   9  10 [11]
[12] 13  14  15  16  17 [18]
[19][20] 21  22  23  24 [25]
[26] 27  28  29  30  31

Frequently Asked Questions

Why are open times one minute late?

Due to its historical use in the Zipline backtesting system, trading_calendars will only indicate a market is open upon the completion of the first minute bar in a day. Zipline uses minute bars labeled with the end of the bar, e.g. 9:31AM for 9:30-9:31AM. As an example, on a regular trading day for NYSE:

This may change in the future.

Calendar Support

ExchangeISO CodeCountryVersion AddedExchange Website (English)
New York Stock ExchangeXNYSUSA1.0https://www.nyse.com/index
CBOE FuturesXCBFUSA1.0https://markets.cboe.com/us/futures/overview/
Chicago Mercantile ExchangeCMESUSA1.0https://www.cmegroup.com/
ICE USIEPAUSA1.0https://www.theice.com/index
Toronto Stock ExchangeXTSECanada1.0https://www.tsx.com/
BMF BovespaBVMFBrazil1.0http://www.b3.com.br/en_us/
London Stock ExchangeXLONEngland1.0https://www.londonstockexchange.com/home/homepage.htm
Euronext AmsterdamXAMSNetherlands1.2https://www.euronext.com/en/regulation/amsterdam
Euronext BrusselsXBRUBelgium1.2https://www.euronext.com/en/regulation/brussels
Euronext LisbonXLISPortugal1.2https://www.euronext.com/en/regulation/lisbon
Euronext ParisXPARFrance1.2https://www.euronext.com/en/regulation/paris
Frankfurt Stock ExchangeXFRAGermany1.2http://en.boerse-frankfurt.de/
SIX Swiss ExchangeXSWXSwitzerland1.2https://www.six-group.com/exchanges/index.html
Tokyo Stock ExchangeXTKSJapan1.2https://www.jpx.co.jp/english/
Austrialian Securities ExchangeXASXAustralia1.3https://www.asx.com.au/
Bolsa de MadridXMADSpain1.3http://www.bolsamadrid.es/ing/aspx/Portada/Portada.aspx
Borsa ItalianaXMILItaly1.3https://www.borsaitaliana.it/homepage/homepage.en.htm
New Zealand ExchangeXNZENew Zealand1.3https://www.nzx.com/
Wiener BorseXWBOAustria1.3https://www.wienerborse.at/en/
Hong Kong Stock ExchangeXHKGHong Kong1.3https://www.hkex.com.hk/?sc_lang=en
Copenhagen Stock ExchangeXCSEDenmark1.4http://www.nasdaqomxnordic.com/
Helsinki Stock ExchangeXHELFinland1.4http://www.nasdaqomxnordic.com/
Stockholm Stock ExchangeXSTOSweden1.4http://www.nasdaqomxnordic.com/
Oslo Stock ExchangeXOSLNorway1.4https://www.oslobors.no/ob_eng/
Irish Stock ExchangeXDUBIreland1.4http://www.ise.ie/
Bombay Stock ExchangeXBOMIndia1.5https://www.bseindia.com
Singapore ExchangeXSESSingapore1.5https://www.sgx.com
Shanghai Stock ExchangeXSHGChina1.5http://english.sse.com.cn
Korea ExchangeXKRXSouth Korea1.6http://global.krx.co.kr
Iceland Stock ExchangeXICEIceland1.7http://www.nasdaqomxnordic.com/
Poland Stock ExchangeXWARPoland1.9http://www.gpw.pl
Santiago Stock ExchangeXSGOChile1.9http://inter.bolsadesantiago.com/sitios/en/Paginas/home.aspx
Colombia Securities ExchangeXBOGColombia1.9https://www.bvc.com.co/nueva/index_en.html
Mexican Stock ExchangeXMEXMexico1.9https://www.bmv.com.mx
Lima Stock ExchangeXLIMPeru1.9https://www.bvl.com.pe
Prague Stock ExchangeXPRACzech Republic1.9https://www.pse.cz/en/
Budapest Stock ExchangeXBUDHungary1.10https://bse.hu/
Athens Stock ExchangeASEXGreece1.10http://www.helex.gr/
Istanbul Stock ExchangeXISTTurkey1.10https://www.borsaistanbul.com/en/
Johannesburg Stock ExchangeXJSESouth Africa1.10https://www.jse.co.za/z
Malaysia Stock ExchangeXKLSMalaysia1.11http://www.bursamalaysia.com/market/
Moscow ExchangeXMOSRussia1.11https://www.moex.com/en/
Philippine Stock ExchangeXPHSPhilippines1.11https://www.pse.com.ph/stockMarket/home.html
Stock Exchange of ThailandXBKKThailand1.11https://www.set.or.th/set/mainpage.do?language=en&country=US
Indonesia Stock ExchangeXIDXIndonesia1.11https://www.idx.co.id/
Taiwan Stock Exchange Corp.XTAITaiwan1.11https://www.twse.com.tw/en/
Buenos Aires Stock ExchangeXBUEArgentina1.11https://www.bcba.sba.com.ar/
Pakistan Stock ExchangeXKARPakistan1.11https://www.psx.com.pk/

Note that exchange calendars are defined by their ISO-10383 market identifier code.