Quant Analytics: from scratch to pro · Backtesting done right · Intermediate
Look-ahead bias and other leaks into a backtest
A backtest leaks when the rule uses information that was not yet available at the time of the trade. Leaks are the most common reason a backtest looks excellent and the live result does not. This lesson shows the main forms and a test for each.
In this lesson
- Define look-ahead bias and give four ways it enters a backtest
- Align a signal and a trade so that the signal uses only past information
- Quantify how large a one-bar leak can be on a small example
- Apply a lag test and a point-in-time check to find leaks
Read first: What a backtest is, and what it is not, Clean data: timestamps, time zones, DST and survivorship
A backtest is only as honest as its clock. If the rule sees even a little of the future, the result is a simulation of something nobody could do. This is look-ahead bias, and it is the commonest reason a result collapses when it meets live prices.
The principle
At every decision time t, the rule may use only what was known at t, in the form it was known. Three words carry the test: known, at t, as it was.
Leaks usually come from code that is correct in arithmetic and wrong in timing.
Four common leaks
- Same-bar signals. The rule reads the close of a bar and trades at that same close, or even at the open of that bar. The close is not known until the bar ends. A signal built from bar
tis traded in bart+1. - Whole-sample statistics. Standardising with a full-sample mean, choosing a threshold from the full sample, or fitting a model on all the data before testing. Early decisions then use numbers from the future. Compute every statistic on a window that ends at the decision date.
- Late or revised data. Economic and company figures arrive after the period they describe, and often change. A database that stores the final figure under the reference date hands the rule a number nobody had. This is what point-in-time data fixes.
- A universe chosen with hindsight. Testing only on instruments that exist or are large today leaves out those that failed or were delisted. See survivorship bias and clean data.
Timestamps add a quieter version. A bar labelled by its start time (10:00) contains prices up to the end of the hour. If the rule treats 10:00 as the time of the last price, it acts one bar early. The label convention must be known for every data source.
Worked example
Daily returns for five days are +1%, -2%, +1.5%, -1% and +2%. The rule: be in the market when the day is positive.
Leaked version. It holds on days 1, 3 and 5, the days whose sign it already knows: 1.01 x 1.015 x 1.02 - 1, a return of 4.57%. It never loses, because it picks days by their outcome.
Honest version. The sign of day t is known only at the close, so the position is held on day t+1. After day 1 (positive) it holds day 2 (-2%). After day 2 (negative) it stays out of day 3. After day 3 (positive) it holds day 4 (-1%). After day 4 it stays out of day 5. The result is 0.98 x 0.99 - 1, a loss of 2.98%.
The leak turned a loss into a gain, and it was worth 7.55 percentage points on five days. Real leaks are subtler, but the effect has the same shape: a small error in timing produces a large, smooth, convincing equity curve.
Tests that find leaks
- Lag test. Delay every signal by one extra bar. If the result improves, or barely falls, something is wrong. Honest signals lose a little from a delay; leaked ones often collapse, because the leak was the edge.
- Too good to be true. Very smooth curves with few losing days, or a Sharpe ratio far above what large, well-studied markets allow, call for a search for the leak before anything else.
- Truncation test. Rerun the rule on data cut off at an earlier date. Decisions up to that date must be identical to the full run. If they change, the rule used later data.
- Date audit. For every data field, record when it became known, not only what period it covers.
Common mistakes
- Using the daily high or low to decide an entry at the open of the same day.
- Adjusting prices for splits and dividends with factors computed from later data, without checking how the rule uses the adjusted levels.
- Fitting parameters on the full history, then calling the same history a test. See walk-forward and the locked holdout.
- Joining two data sets on a calendar date when one is stamped in a different time zone.
- Labelling trades with outcomes that run beyond the decision date in machine-learning work. See leakage and purged cross-validation.
In practice
Build the test so that leaks are hard to write: one function that, given a date, can see only earlier rows. Record the time a value was known next to the value itself. Data on FinUties provides price series to practise these checks on; the data APIs are free after registration.
Worked numbers
Each result is recomputed from the formula shown.
| What | Formula | Result |
|---|---|---|
| leaked rule, return on positive days only | 1.01 * 1.015 * 1.02 - 1 | 4.57% |
| honest rule, lagged by one day | 0.98 * 0.99 - 1 | 2.98% |
| value of the leak | (leak_ret - honest_ret) * 100 | 7.55 |
Check yourself
A rule holds a position on day t if the return on day t is positive. Why is the backtest wrong?
The sign of day t is known only when day t ends. Holding during day t on that basis uses the future. The position must come from day t-1 or earlier.
Daily returns are +1%, -2%, +1.5%, -1%, +2%. What does the leaked rule earn, and what does the honest one (hold the next day if today was positive) earn?
Leaked: 1.01 x 1.015 x 1.02 - 1 = 4.57%. Honest: it holds on days 2 and 4 only, 0.98 x 0.99 - 1 = a loss of 2.98%. The leak is worth 7.55 percentage points.
A test uses the whole sample's average and standard deviation to standardise a signal. What is the leak?
Early dates are scaled with numbers computed from later data. Use only an expanding or rolling window that ends at the date of the decision.
A fundamental figure for a quarter appears in the database under the quarter end date. Why does that leak?
The company reported weeks later and may have revised it. The rule saw the number before the world did. Use the date it became known, and the figure as first published.
Key terms
Sources
- Advances in Financial Machine Learning. Wiley (Marcos López de Prado), 2018. Accessed 9 Nov 2026
- The Econometrics of Financial Markets. Princeton University Press (John Y. Campbell, Andrew W. Lo, A. Craig MacKinlay), 1997. Accessed 9 Nov 2026