How to Algo

How to Build a Backtester: Event Loop and Fill Order

The architecture of an event-driven backtester (signal, order, fill, position, P&L) and the same-bar stop-vs-target rule that decides whether it lies.

Part 5 of 20 · How to Algo · Previous: Continuous futures and rolls · Next: Lookahead bias

A backtester has one job: replay history so that your strategy sees exactly what it would have seen live, and nothing more. Every bug that inflates a backtest is a way the engine lets the strategy see a little more: the rest of the bar, the order in which two prices were touched, which of two resting orders worked out.

So the architecture follows from a single constraint. Time only moves forward, decisions use only closed information, and fills happen on events after the decision. Everything else is bookkeeping.

Swimlane diagram across market data, strategy, order manager, fill model and portfolio: decide on the closed bar, fill on the next event, and book the stop when a bar hits both stop and target

The event loop

At its core an event-driven backtester is a loop over events in time order. An event can be a trade, a quote, or a completed bar. For each event:

  1. Update market state. Last price, current bar, indicators that depend only on data up to this event.
  2. Check resting orders. Does this event fill any order that was already working? Stops, targets and limit entries are resolved here, using only this event’s prices.
  3. Update position and P&L. Fills change the position; marks change unrealised P&L.
  4. Let the strategy decide. Only now does the strategy see the new state. It may cancel orders or place new ones.
  5. Queue new orders. They become eligible for fills from the next event onwards, never this one.

Step order is the whole design. If the strategy decides before resting orders are checked, or new orders can fill on the event that created them, the engine is leaking the present into the past.

In code, the skeleton is short:

for ev in events:                       # strictly increasing time
    state.update(ev)
    for o in book.working():            # orders placed before this event
        fill = try_fill(o, ev)          # uses ev's prices only
        if fill: portfolio.apply(fill)
    portfolio.mark(ev)
    for order in strategy.on_event(state, portfolio):
        book.add(order, eligible_from=ev.next)

Real engines add sessions, rolls, costs and logging, but the skeleton should stay recognisable. If you cannot point at the line that guarantees an order cannot fill on the event that created it, the engine has no such guarantee.

Decide on the close, fill on the next event

The most common convention for bar strategies: compute the signal when a bar closes, fill on the next bar’s open (or, with tick data, at the next trade). Three shortcuts break it:

  • Filling at the signal bar’s close. For market orders on a liquid market this is a small optimism. For anything that triggers on a price inside the bar it is lookahead. A breakout filled at its level instead of the next traded price kept $169,125 of overnight gaps nobody could have bought.
  • Filling at an indicator value inside the signal bar. Our ES test of the EMA 9/20 pullback filled at the EMA inside the very bar whose close confirmed the signal. It printed +$2,290,997. The honest version, which waits for the next tradable price, lost $459,730 on the same 12,130 trades.
  • Using the bar’s close to decide which side of the bracket was hit. A bar is not a path.

The same-bar problem and the conservative rule

When one bar’s range contains both your stop and your target, the bar does not say which came first. You have three options: use the tick path, assume the stop, or assume the target. Only the first two are defensible.

Our clearest example is a rejection-block test on NQ five-minute bars: limit entry inside a swept wick, stop beyond the tip, target a multiple of the risk. The first pass booked the target whenever entry and target were inside the same bar. It showed +$85k at a 2R target, profitable in seven of eight years, holdout better than training, and a Sharpe of about 5 at 1R. Forbidding same-bar target fills turned it into −$65k, negative in training and holdout, both sides losing.

The Sharpe of 5 was the tell. No intraday rule on a liquid index future earns that from a candle pattern. When a result looks that clean, look for the fill convention that produced it. Our GEX and volume-profile confluence backtest printed half a million dollars per contract, and the tell there was that long and short both “won”: a fill artefact.

The rule we now use everywhere: a bar that contains both the stop and the target resolves as the stop, and a target cannot fill on the bar that filled the entry. The second half matters as much as the first, because a limit entry and a nearby target inside one bar are the most common same-bar case.

Its mirror image is also a bug. A placebo we built for a candle-level test entered at a bar’s close and then let the same bar’s low stop it out. The low happened before the close, so that stop could never have been hit after the entry. The placebo showed a fake −$1.07 million, which would have made the real rule look brilliant by comparison. A fill at the close can only be affected by later bars.

Two bar panels, same signals scored two ways: EMA 9/20 pullback on ES, 12,130 trades, +2.29M optimistic vs −0.46M conservative; NQ rejection block at 2R, +85k vs −65k

One book for resting orders

Strategies that rest limit orders need an explicit order book in the engine. The tempting shortcut is to handle each signal separately: find the level, scan forward until it fills, record the trade, block new signals until the exit. Part 6 shows how that shortcut chose, with hindsight, which of several resting orders got filled, and turned −$1,779,525 into +$1,385,292 on an ES trend pullback.

