strateva.

Articles

Look-ahead bias: when your backtest can see the future

In this article

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.

The same one-month slip, over 153 years of real marketsRule: invested when the S&P 500 is above its 10-month average. With look-ahead the Sharpe is 1.56; applying the signal the next month, 1.00; two months late, 0.84. Buy and hold gives 0.71. Caveat: this series uses the monthly average of daily closes, which smooths prices (autocorrelation of 0.27) and flatters any trend rule; that is why the two-month delay is shown too.
×1×100×10k×1M×100M×10bn18901920195019802010
  • 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() or fillna(method="bfill") fills a gap with the next available value — that is, with the future.
  • Forward-looking joins. A merge_asof with direction="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).

Related cases

RF100 — Can a Random Forest beat simple momentum?

All articles

CONTACT

Tell me about your strategy. I will say whether it fits within 48 business hours.

Tell me what you want checked, the asset, the frequency and what you have: rules, a report or code. I will tell you whether it fits and which plan it would be.

What to include in your email

  • Market, instruments and frequency (Spot only).
  • What you want checked, and the period.
  • Language and approximate code size, without pasting the code.
  • What data you have, and whether you need an NDA.

Do not send private code, keys or passwords.

stan@strateva.ai

Reply within 48 h (business days). No private material in the first message. No obligation to commission work.

Who replies

I am Filip Stanciu. I have an MSc in Quantitative Finance from the University of Alcalá, a degree in Finance from the Complutense University of Madrid, and a MiFID II certificate. I have worked in quantitative research for an asset manager, validating stock and ETF strategies. I read and reply to every email myself.

I am not a certification body and I am not a large team. I am one person, and I say so.

Nicolae Filip Stanciu