What is lookahead bias, and how do I check for it?
Lookahead bias is when a backtest uses information on a given day that wasn't available until later. One test catches most of it: change every value after some date t, rerun, and check that nothing computed up to t changes.
Details
Common forms in pandas:
- A negative shift in a feature.
shift(-1)pulls tomorrow's value into today.df["signal"] = df["close"].pct_change().shift(-1) # leaks tomorrow's return df["signal"] = df["close"].pct_change() # known at today's close - Trading at the close you used to decide. If the signal uses today's close, enter at the next open, not the same close.
- Financials dated by period end. A company's numbers become public when the filing is accepted, which is weeks after the quarter ends. Use the filing date.
- Back-adjusted price levels as features. Split-adjusted historical prices depend on splits that happen later. Use scale-invariant features such as returns.
- Fitting a scaler or model on the whole table. Fit on the training period only.
A strategy with a suspiciously high Sharpe ratio is worth checking for lookahead before anything else.