Look-Ahead Bias
Look ahead bias happens when a backtest uses information that was not available at the time. Learn common sources, real examples and how to prevent it in code.
Look ahead bias occurs when a backtest uses information that would not have been known at the moment a trading decision was made. Even a small leak of future information can make a worthless strategy look brilliant, because knowing the future, even slightly, is enormously profitable on paper. It is one of the easiest mistakes to make and one of the hardest to spot, especially in vectorised code where all data is available at once.
Common sources#
| Source | Example |
|---|---|
| Same bar signal and fill | Using today's close to generate a signal and also filling at today's close |
| Restated fundamentals | Using revised earnings that were published months later. See Point-in-Time and Survivorship-Free Data |
| Reporting lags ignored | Using quarterly results on the quarter end date rather than the filing date |
| Today's index members | Selecting stocks from the current index list for past dates. See Survivorship and Selection Bias |
| Full sample normalisation | Scaling data with the mean and standard deviation of the entire history |
| Centred indicators | Moving averages or smoothing that use future values |
| Adjusted prices misuse | Using back adjusted futures levels for absolute price rules. See Continuous Futures and Back-Adjustment |
| Time zone misalignment | Treating a close in one market as known before another market's close. See Timestamps, Time Zones and Daylight Saving |
| Bar high and low | Assuming a stop and a target were both possible without knowing which was hit first |
Examples#
How to prevent look ahead bias#
- Lag signals: trade on the next bar's open or later after a signal from a completed bar.
- Use point in time data with actual publication dates.
- Use historical index memberships.
- Compute all indicators with trailing windows only. See Rolling and Expanding Windows.
- Fit scalers and models on training data only, then apply them forward. See Data Leakage.
- Use event driven backtests that process data in time order. See Backtesting Methodology.
- Write tests: for example, shift data one bar into the future and confirm results change; if they do not, the code may already be using future data.
A quick sanity check#
If a strategy's backtest looks too good, try delaying all signals by one extra bar. Genuine edges usually weaken a little; strategies driven by look ahead bias often collapse completely. Also check performance on the exact day of the signal: returns concentrated on the same bar as the signal are a red flag.
In code#
In pandas, shift(1) moves data forward one row so today's decision uses yesterday's data:
df["signal"] = (df["close"] > df["close"].rolling(20).mean()).astype(int)
df["position"] = df["signal"].shift(1) # act on the next bar
df["strategy_return"] = df["position"] * df["close"].pct_change()
Forgetting the shift is one of the most common bugs. See NumPy and Pandas for Traders.
Frequently asked questions#
What is look ahead bias?#
Using information in a backtest that would not have been available at the time of the trading decision, making results unrealistically good.
How do I avoid look ahead bias?#
Lag signals, use point in time data with real publication dates, compute indicators with trailing windows and fit models only on past data.
How can I detect look ahead bias in my backtest?#
Delay signals by an extra bar and see whether performance collapses, and check whether returns cluster on the same bar as the signal.
Next, learn about missing failures in data in Survivorship and Selection Bias.
3 quick questions on this lesson. Get them all right to finish it.
Turn on JavaScript to take the quiz.
Mentioned in
- Why Strategies FailResearch and Backtesting
- Historical Data for BacktestingResearch and Backtesting
- Overfitting and Curve FittingResearch and Backtesting
- Event-Driven vs Vectorized BacktestingResearch and Backtesting
- Portfolio and Multi-Asset BacktestingResearch and Backtesting
- Corporate Actions, Delistings and Rolls in BacktestsResearch and Backtesting