The capstone. We take the whole course — the linear payoff, cost of carry, margin and MTM, rollover, risk — and assemble it into one small, disciplined, end-to-end trend-following futures algo. Not a money printer; a careful system that does the boring things perfectly.
The architecture
Every systematic futures strategy runs the same loop:
- Fetch — OHLC data and the currently active contract (the one you trade; roll as expiry nears).
- Signal — does your rule fire? (e.g. the 20-period MA crosses above the 50-period MA → go long.)
- Risk check — size the position by risk and stop distance (Lesson 10), confirm margin and a spare buffer. This gate can veto the trade.
- Order — enter, with a stop-loss already attached.
- Manage + roll — trail the stop, watch MTM and margin, and roll a few days before expiry (Lesson 6).
…looping each bar, wrapped in a kill-switch that flattens everything the instant anything looks wrong.
A runnable skeleton
def run_once(broker, cfg):
bars = broker.history(cfg.active_contract, cfg.timeframe)
signal = ma_crossover_signal(bars.close, cfg.fast, cfg.slow) # Lesson 7
# roll first if expiry is near (Lesson 6)
if days_to_expiry(cfg.active_contract) <= cfg.roll_days:
roll_position(broker, cfg) # close near, open next month
cfg.active_contract = next_contract(cfg.active_contract)
if signal == "flat":
return "no signal"
# RISK CHECK (veto gate) — size by risk, not margin
stop = compute_stop(bars, signal, cfg)
lots = position_size(cfg.capital, cfg.risk_pct, bars.close[-1], stop, cfg.point_value)
if lots < 1:
return "skip — risk too large for one lot"
if broker.free_margin() < required_margin(lots, cfg) * cfg.margin_buffer:
return "skip — insufficient spare margin"
# ORDER — enter with stop attached
broker.enter(cfg.active_contract, signal, lots, stop=stop)
return f"entered {signal} {lots} lot(s), stop {stop}"
# main loop
while market_open():
try:
log(run_once(broker, cfg))
manage_positions(broker, cfg) # trail stops, check MTM & margin
except Exception as e:
kill_switch(broker) # flatten everything, stop
alert(f"KILLED: {e}")
break
sleep(cfg.bar_seconds)Notice, as in the other courses, how little of this is “clever”. The signal is one line (the strategy articles have the real thing). The vast majority is rolling, risk checks, stops, margin monitoring and the kill-switch — and that is exactly right. In a live futures algo, the discipline is the strategy.
The realities that make or break it
- Size by RISK, not margin. The ~8× the exchange allows is rope to hang yourself. Size by your rupee risk if the stop hits (Lesson 10).
- Assume a gap can slip the stop. Stops don’t fill through a gap. Size so a worse-than-stop fill is survivable, especially in energy and over weekends.
- Keep spare margin. Daily MTM debits and volatility-driven margin hikes will come. A buffer prevents a forced liquidation at the low.
- Roll early and deliberately. A few days before expiry, not on the day — and account for the roll yield (contango bleed).
- Manage overnight risk. MCX commodities move on global sessions and news while you sleep; decide whether to hold, hedge or square off.
- Count every cost. Fees, STT/CTT, slippage and roll yield — model them, because leverage magnifies their drag.
- Backtest honestly, then paper-trade. On a back-adjusted continuous series (Lesson 11), with costs. Then forward paper-trade for weeks, then one lot.
The course in one line
A future is a simple, linear, leveraged bet with no premium — so the payoff is easy to model, and the real skill is the mechanics: cost of carry, margin and daily mark-to-market, and rollover’s quiet cost. Automate the boring parts like the programmer you are, size by risk (not by the ~8× margin allows), respect the gap and the margin call, roll early, and make survival the strategy.
You now have the foundations — futures mechanics, pricing, the commodity universe, margin, rollover, strategies, spreads, hedging, risk, and the tooling to build. Go and backtest honestly and paper-trade, patiently.
For more, see the companion Options Trading and Crypto Options & Algo Trading courses, the Low-Latency Trading Systems course, and the strategy articles on trend following, mean reversion and position sizing with Kelly.