TradeBot Check
Trading involves a risk of losing money. Nothing on this site is financial advice and past results do not guarantee future results. Risk disclosure
Guide · 2026-11-25

How to check if the stock market is open today with Python (holidays included)

A script that runs every weekday will eventually hit a market holiday, download empty data or fail. A trading calendar lets your code ask "is there a session today?" before it does anything. This guide uses exchange_calendars, a free open source Python library.

What you need

  • Python installed on your computer.
  • exchange_calendars, free and open source (Apache-2.0). Install it with pip install exchange_calendars.

Step by step

  1. Pick your exchange. Calendars use ISO 10383 market codes, for example XNYS for the New York Stock Exchange.
  2. Load the calendar:
    import exchange_calendars as xcals
    xnys = xcals.get_calendar("XNYS")
  3. Ask if a date is a session: xnys.is_session("2022-01-01") returns False, because it was a holiday.
  4. See opening and closing times. The schedule table has open, break start, break end and close for every session, for example xnys.schedule.loc["2021-12-29":"2022-01-04"].
  5. Find the neighbours. Methods such as previous_session let you jump to the last trading day.
  6. Print a calendar from the command line: ecal XNYS 2020 shows the whole year and ecal XNYS 1 2020 a single month.

Ideas for what to do next

  • Wrap your price download from our yfinance CSV guide in an if is_session(...) check.
  • Run the script on a timer with cron or your task scheduler.
  • Use the schedule to line up backtest data with real trading days.

Good to know

Calendars are maintained through user contributions, so a missing holiday or a wrong time is fixed by contributing an update: double-check important dates with the exchange. Trading minutes exclude pre-trading, post-trading, auction periods and lunch breaks, and some methods were renamed or removed between versions, so check the docs for your version. It is a calendar only, with no prices. Any example you test is illustrative and never promises returns. See also how to backtest a strategy properly.

See more ideas

Browse the use cases or the tool directory. Custom bots are coming soon: tell me when it opens.

Before you start

What does exchange_calendars cost and what licence does it use?

Free and open source (Apache-2.0 per the GitHub repository). Licence: Apache-2.0. Prices change, so check the official website before you decide.

What should I know before using exchange_calendars?

Calendars are maintained through user contributions: missing holidays or wrong times are fixed by contributing updates, so double-check important dates. Trading minutes exclude pre-trading, post-trading, auction periods and lunch breaks. Several methods were renamed, deprecated or removed (for example sessions_opens and sessions_closes in 4.3), so check your version against the docs. It is a calendar only; it has no price data, indicators or backtesting engine.

What does yfinance cost and what licence does it use?

Free (open source). Licence: Apache-2.0. Prices change, so check the official website before you decide.

What should I know before using yfinance?

Not affiliated with or endorsed by Yahoo. Its own documentation says the tool is intended for research and educational purposes and that the Yahoo Finance API is for personal use only: read their terms. Since it is not an official API, it can stop working whenever Yahoo changes something.

Tools in this guide

  • exchange_calendars: Python library with trading calendars for more than 50 exchanges, so your code knows which days are sessions, when markets open, close and take breaks.
  • yfinance: Python library to download prices and history from Yahoo Finance; widely used for research and learning.

Compare your options

Want a bot like this when custom bots open? Get notified · more bot ideas

Coming soon

Coming soon: custom bots

We are preparing a service to code, backtest and automate trading and statistics bots to your own rules. It is not available yet: follow our Telegram channel to know when it opens.

Tools mentioned in this guide

More guides