Look-ahead bias is a backtest using information that was not available when the decision was made, such as filling at the close of the bar that produced the signal or reading data published later. Find it by checking when each value was known, shifting signals by one bar, and seeing whether results collapse.
Where it usually hides
- Filling on the signal bar. A signal computed from today's close cannot also buy at today's close: by the time the close is known, it has passed. The honest fill is on the next bar.
- Vectorised code without a shift. In pandas,
signal = close > close.rolling(20).mean()multiplied byreturnson the same row trades a return the signal already saw. It needssignal.shift(1). - Using the bar's high or low to decide. A rule that reads
highto set an entry inside the same bar knows where the bar went before it got there. - Indicators fitted on the whole series. A z-score using the full-sample mean, a scaler fitted on all the data, or a hedge ratio estimated over the whole history all carry the future into every bar.
- Data stamped with the wrong date. Earnings, fundamentals and economic releases are often stamped with the period they describe, not the day they were published. A quarter ending 31 March may not be public until May.
- Resampled bars. An hourly bar stamped 10:00 holds prices up to 10:59. Treating it as known at 10:00 is an hour of look-ahead.
- Today's index members. Testing on the stocks in an index now uses knowledge of which companies would survive and grow. That one has its own name: survivorship bias.
How to find it
- Shift everything by one bar. Delay every signal by one more bar than you think is needed. A real edge loses a little. A result built on look-ahead loses most of its return.
- Check impossible numbers. Win rates above 70% on a simple rule, almost no losing months, or a Sharpe ratio above 3 on daily bars are worth suspecting before celebrating.
- Trace one trade by hand. Pick a trade, note the bar that produced the signal and the price it filled at, and check that the fill came after the signal bar ended.
- Ask when each input was known. For every column, write down when that value was first public. Anything stamped earlier than that is a leak.
- Run it bar by bar. An event-driven loop that only ever sees past bars cannot read the future by accident, and comparing it with a vectorised version exposes a missing shift.
How Reamer Research handles it
Reamer Research runs your strategy one bar at a time. The window passed to on_bar holds only bars that have closed, with index -1 the bar that just closed. An order returned from that bar is eligible from the next bar's ticks and is never matched against the bar it was decided on. So the most common leak, filling on the signal bar, cannot happen.
import reamer_research as rr
def on_bar(d): # also runs as a class or a configured object
if d.close[0] == 0: # window still filling
return None
if d.close[-1] > d.close.mean() and d.position() == 0:
return rr.buy_market(10) # fills on the next bar, never this one
Bars on several tickers run on one wall clock. A bar reaches on_bar when it closes, at its ticker's next timestamp, so an hourly bar stamped at the start of its hour is seen when the hour ends. For non-price data, sidecar values become visible at the time they are stamped with. The exogenous data guide covers stamping them with when they were first known.
Limits to know
- Your timestamps are trusted. The engine controls when each bar and value becomes visible; it cannot know when your data was really published.
- Anything you compute outside the run is yours to check. A parameter or hedge ratio fitted on the whole history before the run still leaks into every bar.
A $20 month includes the full kit, so you can run your own strategy through it and compare the fills with your current backtest.