Lookahead bias: the four ways a backtest sees the future

Lookahead bias is not a bug you notice. The backtest runs, the numbers are plausible, and the equity curve is smooth — because the strategy was quietly allowed to read data that had not happened yet.

1. Negative shifts

signal = df["close"].shift(-1) > df["close"]

The minus sign moves tomorrow's close into today's row. In a live system that value does not exist at decision time. The correct form lags the signal instead:

position = (fast > slow).shift(1).fillna(0.0)

2. Future indexing

if df["close"].iloc[i + 1] > df["close"].iloc[i]: ...

Same defect, different syntax. iloc[i+1], iat(i+1) and loc[index+1] all reach one row past the decision point. So does pct_change(-1).

3. Whole-series statistics

z = (px - px.mean()) / px.std()

This is the subtle one. There is no negative sign and nothing looks wrong — but px.mean() is computed over the entire history, including the future. At row 10 the strategy "knows" the average of row 1,000. Any normalisation, threshold, or signal built from a full-sample mean, standard deviation, min, max, median, quantile, skew or kurtosis has the same problem.

The causal version uses only what existed at the time:

z = (px - px.rolling(60).mean()) / px.rolling(60).std()

Computing avg = returns.mean() once for a Sharpe ratio in your report is fine — that is a statistic about the result, not an input to a decision. The distinction is dataflow, not syntax, and it is exactly the distinction a naive scanner gets wrong.

4. Point-in-time data

Fundamentals keyed to the fiscal period rather than the publication date, index membership taken from today's constituents, or a dataset that was revised after the fact. The code can be perfect and the backtest still dishonest, because the data knew the future.

How to catch it

Audit a strategy — 10 free credits