trading_calendars
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:
- 9:30:00 is treated as closed.
- 9:30:01 is treated as closed.
- 9:31:00 is the first time treated as open.
- 16:00:00 is treated as open
- 16:00:01 is treated as closed
This may change in the future.
Calendar Support
Note that exchange calendars are defined by their ISO-10383 market identifier code.