Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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

import pandas as pd
import pandas_market_calendars as mcal

pd.options.display.max_rows = 500
markets_all = mcal.get_calendar_names()
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",
]
markets_both = list(set(markets_all) & set(markets_lumibot))
set(markets_lumibot) - set(markets_all)
{'MarketCalendar', 'TradingCalendar'}
open_times = [
    mcal.get_calendar(market).open_time.strftime("%H:%M:%S %Z")
    for market in markets_both
]
def get_close_time(market):
    try:
        return mcal.get_calendar(market).close_time.strftime("%H:%M:%S %Z")
    except:
        return None
close_times = [get_close_time(market) for market in markets_both]
df = pd.DataFrame(
    data={
        "markets": markets_both,
        "open_times": open_times,
        "close_times": close_times,
    }
)
df
Loading...
c.market_times
['pre', 'market_open', 'market_close', 'post']
c.open_time
datetime.time(9, 30, tzinfo=<DstTzInfo 'America/New_York' LMT-1 day, 19:04:00 STD>)
c.close_time
datetime.time(16, 0, tzinfo=<DstTzInfo 'America/New_York' LMT-1 day, 19:04:00 STD>)
c.open_close_map
ProtectedDict({'market_open': True, 'market_close': False, 'break_start': False, 'break_end': True, 'pre': True, 'post': False})
c.regular_market_times
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)),)})
from datetime import datetime, timedelta, timezone
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)
trading_hours
Loading...