Lookahead Bias in Backtests: How to Find the Leak
Lookahead bias lets a backtest see the future: hindsight fills, overwritten levels, deciding and filling on one close. Real cases and three tests to catch it.
Part 6 of 20 · How to Algo · Previous: Building a backtester · Next: Limit order fills
The best backtests we have ever produced were bugs. Not most of them: the very best. That is not bad luck. A backtest that leaks a little future information is optimised, by every subsequent choice you make, toward the variants that leak the most. The more you tune, the more of your result is the leak.
Lookahead bias (also written look-ahead bias) is any use, in a backtest, of information that did not exist yet at the moment of the decision or the fill. This part is a catalogue of the lookahead we found in our own work, what each one faked, and the checks that catch them. None of these bugs looked like price[t+1]. They looked like reasonable engineering.

Simulated: a ±1-tick random walk with no edge, 30,000 bars, a buy limit at the signal bar’s low, 6-tick stop and target, no costs. The only difference between the two curves is the fill engine described in kind 2 below.
Kind 1: future data inside the signal
This is the textbook case, and it still gets through in subtler forms.
Smoothed model states. A hidden Markov model that labels bull and bear regimes is usually fitted on the full sample and plotted with smoothed state probabilities. Smoothing uses the whole series, so the model “detects” the 2022 bear market partly because it has seen how 2022 ends. On NQ the smoothed version printed a Sharpe of 1.84. Walk-forward, with filtered probabilities that use only data up to each day, it fell to 0.59, below buy and hold. The HMM test shows both curves.
A snapshot taken after the session it gates. The first version of our gamma filter gated each ES trade on an option-chain snapshot from the end of the same session. We had included a fade-the-open rule in the test precisely because it loses money, at −$86.37 a trade. Under the leaky gate it became a large winner. Reading the previous session’s chain put it back at a loss. A filter that rescues a rule you know is broken is a leak.
Direction from the fill bar. Our first Optimal Trade Entry test on ES took each trade’s direction from the close of the bar it filled inside. That more than doubled the zone’s average. Read from the previous bar, the famous zone was the second worst of five retracement bands.
Anchors that are only visible later. A swing low is only a swing low once price has moved away from it, often days later. A rule that anchors at swing points must make each anchor usable only after the bars that confirm it, or it draws levels in hindsight.
Features from the same second. In a tick-level machine-learning dataset, an order-book feature stamped in second s used for a decision in second s partly describes the trade it is supposed to predict. Join features from second s − 1.
The label in the features. In one ML run we selected feature columns by a name pattern. On one dataset the label column had a different name and slipped through. The model reported t = +31.4, with a mean of 47 ticks and a dispersion of 65 ticks, against an expected dispersion of about 106 for that barrier. A forecast whose outcomes vary less than the barrier it predicts is impossible without the answer. We now name label columns explicitly and assert their absence from the feature list.
Kind 2: hindsight in choosing fills
This is the most expensive bug we have found, and it contains no future price at all.
Every test we wrote for setups that rest a limit order looked like this: for each signal, scan forward until its level fills, record the trade, and block new signals until the exit (busy = exit_index). Suppose level A is marked at bar 10 and fills at bar 40, and level B is marked at bar 12 and would have filled at bar 15. The loop takes A first because it was created first, sets busy to A’s exit, and skips B. Live, B fills twenty-five bars before A is touched. The backtest kept the later fill and deleted the earlier one. Choosing between resting orders that way requires knowing which one worked out.
The bias runs one way. A level that takes long to fill is one price travelled far to reach, so the scan systematically kept deep entries and dropped shallow ones that get stopped. On ES fair value gaps, 33.9% of kept trades were entered after a sibling level had already been touched.
| Published | Chronologically corrected | |
|---|---|---|
| ES trend pullback | +$23.02/trade, t = 19.57, +$1,385,292 | −$23.88/trade, t = −23.99, −$1,779,525 |
| ES fair value gap | +$34.07/trade, t = 8.06 | −$19.51/trade, t = −5.41 |
The corrected pullback took 74,513 trades instead of 60,189. The bug had not added trades; it had hidden 14,324 losers. The full post-mortem is public.
No control caught it. Both setups had random-entry placebos, distance-matched placebos, a train/holdout split and a cost ladder. They passed all of them because the controls ran through the same fill engine and received the same favourable selection. What caught it was trying to write the live agent: nineteen realisable state machines all lost money against a backtest claiming +$68.75 a trade.
Kind 3: the live agent is not the backtest
The inverse of kind 2 happened in one of our paper-traded sleeves. The backtest committed to the oldest waiting level and scanned forward from it. The live agent held a single level slot and overwrote it on every qualifying bar close. The newest level is the low of the bar that just closed, where price still sits, so live the agent entered almost immediately instead of waiting for a pullback.
Same code, only the level handling swapped:
| Level handling | Trades | Total | Win rate |
|---|---|---|---|
| Keep the waiting level (backtest) | 7,846 | +$543,728 | 59.7% |
| Overwrite every bar (live code) | 9,416 | −$104,103 | 50.6% |
The live paper record over its first months had a 51.3% win rate: matching the live semantics, not the backtest. The backtest column was itself built on the forward scan from kind 2, so its total was optimistic as well; the table is evidence of the gap between two implementations sharing one name, not of the strategy’s value. Neither the backtest nor the agent was wrong in isolation. They implemented different strategies under the same name. There is a sting in the order of fixes too: with the overwrite still in place, removing one of the sleeve’s filters made the result worse, while after fixing the overwrite the same change helped. Fix semantics first, then tune.
Kind 4: the close decides and fills
The bar’s close is the most dangerous number in a backtest because it is both the last information in the bar and a plausible fill price.
On ES, an EMA 9/20 pullback that filled at the EMA inside the bar whose close confirmed the signal printed +$2,290,997. Waiting for the next tradable price, it lost $459,730. The lookahead was worth $2,750,727 on 12,130 trades. The EMA 9/20 test shows the three runs.
A related trap is a breakout filled at the level when the market gapped through it. On NQ, an NR4 breakout we had been running filled at the level after seventeen untested overnight hours: +$447k at t = 7.5. Filled at the worse of the level and the open, it lost $70k. That sleeve was retired from our live board.

