MyAlgoGuru Help
Education Glossary Open the app
Chapter 3 of 5

Three clocks, and why they must stay separate

A trading window, a schedule and a market session are all "a time of day". They answer three different questions, and merging any two of them is a bug waiting for the right date.

Automated trading involves three separate questions about time, and because all three are answered by something that looks like a clock, they get conflated constantly.

May this strategy act at this time? — the trading window. Your rule: do not enter before 09:30, stop entering after 14:45, square off at 15:10.

Is it time to do something now? — the schedule. A scan that should run every five minutes; a check due at 15:10.

Is the market open for this instrument? — the session. An exchange-and-segment fact that you do not control.

session the exchange decides window you decide due the scheduler decides acts all three must agree 09:15 09:30 12:00 14:45 15:30 Three independent facts. An action needs all three to be true, and each can be wrong on its own.
Three clocks, one action. The session is imposed by the exchange and differs by segment. The window is your own policy and differs by strategy. Due times come from the scheduler and are neither. An action requires all three to agree, which is why collapsing any two of them into a single setting removes a check that was doing real work — and why the removal is invisible until the day the two would have disagreed.

What goes wrong when they are merged

The window stands in for the session. A strategy configured to stop at 15:10 appears to handle the close, so the session check looks redundant and gets dropped. It then works perfectly until a day the market closes early, or the strategy is pointed at a segment with different hours, and it sends orders into a market that is not accepting them. The window was never a statement about whether the exchange was open; it only resembled one.

The schedule stands in for the window. The scan runs every five minutes between 09:30 and 14:45, so the schedule enforces the window implicitly. Then someone changes the schedule — adds a square-off check at 15:10, or a pre-open scan at 09:05 — and the strategy acts outside its window, because the window was never checked, only imitated by the timetable. A trading window must be evaluated at the moment of acting, independently of why the code is running.

One session for every segment. Different exchanges and different segments keep different hours — commodity segments in particular run far later into the evening than equity. A single hardcoded close time is correct for one segment and wrong for the others, and the error appears as orders refused in the evening with no obvious cause. The session has to be looked up per exchange and per segment, every time.

Due-ness confused with lateness. "Is this due?" and "has this already happened today?" are different questions. A scheduler that only asks whether the current time is past the due time will fire a 15:10 square-off check again at 15:15, and again at 15:20. Due-ness needs to record that it ran, not merely compare clocks.

Special days are a table, not a rule

Here is the one that catches even careful implementations.

Exchange calendars contain exceptions. Holidays, obviously. But also special sessions — a trading day on a date that is normally closed, or a session with unusual hours, scheduled for a specific reason on a specific date.

The tempting implementation is a weekday rule: trading days are Monday to Friday, except a list of holidays. That handles ordinary weeks and quietly fails on a session held on a Saturday, or on a day whose hours differ from the norm.

The only correct implementation is a looked-up table of dates, maintained from the exchange's own calendar, that answers "is this date a trading day for this segment, and what are its hours?" A weekday rule with a holiday exception list is a table with one kind of exception modelled and the others not, and the others arrive without warning — in the middle of a session, on a day when your automation is the only thing that did not know.

If you are configuring a system rather than building one, the question to ask is simply whether the calendar is a date table or a weekday rule. The answer tells you whether you will be surprised.

Timezone discipline

One short rule that eliminates an entire category of fault: store and compare instants, display local time.

Every timestamp in your system should be an unambiguous instant. Every comparison — is this due, is this within the window, is the session open — should happen on those instants. Only at the point of showing a time to a human should it be rendered in the local timezone.

The failures from getting this wrong are among the hardest to diagnose, because they are almost always correct. A rule comparing a local-time string against an instant works perfectly for months, until a process runs with a different locale, or a server's timezone is changed, or a timestamp arrives from a source that formats things differently. Then it is wrong by hours, intermittently, in a way that no test exercised.

Related, and worth knowing if you write tests: anything that depends on the current time needs the clock pinned in its tests. A test that passes on a weekday and fails on a Saturday is not a flaky test — it is a correct test of code that has a time dependency nobody made explicit. Pin the clock, and the test tells you something every day of the week.

What to take from this chapter

  • Three clocks: may I act (window), is it time (schedule), is the market open (session). An action needs all three, and each can be wrong alone.
  • A window is not a session. It resembles one until the market closes early or the segment changes.
  • A schedule is not a window. Evaluate the window at the moment of acting, not by relying on the timetable.
  • Sessions differ by exchange and by segment. Look them up per segment, never hardcode one close time.
  • "Is it due?" must record that it ran. Comparing the clock alone fires the same task repeatedly.
  • Special sessions and unusual hours make the calendar a looked-up table of dates. A weekday rule plus a holiday list will fail without warning.
  • Store and compare instants; display local time. And pin the clock in any test of time-dependent code.