Exchange Calendars
exchange-calendars is a Python library designed for defining and querying calendars for various securities exchanges, providing out-of-the-box support for over 50 global exchanges. It is an actively maintained fork of the original `trading_calendars` package. The library is currently at version 4.13.2 and receives frequent updates, primarily to add and correct holiday schedules and trading hours for exchanges worldwide.
Warnings
- breaking Version 4.0 introduced significant breaking changes related to timezone handling and method naming. Schedule times returned by the calendar are now UTC-localized, while sessions are timezone-naive. Numerous methods were renamed for consistency (e.g., `session_label` parameters became `session`). The old method names were deprecated in earlier versions and removed in 4.0.
- gotcha Some calendars have defined historical `start` and `end` bounds, outside of which the accuracy of the calendar's schedule is not guaranteed. Querying dates outside these specified bounds may yield incorrect or undefined trading sessions/holidays.
- breaking The library explicitly requires Python >=3.10 and <4.0. Attempting to install or run `exchange-calendars` on unsupported Python versions (e.g., Python 3.9 or older, or Python 4.x when released) will lead to installation failures or runtime errors.
- gotcha The definition of 'trading minutes' and the behavior of 24-hour calendars (specifically, their open/close times) changed significantly in version 3.4, and the default `side` parameter for calendars was adjusted in v4. Prior to v3.4, 24-hour calendars might have had open/close times one minute later/earlier than the actual market times.
- gotcha The library typically defines an exchange as 'open' only during regular trading periods, excluding pre-trading, post-trading, auction periods, or observed lunch breaks. This definition might differ from other sources or user expectations.
Install
-
pip install exchange-calendars
Imports
- exchange_calendars
import exchange_calendars as xcals
- get_calendar
xcals.get_calendar('XNYS')
Quickstart
import exchange_calendars as xcals
import pandas as pd
# Get a list of available calendar names
print("Available calendars (first 5):", xcals.get_calendar_names(include_aliases=False)[:5])
# Get the New York Stock Exchange (XNYS) calendar
xnys = xcals.get_calendar("XNYS")
# Query the schedule for a specific date range
start_date = pd.Timestamp("2023-12-28", tz='UTC')
end_date = pd.Timestamp("2024-01-03", tz='UTC')
schedule = xnys.schedule.loc[start_date:end_date]
print("\nNYSE Schedule (2023-12-28 to 2024-01-03):")
print(schedule)