US Stock Market Calendars (NYSE, CME, CBOE, NASDAQ...)
When pulling Market Data it is often helpful to be able to algorithmically know which dates are trading dates.
There is an independent library that makes this possible called Market Calendar or mcal.
# Get NYSE Schedule for date range
# Documentation : https://pandas-market-calendars.readthedocs.io/en/latest/usage.html
start_date='2024-01-01'
end_date='2024-06-01'
import pandas as pd
import pandas_market_calendars as mcal
nyse = mcal.get_calendar('NYSE')
schedule = nyse.schedule(start_date=start_date, end_date=end_date)
schedule
schedule.market_open.iloc[0] # open time first date
schedule.market_close.iloc[0] # close time first date
schedule.market_open.iloc[-1] # open time last date
schedule.market_close.iloc[-1] # close time last date
schedlist = list(schedule.index)
schedlist
NOTE : Although mcal is generally extremely reliable, it is an external library and has on occasions has missed a market holiday. So be sure to check, double check and always write defensive code. Also note, whilst it is reliable for US Market holidays, it has others markets listed, the reliability of which we cannot attest to.
https://pandas-market-calendars.readthedocs.io/en/latest/usage.html