Ben Chuanlong Du's Blog

It is never too late to learn.

Hands on pandas_market_calendars

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

In [3]:
import pandas as pd
import pandas_market_calendars as mcal

pd.options.display.max_rows = 500
In [11]:
markets_all = mcal.get_calendar_names()
In [12]:
markets_lumibot = [
    'MarketCalendar',
    'ASX',
    'BMF',
    'CFE',
    'NYSE',
    'stock',
    'NASDAQ',
    'BATS',
    'CME_Equity',
    'CBOT_Equity',
    'CME_Agriculture',
    'CBOT_Agriculture',
    'COMEX_Agriculture',
    'NYMEX_Agriculture',
    'CME_Rate',
    'CBOT_Rate',
    'CME_InterestRate',
    'CBOT_InterestRate',
    'CME_Bond',
    'CBOT_Bond',
    'EUREX',
    'HKEX',
    'ICE',
    'ICEUS',
    'NYFE',
    'JPX',
    'LSE',
    'OSE',
    'SIX',
    'SSE',
    'TSX',
    'TSXV',
    'BSE',
    'TASE',
    'TradingCalendar',
    'ASEX',
    'BVMF',
    'CMES',
    'IEPA',
    'XAMS',
    'XASX',
    'XBKK',
    'XBOG',
    'XBOM',
    'XBRU',
    'XBUD',
    'XBUE',
    'XCBF',
    'XCSE',
    'XDUB',
    'XFRA',
    'XETR',
    'XHEL',
    'XHKG',
    'XICE',
    'XIDX',
    'XIST',
    'XJSE',
    'XKAR', 'XKLS',
    'XKRX',
    'XLIM',
    'XLIS',
    'XLON',
    'XMAD',
    'XMEX',
    'XMIL',
    'XMOS',
    'XNYS',
    'XNZE',
    'XOSL',
    'XPAR',
    'XPHS',
    'XPRA',
    'XSES',
    'XSGO',
    'XSHG',
    'XSTO', 
    'XSWX',
    'XTAE',
    'XTAI',
    'XTKS',
    'XTSE',
    'XWAR',
    'XWBO',
    'us_futures',
    '24/7',
    '24/5',
]
In [20]:
markets_both = list(set(markets_all) & set(markets_lumibot))
In [21]:
set(markets_lumibot) - set(markets_all) 
Out[21]:
{'MarketCalendar', 'TradingCalendar'}
In [22]:
open_times = [
    mcal.get_calendar(market).open_time.strftime("%H:%M:%S %Z")
    for market in markets_both
]
In [23]:
def get_close_time(market):
    try:
        return mcal.get_calendar(market).close_time.strftime("%H:%M:%S %Z")
    except:
        return None
