Backtest overfitting: how to tell an edge from a lucky curve

Backtest overfitting is tuning a strategy until it fits the noise in past data. How to spot it, and how to check for data snooping and look-ahead bias.

Written by
The EdgeLuma team
Published
Updated
Reading time
10 min
Table of contents
  1. What is backtest overfitting?
  2. Why does testing many versions inflate the result?
  3. What is look-ahead bias?
  4. What happens when one candle hits both the stop and the target?
  5. Overfitting, data snooping and look-ahead bias compared
  6. How many trades does a backtest need?
  7. How to check if a backtest edge is real
  8. What EdgeLuma does about overfitting
  9. Frequently asked questions

Backtest overfitting is tuning a strategy until it fits the random noise in past data instead of a pattern that repeats, so it shines in the backtest and fails on new data. It gets more likely with every version you try, and a backtest can flatter a strategy in quieter ways too, one of them hidden inside a single candle.

A backtest that looks too good usually is. Nobody has to cheat for that to happen. Each time you look at a result and adjust a rule, you nudge the strategy toward the stretch of history you’re testing on. After enough nudges, the rules fit that history and nothing else.

This guide explains what backtest overfitting is and why trying many versions of a strategy inflates the best one. Then it covers look-ahead bias, including the candle that touches both your stop and your target, puts the three problems side by side, and explains how many trades a result needs before you can trust it. It finishes with five checks that separate a real edge from a lucky curve, and with what EdgeLuma takes care of for you.

What is backtest overfitting?

Backtest overfitting is tuning a strategy’s rules so closely to historical data that they capture its random noise instead of a pattern that repeats. Traders also call it curve fitting. An overfit strategy looks excellent on the data it was built on and fails on any other.

It rarely takes bad intentions. A typical case: you test a moving-average crossover with a 20 and a 50-period average, and it loses money. You try 15 and 45, then 18 and 52, then a dozen more pairs. After 30 tries, one pair looks great. Part of the reason it looks great is that it’s the luckiest of the 30, and luck doesn’t carry over to the next year of data.

Bailey, Borwein, López de Prado and Zhu showed that a strategy can reach impressive simulated performance after only a fairly small number of configurations have been tried (Pseudo-Mathematics and Financial Charlatanism, 2014). In The Probability of Backtest Overfitting, the same authors proposed a way to estimate how likely a backtest is to be overfit, using the results of every version that was tested.

Before you trust the best of 30 versions, ask how good the best of 30 strategies with no edge would have looked.

Why does testing many versions inflate the result?

Every version you test is another roll of the dice. Roll enough times and one version will look good by chance, and the version you keep is, by definition, the one that rolled best. Statisticians call this data snooping: the same data gets used over and over to choose between models, so the winner’s result is inflated by the choosing itself. Halbert White proposed a test for it in A Reality Check for Data Snooping, published in Econometrica in 2000.

In practice, you have to discount the result. The more variations you tried before finding it, the more of it you should assume is luck. Many practitioners simply cut a backtest’s Sharpe ratio, its return per unit of risk, in half. In a paper simply called Backtesting, published in The Journal of Portfolio Management in 2015, Harvey and Liu call that halving only a rule of thumb and work out the “haircut” from the number of tests behind a result instead. The cut they find is uneven: the strongest results lose only a moderate share, while marginal ones are cut hard, because they are the most likely to be false discoveries.

Three habits help:

  • Keep a log of every version you run, not only the one you keep.
  • Prefer rules with few settings. Every free parameter is one more way to fit noise.
  • Be suspicious of sharp peaks. If a stop at 1.8 times the average range works and 1.7 or 1.9 don’t, the 1.8 is fitted. A pattern that really repeats usually survives a small change in its settings.

Our RSI strategy test on Bitcoin is written this way: the rules were fixed before the first run, the one filter was chosen before it was tested, and every run made for the article is in its tables.

What is look-ahead bias?

Look-ahead bias is the use of information in a backtest that wasn’t available at the moment the trade decision was made. It’s one of the most damaging mistakes a backtest can make, because it can turn almost any idea into a winner. It’s also one of the hardest to see, since nothing in the results looks wrong.

It usually gets in one of three ways:

  • Acting on a candle before it has closed. A signal computed on a candle’s close can’t be traded at that same candle’s open.
  • Using levels that were only confirmed later. A swing high exists only once the candles after it have formed. If the backtest marks it on its own candle, it trades on it before any real trader could have.
  • Assuming you know what happened inside a candle. A candle records its open, high, low and close, but not the order in which they happened.

The last one needs its own section, because every backtest built on candles runs into it.

What happens when one candle hits both the stop and the target?

When a single candle’s range covers both your stop and your target, the candle alone can’t tell you which level price reached first. A backtest that assumes the target came first books a win that may really have been a loss. Repeat that over hundreds of trades and you can end up with an edge that never existed.

There are two sound ways to handle it. The first is to look inside the candle: read the same stretch of time on smaller candles, one minute at a time, and see which level price crossed first. The second, for the cases that even one-minute candles can’t settle, is to assume the worst and count the stop.

One hourly candle touches both the stop and the target. Read on one-minute candles, the same hour shows the stop was reached first, so the trade is booked as a loss.TargetEntryStop?One 1h candleboth levels inside itthe stop came firstthe target, laterThe same hour, minute by minutebooked as a stop: a loss of 1R, not a win
One hourly candle touches both levels. The one-minute candles inside it show the stop was hit first, and when even those can’t tell, the stop is counted.

