1. Valmy
  2. Learn
  3. Testing ideas
  4. X4

Why did my backtest work when live trading didn't? Overfitting and data snooping

Intermediate 11 min read

Short answerOne important reason is that the backtest was the best of many tries; others include optimistic fills and costs, look-ahead bias, a changed market and a live period too short to judge. When you test many settings on the same data and keep the one that did best, its result is biased upward: some settings fit that period's random ups and downs, and those fits do not repeat. This is overfitting, and searching one dataset many times is data snooping. In the synthetic example below, the best of 200 moving-average pairs made +10.1% in the ten days of random prices it was chosen on, then +0.2% in the next ten days, worse than 179 of the other pairs.

Each of these reasons can explain a gap on its own, and they add up. Counting every rule and setting you tried tells you how much to discount your best backtest before real money depends on it: the more you search, the better the best result looks, whether or not anything real was found.

Valmy noteAs specified on 25 November 2026, Valmy offers backtests and paper trading before a live deployment, and lists historical optimization among features planned after v1. A user can still run many backtests by hand, and each one counts as a try. Details in the live app could differ, so check them there. Nothing in this lesson is a recommendation to trade a rule.

Why the best of many backtests is biased upward#

A backtest result is partly signal and partly luck. Any rule run on a finite stretch of prices catches some moves by chance. If you run one rule, its luck is as likely to be bad as good. If you run 200 and keep the best, you have selected the one with the most good luck, so its result overstates what it can do on new data.

A coin-toss analogy shows the mechanism. Ask 200 people to toss a coin ten times and pick the one who got the most heads. That person almost certainly got eight or more and probably nine or more, yet their next ten tosses will average five heads like everyone else's. Nothing about the winner changed: the selection picked the luckiest run, and luck does not carry forward.

In-sample data are the candles used to design and choose a rule. Out-of-sample data are candles that played no part in the choice. Overfitting shows up as a gap between the two: a strong in-sample result followed by a weaker out-of-sample one.

Data snooping: every try counts, including the forgotten ones#

Data snooping is not limited to automated parameter searches. All of these are tries on the same data:

  • Testing SMA(10)/SMA(50), then SMA(8)/SMA(40), then SMA(15)/SMA(25), and keeping the best.
  • Adding a filter, such as "only trade after 08:00 UTC", because it removed a few losing trades you could see.
  • Switching from one market to another until the rule works.
  • Reading other people's backtests, which are usually the survivors of their own searches.

Sullivan, Timmermann and White (1999) describe a version spread across a whole community: over decades, rules that happened to work get attention and the rest are forgotten, so the survivors' records look better than chance would explain. They quote Jensen and Bennington (1970), who warned that with enough computing time one can find a mechanical rule that "works" on a table of random numbers, as long as it is tested on the same numbers that were used to find it.

Worked example: 200 SMA pairs on a random walk (synthetic data)#

The best of 200 parameter pairs, SMA(15)/SMA(25), made +10.1% in the ten days it was chosen on and +0.2% in the next ten days, ranking 180th of 200: it looked like a find and turned out to be luck. The data are invented five-minute candles built as a random walk, where each candle's move is random and unrelated to the past, so no moving-average rule can have a real edge. Days 1 to 10 (2,880 candles) are the in-sample period and days 11 to 20 the out-of-sample period, with 200 earlier candles of history for the averages.

The rule is a long-or-flat crossover: buy when the fast simple moving average crosses above the slow one, sell when it crosses below. Signals at a candle's close fill at the next candle's open, and each period is judged on its own trades. The search tried 10 fast lengths (2 to 20 candles) and 20 slow lengths (25 to 120 candles), 200 pairs in all. Results are before costs, to isolate the effect of selection.

Result Days 1 to 10 (in sample) Days 11 to 20 (out of sample)
Best in-sample pair, SMA(15)/SMA(25) +10.1% (56 round trips) +0.2% (64 round trips)
Its rank among the 200 pairs 1st 180th
Median pair −0.3% +2.1%
Average of the 10 best in-sample pairs +7.5% +1.7%
Pairs with a positive result 95 of 200 182 of 200
Holding the token +0.3% +6.6%