What four of our own bugs were worth, with the bug and without it. Overlapping positions and the hindsight fill are the ES trend pullback from kind 2 and the last section; the same-bar target first is a reader’s delta strategy on NQ, +$126k with the target booked first and −$13.7k without; the level fill is a breakout agent’s paper record, +$61.3k booked at the level and +$19.9k at prices that traded.
Signatures worth an alarm
None of these proves a bug. Each has preceded one in our work:
- An intraday Sharpe around 5 from a price pattern. The rejection-block artefact showed exactly that before same-bar fills were forbidden.
- Long and short both winning about the same. A confluence backtest of ours showed both sides “winning” roughly $400k. A real directional edge does not pay both directions equally; a fill artefact does.
- A rule you included as a loser turning profitable.
- Outcome dispersion smaller than the barrier in a classification model.
- The trade count moves when you fix something unrelated. The fingerprint of selection.
- The backtest beats every realisable live implementation by a wide margin.
Detection methods
Truncation test. For a sample of decision times, recompute the decision using only data up to that time and compare with the full-history run. Any difference is a leak. This catches smoothed states, full-sample normalisation and centred indicators.
Single-pass replay. Write the strategy as an agent that receives events one at a time, run it over the same history, and compare its trades with the backtest. Differences are bugs in one of the two.
Shuffle and shift. Shift the signal one bar later: a real edge degrades gradually, a leak collapses. Shuffle the signal across days: anything left is structure, such as drift, in the test.
Live parity. Replay archived live ticks through the production code and match each live trade. Part 19 covers it.
A found bug is the most dangerous moment
Before we found the fill-selection bug, we had already fixed a different one in the same function: overlapping positions, and fixing them had already cut the pullback from about +$3.0 million to +$1.39 million. That fix was real and large, and it felt like the correction. Nobody looked for a second defect in the same code afterwards. Across the 46 S&P claims we re-tested, lookahead decided two of the kills. Finding a bug buys the rest of the code a trust it has not earned. After every fix, look again.
Checklist
- Compute every feature from data available strictly before the decision.
- Fit models walk-forward; trade filtered, not smoothed, states.
- Lag any external snapshot to the last one available before the session.
- Name label columns explicitly and assert they are not features.
- Resolve resting orders in one chronological book.
- Never fill inside the bar whose close produced the signal.
- Fill breakouts at the worse of the level and the next traded price.
- Keep a known-losing control rule in your tests; if a filter rescues it, look for a leak.
- Run a truncation test and a single-pass replay before believing a result.
- After each bug fix, search the same code for the next one.
Even with no lookahead, a backtest can be optimistic about fills that never happen. Part 7: Limit order fill assumptions, why a touch is not a fill.
Frequently asked questions
What is lookahead bias in backtesting?
Any use of information that was not available at the moment the decision or fill happened. The obvious form is a signal computed with future prices. The quieter forms are choosing which of several orders fills using knowledge of how they worked out, using the bar's close to decide a fill inside that bar, or fitting a model on the full sample and then trading its in-sample states.
What are common examples of lookahead bias in trading strategies?
Smoothed hidden Markov model states, which use the whole series; an option-chain snapshot from the end of the session it gates; a trade direction read from the bar the trade fills in; swing-point anchors used before the bars that confirm them; and a label column that slips into the features. In our runs these turned a Sharpe of 0.59 into 1.84 and produced an impossible t = +31.4.
How much can lookahead inflate a backtest?
Enough to change the sign. On an ES trend pullback, a forward scan that chose fills with hindsight showed +$1,385,292 at t = 19.57; resolved chronologically, the same rule lost $1,779,525 at t = −23.99. An EMA 9/20 pullback on ES filled inside the bar whose close confirmed it printed +$2,290,997 and lost $459,730 once it waited for the next tradable price.
How do I detect lookahead bias in my own code?
Run a truncation test: compute every decision on data cut off at that moment and compare with the full-history run; any difference is a leak. Replay the strategy as a single-pass state machine and compare its trades with the backtest. Treat signatures as alarms: an intraday Sharpe near 5, long and short versions both winning the same amount, a losing control rule turning profitable under a filter, or outcome dispersion smaller than the target.
Do placebo tests catch lookahead?
Not when the placebo runs through the same flawed machinery. Our forward-scan fill bug passed random-entry placebos, distance-matched placebos, a train/holdout split and a cost ladder, because every control used the same fill engine and got the same favourable selection. A control tests the signal, not the engine.