Skip to content
Course contents
From backtest to live strategy

Running on time, every time

A live strategy must start, run, and stop on the market's schedule, not yours, and survive your laptop closing. This chapter covers the trading calendar and session times, scheduling a program to run automatically, why people move a live system to an always-on machine, and how to restart safely so a crash does not leave a position unmanaged.

9 min readChapter 18 of 28
What you will learn
  • Run a strategy on the NSE session schedule with a scheduler
  • Explain why a live system runs on an always-on machine rather than a personal laptop
  • Make startup safe by reconciling state before trading

A strategy that runs itself still needs someone, or something, to start it at the right time and stop it at the right time, every trading day, whether or not you remember. Markets keep strict hours and closed days, and a program that trades must keep to them exactly. This chapter is about running reliably: on the market's schedule, on a machine that does not sleep, and in a way that survives a crash without leaving a position adrift.

Trade on the market's clock

Trade on the market's clock: a scheduler sets up before the open, runs through the session, reports after the close, and restarts itself on a crash.
Trade on the market's clock: a scheduler sets up before the open, runs through the session, reports after the close, and restarts itself on a crash.

A live strategy must know the market's schedule and obey it. On the NSE the equity session runs through the middle of the day, on weekdays, minus a list of holidays, and outside those hours nothing should trade. Your program needs to run the strategy during the session and stand down otherwise. Here is that logic in its simplest form, checking the session times and only acting while the market is open.

ExampleRun the strategy only during market hours, and reconcile at startupch18/scheduler.py
# A live strategy must run on the market's schedule, not yours, and it must
# reconcile before it trades. This skeleton checks the NSE session times and only
# runs the strategy while the market is open. In a real deployment the times come
# from a scheduler on an always-on machine, not this simulated list of moments.
from datetime import time

MARKET_OPEN = time(9, 15)          # NSE session, illustrative
MARKET_CLOSE = time(15, 30)


def market_is_open(now):
    return MARKET_OPEN <= now <= MARKET_CLOSE


def on_startup():
    print("startup: reconcile against the broker before trading anything")


def run_one_bar(now):
    print(f"{now}  market open  -> run the strategy on this bar")


# A simulated sequence of moments through a day. Normally a scheduler wakes your
# program at each of these; here we just walk the list.
day = [time(9, 0), time(9, 15), time(11, 30), time(15, 30), time(15, 45)]

on_startup()
for now in day:
    if market_is_open(now):
        run_one_bar(now)
    else:
        print(f"{now}  market closed -> do nothing")
Output
startup: reconcile against the broker before trading anything
09:00:00  market closed -> do nothing
09:15:00  market open  -> run the strategy on this bar
11:30:00  market open  -> run the strategy on this bar
15:30:00  market open  -> run the strategy on this bar
15:45:00  market closed -> do nothing

The skeleton does the essential thing: at each moment it is woken, it checks whether the market is open and either runs a bar or does nothing. Before any of that, on startup, it reconciles against the broker, the habit from the reconciliation chapter, so it never begins a day trading on a stale picture. Note the illustrative session times, and that the real trading calendar, with its holidays and the exact open and close, must come from the exchange rather than be hardcoded and forgotten.

A machine that does not sleep

Your laptop is the wrong home for a live strategy. It sleeps, it reboots for updates, it loses wifi when you leave the cafe, and any of those at the wrong moment leaves a position unmanaged. A live system runs on a machine that stays on: a small always-on server, often a rented virtual machine in the cloud, whose only job is to run your program on schedule. This is not about speed, which the reality chapters will address; it is about reliability, the plain requirement that the program be running and connected whenever the market is. A scheduler on that machine wakes the program for the session and keeps it running.

Survive a restart

Things will interrupt a live program: a crash, a deploy, a reboot. The danger is not the interruption itself but what the program does when it comes back. A program that restarts and blindly resumes, with no memory of the position it already holds, can double a trade or trade against itself. The safe pattern is the one the scheduler shows: on every startup, before trading anything, reconcile against the broker to learn the true state, then resume from there. A live system is not one that never falls over. It is one that gets up correctly.

What to carry forward

Running reliably means three things. Trade on the market's clock, only during the session and never on a closed day, taking the real calendar from the exchange. Run on an always-on machine rather than a laptop that sleeps, so the program is present whenever the market is open. And make every startup safe by reconciling against the broker before trading, so an interruption never leaves a position unmanaged. With the strategy now running itself, on schedule, on top of a dependable execution layer, one thing still stands between you and disaster: the live risk controls that can say no and pull the plug. That is the final part.