Overfitting, data snooping and look-ahead bias compared

The three are easy to mix up, because each one makes a backtest look better than it is. They come from different places, though, and each has its own check:

ProblemWhat goes wrongThe warning signThe check
OverfittingThe rules fit the noise of the data they were tuned onGreat on that data, weak anywhere elseRun the final rules once on data you held back
Data snoopingMany versions were tried on the same data and the best one was keptThe result came out of a long searchCount the versions and discount the result
Look-ahead biasThe backtest used information nobody had at the timeToo good to be true, with no visible errorCheck that every signal waits for a closed candle and a confirmed level

How many trades does a backtest need?

More than most people expect. Expectancy, the average result per trade, needs a real sample before it means anything. Over 10 or 20 trades it mostly measures luck, and only at around 100 trades does it start to tell you something. A backtest that covers five years but produces 12 trades is still a small sample.

The same goes for every other number in the report. A win rate measured over 15 trades can change completely with the next five. A maximum drawdown from a short history is the worst thing that has happened so far, not the worst thing that can happen.

Our guide to how many trades a backtest needs works the number out for different kinds of strategy, and measures how often luck alone produces a result that looks like an edge.

How to check if a backtest edge is real

No single test proves an edge. Together, these five checks make a lucky curve much harder to mistake for one:

  1. Hold data back. Split your history, build and tune on the first part, then run the final rules once, unchanged, on the second. This is called out-of-sample testing. If the result collapses on data the rules never saw, they were fitted.
  2. Test in different kinds of market: rising, falling and flat. An edge that only shows up in one of them is really a bet on that kind of market.
  3. Nudge every setting. Move each parameter a step in each direction. If the result fades a little, fine. If it collapses after a one-step change, the original setting was fitted.
  4. Include costs. A strategy that only just makes money before costs often loses after them, as our guide to fees and slippage shows.
  5. Read the trades. If one or two trades account for most of the result, take them out and see what’s left.
One price history split in two: the rules are tuned on the first part, then run once, unchanged, on the part held back.Tuned herein-sample: the rules are fitted on this partChecked here, onceout-of-sample: never seen
Build on one part of the history and check on the other. The rules are tuned on the first part, then run once, unchanged, on the part held back.

What EdgeLuma does about overfitting

Software can fix some of these problems for you. The rest depends on how you work.

What EdgeLuma takes care of

  • The candle that touches both levels. EdgeLuma checks every fill, stop and target on the smallest candles it has inside each candle, one minute wherever they exist. When a candle still touches both levels, it counts the stop.
  • Unfinished candles. Signals are read when a candle closes, and the order goes in on the next candle.
  • Swing points confirmed after the fact. A swing high or low is only used once the candles that confirm it have closed.
  • Sample size. The report always shows the number of trades, so you can see how much evidence sits behind every other number.

What stays with you

  • Counting the versions you try, and stopping before the curve looks perfect.
  • Holding data back. You choose the period of every backtest, so you can build on one stretch of history and run the final rules once on another.

For the full method, from writing the rules to reading the report, see how to backtest a trading strategy.

Test a strategy yourself: start with the Free plan, no card needed.

Key takeaways

  • Overfitting, also called curve fitting, means tuning a strategy until it matches past data so closely that it describes random noise. The more versions you try, the more likely your best one is simply the luckiest.
  • Look-ahead bias means the backtest used information nobody had when the trade was taken, like a swing high that was only confirmed later. It can make almost any idea look good.
  • A candle that touches both your stop and your target doesn’t show which came first. A backtest that counts the target turns some losses into wins.
  • Over 10 or 20 trades, almost any result can be luck. Around 100 trades, the average starts to mean something.
  • To check an edge, hold back data you never tune on, change one rule at a time, nudge every setting and keep count of the versions you try.

Frequently asked questions

What is the difference between overfitting and data snooping?

Overfitting describes the result: rules that fit the noise of the data they were tuned on. Data snooping describes the process that usually leads there: reusing the same data to choose between many versions, so the winner’s result is inflated by the choice itself.

How do I know if my backtest is overfit?

Look for the warning signs: many versions tried before this one, lots of free settings, a result that changes sharply when a setting moves a little, and performance that collapses on data the rules weren’t tuned on. The more of these apply, the more of the result is probably luck.

What is out-of-sample testing?

Out-of-sample testing means running a strategy, unchanged, on data it wasn’t built or tuned on. It’s the closest a backtest gets to the future. If the rules only work on the data they were fitted to, the out-of-sample run usually shows it.

Is a candle that touches both the stop and the target a problem?

Yes, because the candle doesn’t record which level price reached first. Checking the same stretch on smaller candles settles most cases. For the rest, counting the stop keeps the backtest from booking wins that may have been losses.

Can a strategy with a good backtest still lose money?

Yes. A backtest replays the past with the rules and costs you chose, and markets change, so an edge that was real can fade. A careful backtest lowers the odds of trading an idea that never worked, but it can’t promise anything about the future.

Written by the EdgeLuma teamEdgeLuma is a backtesting platform for crypto traders. Describe a trading strategy in plain words: EdgeLuma turns it into rules, replays them on years of real candles with the exchange's real fees and slippage, and shows every trade and every number, even when the strategy loses.

Every article

Keep reading

What took years of chart-watching now takes seconds.