In [24]:
close_times = [
    get_close_time(market) for market in markets_both
]
In [25]:
df = pd.DataFrame(data={
    "markets": markets_both,
    "open_times": open_times,
    "close_times": close_times,
})
df 
Out[25]:
markets open_times close_times
0 CBOT_Agriculture 17:01:00 America/Chicago 17:00:00 America/Chicago
1 CBOT_Bond 17:00:00 America/Chicago 16:00:00 America/Chicago
2 BATS 09:30:00 America/New_York 16:00:00 America/New_York
3 XSGO 09:30:00 None
4 XWBO 09:00:00 17:30:00
5 XBKK 10:00:00 16:30:00
6 stock 09:30:00 America/New_York 16:00:00 America/New_York
7 CBOT_Equity 17:00:00 America/Chicago 16:00:00 America/Chicago
8 CBOT_InterestRate 17:00:00 America/Chicago 16:00:00 America/Chicago
9 XAMS 09:00:00 17:30:00
10 XBUE 11:00:00 17:00:00
11 XETR 09:00:00 17:30:00
12 XSHG 09:30:00 15:00:00
13 XBOG 09:30:00 16:00:00
14 XKLS 09:00:00 17:00:00
15 CMES 17:00:00 17:00:00
16 CFE 08:30:00 America/Chicago 15:15:00 America/Chicago
17 24/5 00:00:00 UTC 00:00:00 UTC
18 LSE 08:00:00 Europe/London 16:30:00 Europe/London
19 XWAR 09:00:00 17:00:00
20 XBOM 09:15:00 15:30:00
21 XTAI 09:00:00 13:30:00
22 TASE 10:00:00 Asia/Jerusalem 15:59:00 Asia/Jerusalem
23 XHKG 09:30:00 16:00:00
24 BMF 10:01:00 America/Sao_Paulo 16:00:00 America/Sao_Paulo
25 CME_Agriculture 17:01:00 America/Chicago 17:00:00 America/Chicago
26 XTSE 09:30:00 16:00:00
27 XLIS 08:00:00 16:30:00
28 XBUD 09:00:00 17:00:00
29 XLIM 09:00:00 16:00:00
30 NASDAQ 09:30:00 America/New_York 16:00:00 America/New_York
31 XFRA 09:00:00 17:30:00
32 OSE 09:00:00 Europe/Oslo 16:20:00 Europe/Oslo
33 XSTO 09:00:00 17:30:00
34 CBOT_Rate 17:00:00 America/Chicago 16:00:00 America/Chicago
35 ICE 20:01:00 US/Eastern 18:00:00 US/Eastern
36 XPRA 09:00:00 16:20:00
37 XTKS 09:00:00 15:00:00
38 XPHS 09:30:00 15:30:00
39 XKRX 09:00:00 15:30:00
40 NYSE 09:30:00 America/New_York 16:00:00 America/New_York
41 XMOS 10:00:00 18:45:00
42 XIDX 09:00:00 15:50:00
43 XNYS 09:30:00 16:00:00
44 XKAR 09:32:00 15:30:00
45 us_futures 18:00:00 18:00:00
46 ICEUS 20:01:00 US/Eastern 18:00:00 US/Eastern
47 BVMF 10:00:00 18:00:00
48 XLON 08:00:00 16:30:00
49 XOSL 09:00:00 16:20:00
50 XHEL 10:00:00 18:30:00
51 XASX 10:00:00 16:00:00
52 XMEX 08:30:00 15:00:00
53 NYMEX_Agriculture 17:01:00 America/Chicago 17:00:00 America/Chicago
54 XMAD 09:00:00 17:30:00
55 TSX 09:30:00 Canada/Eastern 16:00:00 Canada/Eastern
56 XJSE 09:00:00 17:00:00
57 XNZE 10:00:00 16:45:00
58 ASX 10:00:00 Australia/Sydney 16:10:00 Australia/Sydney
59 XDUB 08:00:00 16:28:00
60 IEPA 20:00:00 18:00:00
61 COMEX_Agriculture 17:01:00 America/Chicago 17:00:00 America/Chicago
62 BSE 09:15:00 Asia/Calcutta 15:30:00 Asia/Calcutta
63 NYFE 20:01:00 US/Eastern 18:00:00 US/Eastern
64 CME_Equity 17:00:00 America/Chicago 16:00:00 America/Chicago
65 XSES 09:00:00 17:00:00
66 TSXV 09:30:00 Canada/Eastern 16:00:00 Canada/Eastern
67 XCBF 08:30:00 15:15:00
68 XTAE 09:59:00 17:15:00
69 SSE 09:30:00 Asia/Shanghai 15:00:00 Asia/Shanghai
70 XICE 09:30:00 15:30:00
71 XMIL 09:00:00 17:30:00
72 JPX 09:00:00 Asia/Tokyo 15:00:00 Asia/Tokyo
73 XIST 10:00:00 18:00:00
74 EUREX 09:00:00 Europe/Berlin 17:30:00 Europe/Berlin
75 XPAR 09:00:00 17:30:00
76 24/7 00:00:00 UTC 00:00:00 UTC
77 CME_InterestRate 17:00:00 America/Chicago 16:00:00 America/Chicago
78 XCSE 09:00:00 17:00:00
79 CME_Rate 17:00:00 America/Chicago 16:00:00 America/Chicago
80 XSWX 09:00:00 17:30:00
81 CME_Bond 17:00:00 America/Chicago 16:00:00 America/Chicago
82 XBRU 09:00:00 17:30:00
83 HKEX 09:30:00 Asia/Shanghai 16:00:00 Asia/Shanghai
84 ASEX 10:00:00 17:20:00
85 SIX 09:00:00 Europe/Zurich 17:30:00 Europe/Zurich
In [6]:
c.market_times
Out[6]:
['pre', 'market_open', 'market_close', 'post']
In [8]:
c.open_time
Out[8]:
datetime.time(9, 30, tzinfo=<DstTzInfo 'America/New_York' LMT-1 day, 19:04:00 STD>)
In [23]:
c.close_time
Out[23]:
datetime.time(16, 0, tzinfo=<DstTzInfo 'America/New_York' LMT-1 day, 19:04:00 STD>)
In [9]:
c.open_close_map
Out[9]:
ProtectedDict({'market_open': True, 'market_close': False, 'break_start': False, 'break_end': True, 'pre': True, 'post': False})
In [10]:
c.regular_market_times
Out[10]:
ProtectedDict({'pre': ((None, datetime.time(4, 0)),), 'market_open': ((None, datetime.time(10, 0)), ('1985-01-01', datetime.time(9, 30))), 'market_close': ((None, datetime.time(15, 0)), ('1952-09-29', datetime.time(15, 30)), ('1974-01-01', datetime.time(16, 0))), 'post': ((None, datetime.time(20, 0)),)})
In [14]:
from datetime import datetime, timedelta, timezone
In [17]:
dt_now_utc = datetime.now(timezone.utc)

date = dt_now_utc
trading_hours = c.schedule(start_date=date, end_date=date + timedelta(weeks=1)).head(2)
In [18]:
trading_hours
Out[18]:
market_open market_close
2024-03-14 2024-03-14 13:30:00+00:00 2024-03-14 20:00:00+00:00
2024-03-15 2024-03-15 13:30:00+00:00 2024-03-15 20:00:00+00:00

Comments