In sample, SMA(15)/SMA(25) stood out: +10.1% while the median pair lost 0.3% and holding made 0.3%. A trader who saw only that column might conclude the pair captures something real. Out of sample, the market rose 6.6%, most long-or-flat pairs made money, and the chosen pair made only 0.2%, ranking 180th of 200. The ten best in-sample pairs together averaged 1.7%, below the median pair's 2.1%.

Scatter plot of 200 SMA crossover pairs on a synthetic random walk. Horizontal axis: return in the first ten days, where the pairs were chosen, from about minus 5% to plus 10%. Vertical axis: return in the next ten days, from about minus 1% to plus 8%. The best first-period pair, SMA(15) and SMA(25), made plus 10.1% and then plus 0.2%, below the dashed line for the median pair's plus 2.1% in the second period.
Figure 1. Synthetic data. Each circle is one pair. The filled point, the pair a search would have chosen, sits at the far right of the in-sample axis and below the median out of sample. Notice how weakly the cloud slopes upward.

The cloud in Figure 1 has a weak upward tilt (a correlation of 0.21 between the two periods' results), because pairs that react to the same trends tend to win and lose together. That shared behavior is not an edge: on a random walk it is the same luck counted many times. Being first in sample told almost nothing about being good out of sample.

Equity of the best first-period pair, SMA(15) and SMA(25), over twenty days of synthetic random-walk candles, before costs. It rises 10.1% in the ten days used to choose it, then returns 0.2% in the next ten days, moving sideways after day 10.
Figure 2. Synthetic data. The chosen pair's equity before and after the choice. The smooth climb on days 1 to 10 is what a search rewards; the flat stretch on days 11 to 20 is what the trader would have lived through.

Repeating the search on 20 random walks#

One example could be bad luck in the other direction, so the script ran the same search on 20 independent random walks, including this one. On average the best in-sample pair made +6.7% in its first ten days and −1.3% in the next ten. Its out-of-sample result fell below its in-sample result in 16 of the 20 walks, and it beat the median pair out of sample in 10 of 20, no better than a coin toss. The search reliably found an impressive backtest and never found an edge, because none existed.

With costs of 0.30% per round trip, the figure used in How much do fees and slippage change a backtest?, none of the 200 pairs made money in either period of the main example. The best in-sample pair after costs, SMA(12)/SMA(110), lost 4.8% in sample and 6.0% out of sample. A search that ignores costs tends to pick fast, frequently trading pairs, which are exactly the ones costs hurt most.

How much luck to expect: the multiple-testing problem#

Statisticians call this the multiple-testing problem: the more hypotheses you test, the more of them pass any fixed threshold by chance. Several lines of research quantify it for trading.

The expected best result of many tries#

Bailey, Borwein, López de Prado and Zhu (2014) work in terms of the Sharpe ratio, a strategy's average return divided by the variability of its returns, scaled to a year. They show how high the best in-sample Sharpe ratio is expected to be when every strategy tried has a true Sharpe ratio of zero. With one year of data, trying just 10 independent configurations gives an expected best of about 1.57, and their approximation gives about 2.77 for 200 tries. They also estimate that with five years of data, no more than about 45 independent configurations should be tried before an in-sample Sharpe ratio of 1 is expected from luck alone. Their paper uses crossing moving averages as an example of a strategy whose parameters are easy to tune this way, and it stresses that a backtest which does not report the number of trials cannot be judged for overfitting.

The 200 pairs in this lesson were not independent, since neighboring pairs trade alike, so the effective number of tries was smaller. The direction of the effect is the same.

White (2000) proposed the Reality Check, a bootstrap test of whether the best model found in a search really beats a benchmark once the search itself is taken into account. Sullivan, Timmermann and White (1999) applied it to nearly 8,000 variations of simple technical rules on the Dow Jones Industrial Average. Some rules beat the benchmark from 1897 to 1986 even after the adjustment, but the evidence that the best rule added value in the following ten years was scant. Bajgrowicz and Scaillet (2012) revisited the same index through 2011 and found that an investor could not have picked the future best-performing rules in advance, and that low transaction costs offset the rules' performance even in sample.

Harvey, Liu and Zhu (2016) made the same argument for academic research on stock returns. With hundreds of tested factors, they argued, the usual significance hurdle (a t-statistic of about 2) is too low, and a newly found factor should clear a t-statistic above 3.0. A trader running dozens of backtests faces the same arithmetic on a smaller scale.

Live frictions: other reasons live results differ from a backtest#

Even a rule with no overfitting will not reproduce its backtest exactly. The backtest models execution; live trading performs it.

  • Fills. A backtest fills at a modeled price. Live orders pay the actual spread and slippage, can be partly filled, and can be rejected by size filters or insufficient funds. Valmy's paper mode, as specified, simulates partial fills, fees, size filters and available funds, which narrows but does not close the gap.
  • Delays. Time passes between a candle's close, the decision and the order reaching the exchange. In a fast market the price moves in that interval.
  • Costs. A user's actual fee rate, the BNB discount, and funding on perpetuals can differ from the cost model. Funding rates change at every settlement.
  • Regime change. A rule designed on a trending month meets a sideways one, or volatility doubles. Markets change in ways no single backtest period contains; see Is this market trending or stuck in a range?.
  • Interruptions. As specified, after downtime Valmy's runtime rebuilds its indicators, reconciles orders and positions, records missed signals and skips stale ones, then resumes with the next fresh candle. A backtest never misses a signal; a live deployment can.
  • Small samples. Ten days of live trading is a small sample, and a rule with a real but modest edge can lose over it. A gap between backtest and live results is not proof of overfitting on its own.

What could change the conclusion about an overfit backtest#

Overfitting is a matter of degree. Several things make a backtest more or less believable:

  • How many tries, and how they were chosen. Write down every rule and setting you test. A result that is the best of 3 is more credible than the best of 300.
  • Data the search never saw. Hold back a later period, and judge the rule there once. What is out-of-sample testing, and how do I set one up? explains how, and why a hold-out period that is reused for choosing stops being out of sample.
  • Neighbors. If SMA(15)/SMA(25) works but SMA(14)/SMA(25) and SMA(15)/SMA(30) do not, the result is probably a fit to noise. A real effect usually survives small changes.
  • A reason before the result. A rule motivated by a mechanism, written down before testing, is less likely to be a fit than one found by searching.
  • Costs and look-ahead. A result that depends on optimistic fills or zero costs is weaker still; see What is look-ahead bias, and how does it get into a backtest?.
  • Paper trading. Running the chosen version on new prices in a paper account adds genuinely unseen data, as How do I paper trade without fooling myself? explains.

Check your understanding: is the best backtest evidence?#

Question. A trader tests 12 fast lengths and 15 slow lengths of a moving-average crossover on one year of five-minute candles and finds a pair with an annualized Sharpe ratio of 2.5 in that year.

  1. How many pairs were tried?
  2. If no pair had any real edge and their results were independent, about how high would the best Sharpe ratio be expected to be by luck alone? Use Bailey and colleagues' approximation, which gives about 2.73 for this number of tries with one year of data.
  3. What does that say about the 2.5, and what should the trader do before trading the pair?
Show the answers

Answer.

  1. 12 × 15 = 180 pairs.
  2. About 2.73. Because neighboring pairs trade alike, the effective number of independent tries is smaller, so the luck-only benchmark is somewhat lower, but it remains well above zero.
  3. A best result of 2.5 is about what luck alone would produce from this many tries, so it is not evidence of an edge. Before trading, the trader could test the chosen pair, once, on later data that played no part in the search; check whether neighboring pairs also do well; rerun with costs included and honest next-open fills; and paper trade it on new prices. They should also record that 180 pairs were tried, so anyone reading the result can judge it.