Guide · running it · time and calendars

Time, holidays and half days: how a trading bot knows the market is open

Most live bugs around time are quiet ones. The bot starts on a day the exchange is shut, or it uses the wrong clock for a rule that depends on the opening minutes. This guide covers the checks we build into trading software so that does not happen.

One time zone rule per project

Pick the rule once and keep it. On our projects that trade non-Indian markets, the code works in UTC. On projects that trade Indian markets, it works in IST. Mixing the two inside one program is where mistakes start.

Let the clock sync handle daylight saving

The US changes its clocks for daylight saving and India does not, so the gap between the two sessions moves during the year. We do not hard-code the offset. The system syncs its time automatically with the broker or the internet, so it moves with the change on its own.

Use the exchange clock, not the PC clock

Some rules depend on the exchange’s time, such as no orders in the first five minutes. Those rules read the broker or platform clock. A PC clock can be wrong or can drift, and then the rule fires at the wrong moment. We also re-sync the clock every evening after the market closes, against the broker or platform APIs and a public time source.

Two layers for holidays

  1. A yearly list. Before the end of December we download the exchange holidays for the next year and set them into each program. The bot then knows the next twelve months.
  2. A daily check. After the US market closes each day, the system looks up the date of the next trading session from a public finance site and marks holidays from that.

On a holiday the bot does not load by itself. If a client starts it anyway, it does not connect to the broker. It shows a plain message with the date: no market today, it is a holiday.

The final check is the live price

Calendars can be wrong or can disagree with each other, and half days are the usual surprise. So the last test is not a calendar. When the bot loads, it connects to the broker API and subscribes to a live benchmark price. If live bid and ask prices are not updating, the bot does nothing and sends no orders. The check runs every 0.2 seconds.

The benchmark depends on the market: the S&P 500 for US projects, the Nifty 50 index for Indian markets, and a live price such as gold or a currency pair like EURUSD for forex and commodity projects.

This one rule covers a half day, an unplanned closure, and the case where two calendar sources disagree. The bot only trades when the market is actually sending prices.

A short checklist

  • One time zone rule for the whole project: UTC, or IST for Indian markets.
  • Automatic clock sync, with no hard-coded daylight saving offsets.
  • Rules that depend on exchange time read the broker or platform clock.
  • A yearly holiday list loaded before the year starts.
  • A daily check for the next trading session.
  • A clear holiday message instead of a failed connection.
  • No orders unless the benchmark live price is updating, checked every 0.2 seconds.
  • A clock re-sync every evening after the close.

More of the checks that keep a live system running are in our guide to the edge cases of live trading systems, and the trading bot checklist covers what to prepare before a build. If you trade through Interactive Brokers, our Interactive Brokers page explains how a system handles the daily restart.

Where we fit

We build these checks into the software we write for clients, to each client’s own specification. This guide is about software engineering only. It does not give investment advice or recommend any trade. If you are planning a build, you can send us your brief.

Common questions

How does a trading bot know the market is closed?
It uses two things. A holiday calendar tells it which days are closed, and a live price check confirms it. On start, the bot subscribes to a live benchmark price. If bid and ask prices are not updating, it places no orders, whatever the calendar says.
What happens to a trading bot on a half day?
A calendar can miss a half day or an early close. A bot that only trades while its live benchmark price is updating handles it without a special rule: when the prices stop, the orders stop.
Should a trading bot use the PC clock or the broker clock?
The broker or platform clock. A rule such as no orders in the first five minutes depends on exchange time, and a PC clock can be wrong or drift. The bot also re-syncs its clock against the broker or platform APIs every evening after the market closes.
start here

Tell us your market, your broker, and what you want automated.

Send us the requirement. We'll come back with the questions that turn it into a real scope, cost and timeline, usually within one working day.

Send us your brief

A few lines is enough to start: your rules in plain words, or a link to your script. We'll ask for the rest.