Skip to content

Your backtest bought at a price you could never get

The rule says "buy when the candle closes above the high". Your backtest buys at that close. In real life, that price is already gone.

TradeXAI research · 5 min read

You write a simple rule, run it on two years of candles, and the equity curve climbs like a staircase. You go live. A month later the staircase is a slide.

Very often nothing is wrong with the idea. The backtest simply knew something a moment before you could have known it.

That mistake has a name: lookahead bias. It means the test used information that did not exist yet at the moment of the trade. It is the easiest mistake to make and the hardest to see, because the code runs and the results look great.

The classic version: same candle, same close

A candle's close is only known when the candle has ended.

Say your rule is "buy when the close is above the 20-candle high". You learn that at the close. You cannot also buy at that close. By the time you know, that price is history.

Five candles. A dashed line marks the end of candle 10, where the signal becomes known. A red circle marks the close of candle 10, where the backtest buys. A green circle marks the open of candle 11, where a trader can really buy.
The signal exists only once candle 10 has closed. The first price you can really get is the open of candle 11.

The fix is one line of code: decide on a closed candle, fill on the next one.

In our own research every decision uses closed candles only, and the order is filled on the following candle. For our many-coin bot the rule is stricter still: decide at one close, fill one full candle later.

How much can one candle matter?

For a fast idea, everything.

We tested a strategy that buys right after a sudden crash and sells the bounce. In the period we designed it on, it made +0.29% per trade. Then we moved the entry:

Three bars: entry on time plus 0.29 percent per trade, one minute later minus 0.08 percent, five minutes later minus 0.26 percent.
The same crash-fading rule with a later entry. The whole edge lived in a fill we could not be sure to get.

One minute late: -0.08%. Five minutes late: -0.26%.

A second example. A 1-hour "follow the big candle" rule looked fine on paper. With the signal one candle late, the median result was -0.9 basis points per trade. (A basis point is 0.01%.)

If your edge disappears when you arrive one candle late, you never had an edge. You had a fill you cannot get.

Slow strategies barely notice

Now the good news. For slow ideas the same delay costs very little.

Our Vote+ bot ranks many coins on 4-hour candles and holds for days. Its Sharpe in the design period was 2.41. (Sharpe is return per unit of risk; around 0 means no edge.) With every fill one candle late, it was 2.21.

Two bars: Sharpe 2.41 filled on time and 2.21 with every fill one candle late.
A strategy that holds for days gives up little when its fills come one candle late.

A breakout idea held for days also barely changed when its entry came 15 or 60 minutes late.

That gives you a cheap, honest test for any strategy: delay every fill by one candle and run it again. If the result bends, fine. If it breaks, the profit was in the timing, and live trading will not deliver it.

Leaks that are harder to see

The same-candle fill is the famous leak. These are quieter. We ran into each of them, or guard against it:

  • A tie-break. In one review we found a sort that broke ties using another column. It quietly leaked one-day momentum into a test of single indicators.
  • An average of the whole history. If you normalise a signal with the mean of all your data, every early value already "knows" the future. Use only data up to the current candle.
  • Publication delay. A number stamped with time T is not always available at T. We only used daily stock index closes 48 hours after their date. Exchange positioning data stamped at T was published about six minutes later.
  • The explanation next to a signal. On this site, the numbers in a signal's "why" come from the same closed candle the strategy decided on. The explanation never knows more than the signal did.

A leak test you can copy

  1. Compute your signal on the full history and save it.
  2. Cut the data off at some candle in the middle.
  3. Compute the signal again on the shortened data.
  4. Compare. Every value before the cut must be identical.

If any value changed, something in your code looks into the future.

We planted a one-day lookahead and a whole-history mean on purpose, to check that this test catches them. It caught both.

This article is for education. It is not financial advice.

  • lookahead
  • candle timing
  • fills

For education only. Backtests and paper results do not predict future results. Not financial advice.

More on backtesting pitfalls

‹ All posts