The structure that cannot do that is a single book of working orders, resolved chronologically:

  • each new level is added to the book when it is created,
  • levels expire after their time to live,
  • on each bar, every level touched is a candidate; the first in time takes the position,
  • if several are touched in the same bar, fill the one least favourable to you (we use the level nearest the bar’s open, because we cannot see the path inside the bar),
  • once a position is open, the book follows whatever rule the live strategy would follow.

The last line is the important one. Whatever the backtest does with resting orders, a live agent must be able to do exactly the same thing in a single pass through time.

Position accounting

Position bugs inflate results quietly because each trade looks correct on its own.

Our first run of a trading book’s anchored-VWAP rules on NQ daily bars showed $3.7 million. Each anchor opened its own trade, so up to 20 positions were open at the same time. Buy and hold on one contract over the same years made about $356k. The result was leverage, not edge. Forcing one position at a time removed it.

Overlapping positions have a second effect: they inflate the t-statistic, because overlapping trades share the same price moves and are counted as independent. Part 10 quantifies that.

Make position state explicit in the engine, decide whether the strategy may pyramid, and assert on every entry that the position limit holds.

Costs belong inside the engine

Commission and slippage should be applied to every fill by the engine, not subtracted at the end from an average. Two reasons. First, cost per trade differs by fill type: a market entry pays the spread, a resting limit does not but fills less often (see Part 7). Second, a strategy whose trade count changes with a parameter needs costs that change with it, or the parameter search will favour high-frequency variants that only win before costs. Our default for ES is $4.50 commission plus two ticks of slippage per round trip, $29.50 in total; Part 8 explains how to choose yours.

The backtest and the live agent should share code

The strongest test of an engine is to run the live agent through it. We now require that no backtest result ships until a single-pass state machine, the shape of a live agent, can reproduce it. A backtest number that no realisable agent can reach is a bug, not an execution detail.

The practical version: write the strategy as a class that receives events one at a time and emits orders, and use that same class in the backtest and in production. Then replay archived ticks through the production class and compare with the research backtest trade by trade. Part 19 shows what that comparison found in our own live records.

On language, our roadmap covers Python versus a compiled engine; the point here is that speed is a research-quality issue. A sweep that takes four minutes gets its placebo and walk-forward runs; one that takes seven hours does not.

Checklist

  1. Process events strictly in time order.
  2. Check resting orders before the strategy sees the new event.
  3. New orders become eligible from the next event, never the current one.
  4. Decide on closed bars; fill at the next tradable price.
  5. Resolve bars containing stop and target as the stop; no target fill on the entry bar.
  6. A fill at a bar’s close can only be affected by later bars.
  7. Keep all resting orders in one chronological book; fill the least favourable when several are touched.
  8. Enforce position limits explicitly and assert them.
  9. Apply costs per fill inside the engine.
  10. Make the live agent’s code reproduce the backtest before trusting it.

A correct engine removes the structural ways to see the future. It does not stop a strategy from using future information in its own logic. Part 6: Lookahead bias, the backtesting bugs that make results look great.

Frequently asked questions

What is an event-driven backtester?

A backtester that processes market events strictly in time order and lets the strategy react only to what has already happened. Each event updates the market state, resting orders are checked, the strategy may emit orders, and those orders can only be filled by later events.

Event-driven vs vectorized backtesting: which should I build?

Vectorised backtests are faster for simple daily rules that decide and trade at the close. Anything with resting orders, stops and targets inside bars, or position limits needs an event loop, because vectorised code makes it easy to use information from the same bar or later without noticing.

What happens when a bar hits both the stop and the target?

The bar cannot say which came first, so score it as the stop unless you have the tick path, and never let a target fill on the bar that filled the entry. On an NQ rejection-block test, booking same-bar targets produced +$85k; forbidding them turned the same signals into −$65k.

Should a backtest fill at the signal bar's close or the next bar's open?

At the next tradable price: the open of the next bar, or the next trade. Filling at the same close you used to decide is a mild optimism for market orders on liquid markets, and filling at any price inside the signal bar is lookahead. On our ES EMA 9/20 test, filling at the EMA inside the signal bar printed +$2,290,997 where the honest version lost $459,730.

How do I stop a backtest from holding overlapping positions?

Make position state explicit and let the engine refuse new entries while a position is open, unless the strategy is designed to hold several. Our first anchored-VWAP run showed $3.7 million because each anchor opened its own trade; up to 20 positions were open at once, which is leverage, not edge.

Keep reading

How to Algo

Backtest vs Live Trading: Reconciling Every Trade

Why live trading drifts from the backtest: fills, latency, data gaps, rolls and code divergence. How to replay your live engine and reconcile trade by trade.

How to Algo

Tick Data vs Bars: What Resolution Your Algo Needs

Daily bars, minute bars, ticks or order-book depth? Match data resolution to the decision your algo makes, and see what a bar hides about stops and targets.