Backtesting Essentials · Lesson 7
The best of many
Why the best of many tries is probably luck, and what to do about it.
Know the basics? Read the lesson notesHere is a trading strategy. It's a coin.
A trading strategy is just a rule that decides when to trade. This one is a coin: every flip is one trade. Heads, the trade wins 1R (+1R). Tails, it loses 1R (−1R). A fair coin lands heads half the time.
R is the amount you stand to lose on one trade. We count in R, so we never need a price. The account starts at 100R. Flip it ten times.
Flips 0 · account 100R
On average, what does one trade make?
Think about the next thousand flips, not the last ten. A strategy has an edge when it has a real reason to make money on average over time.
Now let it run for a year
Running a trading rule over a stretch of trades to see what it would have done is called a backtest. Here is your coin's backtest: the same coin, carried on from your ten flips to 250 trades, about a year if it traded once a day. Press play, then try a few other coins. Yours stays on the chart in a lighter line.
One coin-flip strategy, 250 trades
Account in R. Each step is one trade: up 1R on a win, down 1R on a loss.
Some finish well up, some well down. None of them can do anything but flip. Luck alone spreads results out, and over 250 trades it spreads them a lot.
A guess before we go big
We'll make 1,000 of these coin strategies and give each one 250 trades. Then we look at the best of them: the share of its trades it won, its win rate. A fair coin wins 50% on average. How high will the best one get?
How people tell luck from skill
Take one fair coin and flip it 250 times. Most of the time it wins somewhere near 125. Drag the marker to see how often a single coin wins at least that many.
One fair coin, 250 flips: how many wins?
The height of each bar is how likely that exact number of wins is.
At 139 wins a coin gets there only about 4 times in 100. So when one strategy, tested once, wins 139 or more of 250 trades, it has done something luck alone rarely does, and people take that as evidence of an edge.
People draw the line where a coin gets there less than 5 times in 100, and call it the 5% line. It is a convention, not a law of nature. A result past it is called statistically significant.
Keep one thing in mind. The line is fair for one strategy, tested once. Next: 1,000 of them.
Now 1,000 coins
None of these coins has an edge. How many of the 1,000 will land on the 5% line or past it? Set your guess, then drop them in.
1,000 coin strategies, stacked by wins out of 250
Each block is one strategy. The dotted outline is what luck predicts.
The line didn't fail. A single coin crosses it 4.4 times in 100 (that's the "less than 5" in its name), so about 44 of 1,000 coins should. We got 56, a little more: counts like this jump around from one drop to the next. The line was made for one test. We ran 1,000.
For the precise
Expected count on or past the line: 1,000 × 0.0438 = 43.8, with a standard deviation of √(1,000 × 0.0438 × 0.9562) ≈ 6.5; 56 is about 1.9 standard deviations above. The best coin: 155 wins, the worst: 100 (40%).What a search does
Building a strategy usually means trying versions: a different setting (say, how many days of prices the rule looks back on), an extra condition before it trades, another market, hourly data instead of daily. Each version you try is one more coin in the pile. Then you keep the one with the best backtest. Slide from 1 version to 1,000 and watch the best one.
Try N versions, keep the best
Top: every version you tried (grey) and the best (colour), in R: a win adds 1R and a loss takes 1R from the 100R start, so 160R means 155 wins of 250. Bottom: the win rate the best of N coins reaches by luck alone, on average; each step along the bottom is ten times more versions. The more tries, the higher it goes.
Nothing about the coins changed. Only how many you looked at. And "close to certain" has a reason: each try misses the line about 96 times in 100, and missing 1,000 times in a row almost never happens.
So what did you find?
Now leave our coins and picture your own search on a real idea. You tried about 1,000 versions of your rule (each one a different setting, an extra condition before trading, or another market, so each one is one more coin in the pile). Your best version won 60% of its 250 trades.
Pick one to trade
Here are the ten best backtests out of the 1,000. Choose the one you would trade. Then it gets 250 new trades it has never seen, flipped after the choosing was done. That's what happens when you start trading a winner, or test it on data you kept aside and never used to choose it (testing on data kept aside like this is called a held-out test).
The ten best backtests, then 250 new trades
Left: the backtest they were chosen on. Shaded, right: new trades.
The ten averaged +44.2R in their backtests and −5.4R on the new trades. 4 of the 10 made money, about what any 10 coins would do. The best of many gets picked partly because of luck, and luck doesn't repeat.
All 1,000 at once
Each dot is one coin: how much it made in its backtest (across) and in its 250 new trades (up). Does a better backtest go with better new trades?
The chance that at least one no-edge try passes
A no-edge result that clears the line anyway is a false positive: the test flags it as more than luck, and it is luck. The 56 coins on the line or past it in your drop were 56 false positives. Running many tests and keeping whichever passes is called multiple testing (in trading research, data snooping).
If one no-edge try clears the line with chance p, it misses with chance 1 − p. Separate tries don't affect each other, so the chance that all n miss is 1 − p multiplied by itself n times, written (1 − p)n with a small raised n. The chance that at least one clears is 1 minus that.
The correction is to raise the line. Pick the most you accept being fooled across the whole search, α (the Greek letter alpha, usually 5 in 100), and give each of the n tests a line a no-edge try clears only α ÷ n of the time. Then n chances of α ÷ n add up to α, and the chance that at least one clears can't be more than that sum (two tries can both pass and get counted twice, so the real chance is a bit smaller: the sum is a safe upper limit). That is why the formula says ≈. This is the Bonferroni correction.
- p
- the chance one no-edge try clears the line (0.0438 for 139 wins of 250)
- n
- the number of tries: every version you tested, kept or dropped
- (1 − p)n
- the chance all n miss
- α
- the most you accept being fooled across the whole search (usually 0.05)
- peach
- the chance a no-edge try clears the raised line
The 139 line: p = 0.0438, so one coin misses with chance 1 − 0.0438 = 0.9562.
10 versions: 0.956210 = 0.639, so at least one crosses with chance 1 − 0.639 = 0.361, about 36 times in 100.
1,000 versions: 0.95621000 has 19 zeros after the decimal point, so at least one crossing is all but certain (the "over 99.9%" from "What a search does").
Raised line for 20 versions. It starts from the target α = 0.05, not from the 139 line's p = 0.0438: 0.05 ÷ 20 = 0.0025, or 0.25 in 100. That line is 148 wins of 250, and the best of 20 coins gets there about 4 times in 100. To check it yourself, press Back to "How people tell luck from skill" and drag the marker until "A single coin gets there" drops below 0.25% of the time: that happens at 148.
- no try has an edge (the formula counts lucky passes);
- every try faces a line it clears with the same chance p;
- the tries are separate: each coin ignores the others;
- n counts every try, including the ones you dropped.
The tries are related: a 20-day and a 21-day setting trade almost alike, so they add less luck than two coins. Then the formula overstates the chance and α ÷ n is stricter than needed. Lesson B12 (The Sharpe ratio, and its error bar) counts how far a picked backtest's error bar falls short; a correction built for real searches comes in lesson C5 (Many tests at once).
A looser line, one a no-edge try clears 10 times in 100 (p = 0.1), and 5 no-edge versions. What's the chance at least one clears it? Type a percentage.
What this means for your backtest
Three habits follow from everything above. Turn them over one at a time.
- Count every version you try.
Every setting, extra condition and market you looked at is another coin in the pile, including the ones you dropped and forgot. Write them down as you go.
- Raise the bar to match the search.
The bar is the line a result must clear. For one test, it is 139 wins out of 250. But the best of 1,000 coins reaches 139 almost every time, so for 1,000 versions move the bar up to 157 wins of 250: even the best of 1,000 coins gets there only about 3 times in 100. (To check it yourself, drag the marker on "How people tell luck from skill" until "A single coin gets there" drops below 0.005% of the time: 5% ÷ 1,000.) The luckiest of our 1,000 coins, at 155, no longer passes.
For the precise
The line for N versions is the smallest k with P(X ≥ k) < 0.05 / N, X ~ binomial(250, ½). For Sharpe ratios, the Deflated Sharpe Ratio (Bailey and López de Prado, 2014) raises the bar with the number of trials in a similar spirit. - Keep some data you never touch, and use it once.
A held-out test runs your final version on a period that played no part in choosing it, like the new trades you just ran. It works only once: look at the result, tweak the rule, test again, and that period has become one more coin in the pile.
One last check
Why does a held-out test count only once?
The course seller's strategy
Before you pay, is 58% evidence of an edge?
Hint: press Back to the "What a search does" card and set its slider to 200 versions. The number under "Best of N coins, on average" is where the best of 200 coins lands.
Thirty settings, no edge
What's the chance that at least one of them lands on the 5% line or past it? Type a percentage.
Hint: each coin ignores the others. One coin misses the line 95.6 times in 100. All 30 missing is 0.956 × 0.956 × … thirty times. On a phone calculator: type 0.956, press the x^y key, type 30. About 0.26. Then: at least one crosses = 1 minus the chance that all 30 miss.
Forty versions, one bar
Using the Bonferroni correction, each test's line should be one a no-edge try clears how many times in 100? Type a number.
Your own research
What do you do next?
Lesson done
Try many versions, keep the best, and part of its result is luck. Count the versions, raise the bar to match, and let a period you never touched have the last word.
Key ideas
- 5% line
- a result a no-edge try reaches less than 5 times in 100; past it is called statistically significant. Fair for one test.
- False positive
- a no-edge result that clears the line anyway
- Multiple testing
- trying many versions and keeping the best (also: data snooping). Each try is one more chance of a false positive.
- Bonferroni correction
- to be fooled at most α of the time across the whole search (usually 0.05), raise the line so each of n tests clears it by luck only α ÷ n of the time
- Held-out test
- data kept aside while choosing, run once on the final version
Formulas
chance at least one of n no-edge tries clears = 1 − (1 − p)n
raised line, for each test: peach ≈ α ÷ n