Blog / Trading systems
Look-ahead bias: how a backtest lies to you
Look-ahead bias is when a backtest lets a strategy use information it could not have had at the moment it made a decision. The strategy is not cheating on purpose. The test harness hands it the future by accident, and the results come out better than anything the strategy could do live.
It is the most common reason a strategy that looked excellent in testing falls apart in its first weeks with real orders. We found it in one of our own earlier backtesters, which is why our current simulator is built the way it is.
How the future sneaks in
It is almost never one dramatic line of code. It is small, reasonable-looking shortcuts:
- Using the bar’s close to decide, then filling at the same bar’s open. The strategy reads where the bar ended, then gets an entry price from when it started.
- Indicators calculated on the whole dataset at once. A moving average or a normalisation step that runs over every row before the loop starts can leak later values into earlier rows.
- Resampling without care. Turning one-minute bars into hourly bars and labelling each hour by its start time, so the strategy “sees” an hour that has not finished yet.
- Stops and targets resolved inside one bar. If a bar touched both your stop and your target, the backtest has to decide which came first. Assuming the good one every time is look-ahead too.
- Data that was revised later. Some data sets are corrected after the fact. Testing on the corrected version uses information nobody had on the day.
If a backtest result surprises you in a good way, look for the leak before you look for the champagne.
Signs your backtest can see ahead
None of these prove a leak, but each one is a reason to go looking:
- Win rates that are far above what anyone trading that market by hand would believe.
- Entries that land almost exactly on the high or low of a move, again and again.
- Results that get dramatically worse when you add a one bar delay between signal and fill.
- A strategy that performs well in sample and falls apart out of sample on every instrument at once.
The one bar delay test is the cheapest and most useful. Shift every fill one bar later than the signal. An honest strategy usually gets a bit worse. A leaking one often collapses.
What we found, and what we changed
Our earlier batch backtester ran the strategy over a full table of bars. Somewhere in the preparation, values from later rows were reaching earlier decisions, and the results were better than they should have been. Patching that one spot would have fixed that one leak. It would not have stopped the next one.
So we rebuilt the simulator so that look-ahead is impossible by construction, rather than something we try hard to avoid:
- One clock owns time. The simulator moves forward one bar at a time, and nothing else decides what “now” is.
- The strategy gets a read-only buffer of the past. At each step it is handed the bars that have closed, and only those. There is no object in its reach that holds a future bar.
- Fills happen after the decision. An order placed on one bar is filled on the price sequence that follows it, with latency and slippage modelled.
- Intrabar order is resolved from finer data. When a bar touches both stop and target, we look at the one second prices underneath to see which came first.
- The simulator and the live engine are checked against each other. Fed the same data, they must make the same decisions bar for bar.
Walk forward, and label everything
Even a clean simulator can be fooled by tuning. If you adjust parameters until the test looks good, you have fitted the strategy to the past. Walk-forward testing fixes that: tune on one window, score on the next window that the tuning never saw, slide both forward and repeat. Then run it across many instruments, so a result that only works on one contract stands out.
Finally, label every equity curve with what produced it. In sample and out of sample should never share an axis without saying so. A curve you cannot trace back to its run is a curve you should not trust.
The short version
Look-ahead bias is the backtest handing the strategy the future. Look for it whenever results seem too good, test with a one bar delay, and if you are building the tools yourself, design the simulator so the future is simply not reachable. A smaller, honest result is worth far more than a large one that the market will take back.
This post is about software engineering for backtests. It is not investment advice, and no testing method can guarantee that a strategy will make money.
Where this comes from. We do this work for clients. See trading systems, or the case study on Naxfront Systems.
Questions about this post? Email contact@naxfront.com. Nancy reads every one.