Testing Popular Ideas · Lesson 1
How we test an idea
Five checks to run before you believe that a trading idea worked on past prices. Every later lesson in this course uses them.
Know the basics? Read the lesson notesWrite the exact rule
Here is a trading idea you might read online: “buy when momentum turns”. Momentum is how much the price has changed over the last few days, in percent. The 10-day change compares today's price with the price 10 days ago.
To test an idea, a computer runs it over past prices and lists what it would have bought and sold; each buy with its later sale is one trade. That run is a backtest. A computer needs exact instructions, and this sentence fits more than one set. Here are three:
- Rule A. Buy when the 10-day change turns from negative to positive. Sell when it is no longer positive.
- Rule B. Buy when the 10-day change (the change, not the price) has been falling and then starts to rise. Sell 5 days later.
- Rule C. Buy when the 20-day change turns from negative to positive. Sell 20 days later.
Below is one year of a simulated market: prices made up by a computer, not a real market. Switch between the rules.
One simulated year (2016), and the trades each rule made
Line: the price at the end of each day. Dot: a buy. Shaded: the days from that buy to its sale.
Rule A traded 24 times that year, Rule B 40 times and Rule C 6 times. One sentence gave three different sets of trades.
An exact rule says when to buy, when to sell, and how much money goes into each trade. The test: two people who code it get the same trades.
For the precise
The details these rules fix: a rule decides at the end of a day and trades at the first price of the next day, because nobody can trade at a price they have only just seen. Every trade puts in the same 100. A rule holds one position at a time.Take off the costs
Take Rule B over eight simulated years, 2015 to 2022. Every trade puts in the same 100, one trade at a time, and we add up what the trades made and lost. They add up to +95: a result of +95%, or 95 points (a point is 1 on the 100).
That is before costs. Every trade pays a cost: the broker's fee, plus a slightly worse price than the chart shows, both when buying and when selling. Here one trade's cost is about 0.135% of the 100, so 0.135 points.
Rule B's account on the simulated market, 2015–2022
It starts at 100 and adds each trade's result. Where the line is flat, the rule held nothing.
Rule B made 298 trades. How many of its 95 points do you think the costs took?
Compare with holding
+55% after costs sounds good. Compared with what, though? The simplest alternative needs no rule: buy on the first day of 2015, sell on the last day of 2022, and pay the cost once. Call it just holding. It is the benchmark: a simple alternative that a rule is measured against.
So the number this lesson reads is the rule's result after costs, against just holding over the same days.
Rule B against just holding, simulated, 2015–2022
Both after costs, both starting at 100. Holding's line appears once you guess.
What do you think just holding made over those eight years?
Count the versions you try
Rule B lost to holding. The tempting next move is to change the settings and try again. The number of days it looks back can be 5, 10, 20, 30, 40, 50, 60, 90, 120 or 200: 10 choices. “Turns” can mean turning positive, or rising after a fall: 2 choices. The sale can come when the change turns back, after 5 days or after 20 days: 3 choices.
That makes 10 × 2 × 3 = 60 versions: the same idea with different settings. Rules A, B and C are the first three on the slider. Slide to try all 60.
60 versions of one idea on the simulated market, 2015–2022
Each dot: one version's result after costs. Dashed line: just holding, +85%. A dot above the line beat holding.
2 of the 60 beat holding, with +88% and +87%. The best one buys when the 120-day change rises after a fall and sells 5 days later. It made +88% after costs: 3 points ahead of holding's +85%.
Shown on its own, that version looks like a find.
For the precise
Exact results. The best version: 267 trades, +123.8% gross, 35.9 points of costs, +87.9% net, 3.1 points ahead of holding's +84.8%. Rule B: +94.8% gross, 40.1 points of costs, +54.7% net, 30.1 points behind. Before costs, 6 of the 60 beat holding's +85.0%; after costs, 2. The cost of one trade differs a little between versions, from 0.133% to 0.143%.What put the best one ahead?
Here is a fact about this simulated market. We built it so that past prices say nothing about which way the next move goes: it rises slowly on average, and the rest of each day's move is chance. So no rule that reads past prices can tell the next move here. We say such a rule has no skill.
What put the best version 3 points ahead of holding?
Keep some years held out, and use them once
How can you tell luck from something real? Luck rarely repeats. So before running anything, we set two more years aside: 2023 and 2024. No version has seen them. Years kept aside like this are held out.
Now the best of the 60 runs on them, once. Same comparison: its result after costs against just holding. Guess first, then run it.
The best of 60 on the held-out years, simulated, 2023–2024
Both after costs, both starting at 100.
You guessed ahead. The 3-point lead came from luck on 2015–2022, and luck doesn't carry over to new years.
You guessed about level. A rule with no skill should not beat holding, and it also pays a cost on every trade, so behind is the likelier place.
You guessed behind, and it was.
On the held-out years the rule lost 26% over 76 trades, while just holding ended almost where it started, at −0.2%. The lead did not carry over: the search picked this version for its luck on 2015–2022, and luck doesn't repeat. This time luck ran against it (−16% before costs), and the cost of 76 trades took about 10 points more.
One held-out run is a check, not proof, either way: luck can pass a rule with nothing in it and fail one with something. But a version picked for its luck is the kind you expect to fail, and this one did.
A held-out test counts once. If you change the rule and run those years again, they have helped choose the rule, and they are no longer held out (lesson B10, The held-out test).
For the precise
Exact: −26.3% after costs over 76 trades, −16.2% before costs; holding −0.2%. On 2023–2024, 17 of the 60 versions beat holding, and the best of 2015–2022 ranked 55th of 60.Net result, and the lead over holding
Two subtractions turn a backtest's headline number into the one that counts. First the costs: the number of trades times the cost of one. Then just holding, the benchmark.
- gross
- the trades' results added up, before costs, in points
- n
- the number of trades
- c
- the cost of one trade, in points
- net
- the result after costs, in points
- hold
- what just holding made over the same days, after its one cost
- lead
- points ahead of holding; a minus sign means behind
net = 95 − 298 × 0.135 ≈ 95 − 40 = 55.
lead = 55 − 85 = −30: 30 points behind holding.
- every trade puts in the same amount, and results are added up;
- every trade pays about the same cost;
- holding covers the same days, after costs.
The amount traded grows with the account: results then multiply instead of adding (lesson A14, Compounding and the asymmetry of losses).
A rule made 70 points gross over 150 trades. One trade costs 0.2 points. Just holding made 25 points over the same days. What is the rule's lead over holding? Type a number.
More versions, a stricter pass mark
Trying many versions and keeping the best is called multiple testing. Each extra version gives luck one more try.
First decide how often you accept being fooled by luck: say 5 times in 100. Call that α (alpha). For one rule tested alone, the pass mark is then a lead over holding so large that a rule with no skill reaches it in only 5 of every 100 tests. Put 60 no-skill versions against that same mark and expect 60 × 5 ÷ 100 = 3 lucky passes. The fix: give each version a stricter mark, a larger lead that a no-skill version reaches only α ÷ n of the time. Then a lucky pass anywhere among the n versions happens at most n × α ÷ n = α of the time: 5 in 100 for the whole search. This is the Bonferroni correction.
- chance per version
- how often a no-skill version may reach the mark set for it
- α
- how often you accept a lucky pass anywhere in the whole search, often 5 in 100
- n
- the number of versions tried, dropped ones included
α = 5 in 100 and n = 60: 5 ÷ 60 ≈ 0.083 in 100 for each version, which is 1 time in 1,200.
You have already seen how often luck gets as far as the best version did: on this market, where no version has skill, the best of 60 led holding by 3 points. A mark that luck may reach only 1 time in 1,200 has to sit much further out. How far, in points, is lesson B6 (Hypothesis tests).
- n counts every version you looked at, kept or dropped;
- the versions are separate tries.
Versions are close relatives: a 120-day and a 200-day look-back trade on many of the same days, so their results move together and α ÷ n is stricter than needed (lesson C5, Many tests at once).
For the precise
How to find how often luck reaches a given result is lesson B6 (Hypothesis tests); searches are lesson B7 (The best of many). The chance that at least one of n separate no-skill versions passes a mark that each passes with chance p is 1 − (1 − p)n. For p = 0.05 and n = 60: 0.9560 = 0.046, and 1 − 0.046 = 0.954, about 95 in 100.Someone tried 25 versions and accepts α = 5 in 100 for the whole search. How often may luck reach the pass mark for each version?
The five checks
Every later lesson in this course puts a popular idea through these five checks, in this order.
- Write the exact rule.
Two people who code it must get the same trades.
- Take off the costs.
net = gross − trades × cost of one trade.
- Compare with just holding.
Same days, both after costs. The lead is the result that counts.
- Count the versions you tried.
Dropped ones too. Report the best as “the best of n”, and give it a pass mark that luck reaches n times less often.
- Keep some years held out, and use them once.
Set them aside before you start, and run the final rule on them once.
Same idea, different results
What is the most likely reason?
A friend's backtest
What is the rule's lead over holding? Type a number.
Hint: net = gross − trades × cost of one trade. Then lead = net − hold.
A second look at the held-out years
Does the second run count as a held-out test?
Lesson done
You can now put a trading idea through five checks: exact rule, costs, just holding, versions counted, held-out years used once.
Key ideas
- Backtest
- running a rule over past prices to see its trades
- Exact rule
- one that two people would code into the same trades
- Gross, net
- a result before costs; after costs
- Just holding
- buy on the first day, sell on the last: the benchmark a rule is measured against
- Version
- the same idea with different settings; count every one
- Multiple testing
- trying many versions and keeping the best; the Bonferroni correction sets a pass mark that luck reaches n times less often
- Held-out years
- years set aside before you start, run once on the final rule
Formulas
net = gross − n × c · lead = net − hold
chance per version = α ÷ n