You've coded a simple strategy on an ETF. The backtest shows a Sharpe ratio of 3, almost every year positive and a small maximum drawdown. Before celebrating, ask one question: did the code, at any point, use a piece of data that didn't exist yet at that moment?
That mistake is called look-ahead bias. It's probably the most common flaw in home-made backtests, and it's almost never visible at a glance.
What it is, in one sentence
There's look-ahead bias when a backtest decision is made with information that, in real life, you would only have had after making that decision.
No bad faith required. It's usually one misaligned line of code.
The classic example: trading on the close that generates the signal
Take this rule: "if today's close is above the 20-session average, buy".
The problem is when you buy. Today's close is only known once the market has closed. If the backtest credits you with today's return (yesterday's close to today's close) for a signal that depends on today's close, you're being paid for a move that had already happened when the signal switched on.
The correct version applies the signal the next day: you decide at today's close and earn (or lose) tomorrow's move.
How much it can inflate the result: our own simulation
To measure it we ran our own simulation, for illustration only. We generated prices that are pure noise: random daily returns with zero mean and 1% daily volatility, over five years. By construction, no rule can have an edge on those prices.
On them we ran the 20-session moving-average rule (long or in cash, no costs) in two ways, and repeated the experiment 2,000 times:
- With look-ahead (today's signal earns today's return): mean annual Sharpe of 3.51, positive in 100% of repetitions.
- Without look-ahead (today's signal earns tomorrow's return): mean annual Sharpe of 0.00, positive in 50.7% of repetitions — what you'd expect from chance.
Same rule, same prices: shifting the signal by one day takes it from "spectacular strategy" to "nothing". That's the scale of the problem.
- With look-ahead
- Signal applied the next month
- Buy and hold the index
- Two months later
Source: Robert Shiller’s monthly S&P 500 series, with dividends. No risk-free rate deducted; time out of the market earns zero.
How it sneaks into the code
In Python with pandas, the usual patterns are:
shift(-1)or any negative shift. Pulls tomorrow's value into today's row. Sometimes it's used on purpose to build the label you're predicting; the problem is when that column ends up among the inputs the signal uses.- Forgetting to
shift(1)the position. The signal is computed correctly, but multiplied by the same day's return instead of the next day's. That's the simulation above. - Centred windows.
rolling(20, center=True)averages ten days of past and ten days of future. It looks lovely and smooth on a chart, which is exactly why it's tempting. - Back-filling.
bfill()orfillna(method="bfill")fills a gap with the next available value — that is, with the future. - Forward-looking joins. A
merge_asofwithdirection="forward"or"nearest"can attach to a date a value that was published later. - Scaling with the full history. Computing a mean, volatility or percentile over the whole period and using it on every date. Each day "knows" what the rest of the series looks like.
- Cross-validation that shuffles dates. A standard, shuffled K-fold trains on 2023 data to predict 2019.
Leaks that aren't in the code
Some look-ahead sources can't be seen by reading the program, because they live in the data:
- Fundamental data stamped with the period date, not the release date. Q1 earnings show up dated 31 March, but the company didn't publish them until May.
- Revised data. Some series are corrected after release. The backtest uses the corrected figure, not the one available at the time.
- A universe chosen with today's information. Testing only on companies that exist today, or that are in an index today, is a way of peeking: you know which ones survived. That's survivorship bias, and it has its own article.
- Prices you couldn't get. Buying at the exact close, or at the day's low, when the signal was only known afterwards.
- Your own memory. If you tweak the strategy after seeing how a period went, that period is no longer a clean test, even if the code is perfect.
Why a scanner can't certify it
An automatic scanner can read code and flag suspicious patterns like those listed above. It's useful: it finds in seconds mistakes a person might take a long time to spot. But it has clear limits:
- It doesn't see the data. If a fundamental is stamped with the period date instead of the release date, the code is "clean" and the result is still contaminated.
- It doesn't understand intent. A
shift(-1)can be fine if it only builds the label and never reaches the signal. The scanner flags it; a person has to check where it goes. - It can't find what it doesn't know. There are many ways to move data through time, and some follow no standard pattern.
So a scanner with no warnings doesn't mean "no look-ahead". It means "none of the patterns the scanner knows". The strong test is different: rebuild the strategy independently, with an engine that applies the delay between signal and order itself, and compare.
Warning signs
- A very high Sharpe from a simple rule on daily stock or ETF data.
- Very few losing months, even through sharp market falls.
- The strategy "gets it right" on the day of the big move, not the day after.
- Delay every signal by one day and the result collapses. A real edge usually loses something with a one-day delay; if it vanishes outright, be suspicious.
You can run that last test yourself today: add a one-day delay to every signal and see what's left.
Where it fits in a validation
Our validation can include a bias scanner that analyses Python code for look-ahead and label leakage patterns like the ones in this article. The scanner only reads Python: for Pine Script, MQL or other languages we have no automatic scanner. Either way, the scanner is an aid, not a certificate; what carries the most weight is reproducing the strategy with an engine that applies execution delay and costs on its own. In our public RF100 case, the model was designed to be causal — that is, so that each prediction uses only data available on its date.
These checks are part of a quantitative validation. Strateva validates stock, ETF, spot crypto and spot FX strategies, always unleveraged and without margin. We do not take work on CFDs, futures, perpetuals or options.
To check whether it fits, email stan@strateva.ai with the market (stocks, ETFs, crypto or FX, spot only), frequency, the language the strategy is written in and where your data comes from. Please don't send private code or passwords in a first message.
Educational and research content. Not investment advice. The simulation is our own and is illustrative: random prices with normal daily returns of zero mean and 1% standard deviation, 1,260 sessions (about 5 years), rule "long if the close is above the 20-session average, otherwise in cash", no costs, 2,000 repetitions, seed 20261116. Sharpe annualised with √252. With look-ahead: mean 3.51 (P5 3.12; P95 3.92). Without look-ahead: mean 0.00 (P5 −0.75; P95 0.73).