>>> xhkg.session_minutes("2022-01-03")
DatetimeIndex(['2022-01-03 01:30:00+00:00', '2022-01-03 01:31:00+00:00',
'2022-01-03 01:32:00+00:00', '2022-01-03 01:33:00+00:00',
'2022-01-03 01:34:00+00:00', '2022-01-03 01:35:00+00:00',
'2022-01-03 01:36:00+00:00', '2022-01-03 01:37:00+00:00',
'2022-01-03 01:38:00+00:00', '2022-01-03 01:39:00+00:00',
...
'2022-01-03 07:50:00+00:00', '2022-01-03 07:51:00+00:00',
'2022-01-03 07:52:00+00:00', '2022-01-03 07:53:00+00:00',
'2022-01-03 07:54:00+00:00', '2022-01-03 07:55:00+00:00',
'2022-01-03 07:56:00+00:00', '2022-01-03 07:57:00+00:00',
'2022-01-03 07:58:00+00:00', '2022-01-03 07:59:00+00:00'],
dtype='datetime64[ns, UTC]', length=330, freq=None)
>>> mins = [ "2022-01-03 " + tm for tm in ["01:29", "01:30", "04:20", "07:59", "08:00"] ]
>>> [ xhkg.is_trading_minute(minute) for minute in mins ]
[False, True, False, True, False] # by default minutes are closed on the left side
>>> xhkg.is_break_minute("2022-01-03 04:20")
True
>>> xhkg.previous_close("2022-01-03 08:10")
Timestamp('2022-01-03 08:00:00+0000', tz='UTC')
>>> xhkg.previous_minute("2022-01-03 08:10")
Timestamp('2022-01-03 07:59:00+0000', tz='UTC')
Check out the minutes tutorial for a deeper dive that includes an explanation of the concept of 'minutes' and how the "side" option determines which minutes are treated as trading minutes.
Hopefully you'll find that exchange_calendars has the method you need to get the information you want. If it doesn't, either PR it or raise an issue and let us know!
Command Line Usage
Print a unix-cal like calendar straight from the command line (holidays are indicated by brackets)...
ecal 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
ecal 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
How can I create a new calendar?
First off, make sure the calendar you're after hasn't already been defined; exchange calendars comes with over 50 pre-defined calendars, including major security exchanges.
If you can't find what you're after, a custom calendar can be created as a subclass of ExchangeCalendar. This workflow describes the process to add a new calendar to exchange_calendars. Just follow the relevant parts.
To access the new calendar via get_calendar call either xcals.register_calendar or xcals.register_calendar_type to register, respectively, a specific calendar instance or a calendar factory (i.e. the subclass).
Can I contribute a new calendar to exchange calendars?
<calendar> is missing a holiday, has a wrong time, should have a break etc...
All of the exchange calendars are maintained by user contributions. If a calendar you care about needs revising, please open a PR - that's how this thing works! (Never contributed to a project before and it all seems a bit daunting? Check this out and don't look back!)
You'll find the workflow to modify an existing calendar here.
What times are considered open and closed?
exchange_calendars attempts to be broadly useful by considering an exchange to be open only during periods of regular trading. During any pre-trading, post-trading or auction period the exchange is treated as closed. An exchange is also treated as closed during any observed lunch break.
See the minutes tutorial for a detailed explanation of which minutes an exchange is considered open over. If you previously used trading_calendars, or exchange_calendars prior to release 3.4, then this is the place to look for answers to questions of how the definition of trading minutes has changed over time (and is now stable and flexible!).
Note that exchange calendars are defined by their ISO-10383 market identifier code.
market-prices
Much of the post v3 development of exchange_calendars was driven by the market_prices library. Check it out if you like the idea of using exchange_calendars to create meaningful OHLCV datasets. It works out-the-box with freely available data!
Deprecations and Renaming
Methods renamed in version 4.0.3 and removed in 4.3
Previous name
New name
bound_start
bound_min
bound_end
bound_max
Methods deprecated in 4.0 and removed in 4.3
Deprecated method
Reason
sessions_closes
use .closes[start:end]
sessions_opens
use .opens[start:end]
Methods with a parameter renamed in 4.0
Method
is_session
is_open_on_minute
minutes_in_range
minutes_window
next_close
next_minute
next_open
previous_close
previous_minute
previous_open
session_break_end
session_break_start
session_close
session_open
sessions_in_range
sessions_window
Methods renamed in version 3.4 and removed in 4.0
Previous name
New name
all_minutes
minutes
all_minutes_nanos
minutes_nanos
all_sessions
sessions
break_start_and_end_for_session
session_break_start_end
date_to_session_label
date_to_session
first_trading_minute
first_minute
first_trading_session
first_session
has_breaks
sessions_has_break
last_trading_minute
last_minute
last_trading_session
last_session
next_session_label
next_session
open_and_close_for_session
session_open_close
previous_session_label
previous_session
market_break_ends_nanos
break_ends_nanos
market_break_starts_nanos
break_starts_nanos
market_closes_nanos
closes_nanos
market_opens_nanos
opens_nanos
minute_index_to_session_labels
minutes_to_sessions
minute_to_session_label
minute_to_session
minutes_count_for_sessions_in_range
sessions_minutes_count
minutes_for_session
session_minutes
minutes_for_sessions_in_range
sessions_minutes
session_closes_in_range
sessions_closes
session_distance
sessions_distance
session_opens_in_range
sessions_opens
Other methods deprecated in 3.4 and removed in 4.0
Removed Method
execution_minute_for_session
execution_minute_for_sessions_in_range
execution_time_from_close
execution_time_from_open
Alternatives
Use instead of exchange_calendars: tools that do the same job, closest first.