CustomTradingPlatform .com
Log in Get started free
Home / Guides & research / Backtesting

Guide · Backtesting

How to Backtest a Trading Strategy

The short answer

Write the rule so a computer cannot misread it, hold back data you never look at, and expect most versions to fail.

What a backtest is for

A backtest does not tell you whether a strategy will work. It tells you whether a strategy would have worked, on one sample, under assumptions you chose — and its real value is in making those assumptions explicit enough that someone can disagree with them.

Treated that way it is the most useful tool available. Treated as a forecast it is worse than nothing, because a bad backtest produces confidence rather than doubt.

The four ways it goes wrong

  1. Look-ahead. The rule uses information that was not available at the time it fires. This is the most common and the hardest to see, because it usually enters through a helper — an indicator that peeks at the bar close, a swing point identified with hindsight, a data feed that back-fills.
  2. Survivorship. The instrument list contains only what still exists. Delisted stocks and discontinued pairs are missing, and they are missing precisely because they went badly.
  3. Overfitting. The parameters were chosen by trying many and keeping the best. The result then describes the sample, not the market.
  4. Multiple testing. You tested a hundred rules and reported the one that worked. At p<0.05, five of a hundred look significant on noise alone.

The still-forming bar, and why it is worth naming

A subtle case of look-ahead: including the bar currently in progress. Its close is not yet known, so any rule that reads it is reading the future. On a daily series this is a whole session of information.

This is not hypothetical here. The measured run this site was preparing to publish included the forming bar in its candlestick rows, and the run was withdrawn rather than published with a caveat. The definitions, thresholds and detection rules were unaffected — they describe what is tested, not what was measured — but the hit rates went, and they stay gone until the re-run completes.

Worth knowing

The cost of catching this late is one withdrawn study. The cost of not catching it is a permanent, quotable, wrong number. That trade is not close.

What a defensible backtest reports

  1. The sample size, not just the percentage. 58% of what?
  2. A benchmark on the same data. 58% means nothing until you know an arbitrary entry scored 51%.
  3. A confidence interval, so "it works" becomes "here is the range the evidence supports".
  4. The correction applied, if more than one rule was tested. Say which, or say none.
  5. The exact rule, precisely enough that someone else can run it and get your number.

The last one is the test of the other four. A rule you can publish as an executable condition is a rule whose result can be checked; anything less is an assertion. Every formation on this site publishes its thresholds for that reason — see the candlestick patterns index.

Holding data back, and how much

The single most effective defence against overfitting is data you never looked at. It works because it cannot be gamed by intention — once you have seen a period, you cannot un-see it when choosing parameters.

  1. Split before you start. Typically 70% in-sample for development, 30% out-of-sample held back entirely.
  2. Develop on the first part only. Every parameter choice, every rule variation, every abandoned idea.
  3. Test once on the held-back part. Once. Testing twice and adjusting in between converts it into in-sample data, and the protection is gone.

Where this goes wrong

If the out-of-sample result is much worse than the in-sample one, that is the honest finding. The temptation at that point is to adjust and re-test, which destroys the only unbiased estimate you had.

Walk-forward, for strategies that need re-fitting

A single split assumes one parameter set holds for the whole period. Walk-forward tests that assumption instead of making it: fit on a window, trade the next window, roll forward, repeat.

fit on 2015-2017 → trade 2018
fit on 2016-2018 → trade 2019
fit on 2017-2019 → trade 2020
...
the result is the concatenation of the traded windows only

It is more work and it answers a more useful question: whether the strategy survives being re-fitted the way you would actually have to re-fit it. A strategy whose optimal parameters swing wildly between windows is telling you something a single backtest would have hidden.

How many ideas should fail

Most of them. A research process that produces a working strategy from most attempts is not finding edges, it is fitting noise — and the rate at which ideas survive is itself a diagnostic.

If nine in ten of your tested rules fail, the process is probably honest. If most pass, the tests are too loose, the sample is being reused, or the parameters are being chosen after seeing results.

Worth knowing

Keeping a record of the ideas that failed is what makes the correction possible later. A family you cannot count is a family you cannot correct across.

What this depends on

MethodThe rule is stated precisely enough to run, and the same rule produces every figure quoted.
MeasuredWhere a number comes from our own testing it carries its sample size and its data vintage.
LimitsCosts, slippage and non-USD accounts change the arithmetic. See methodology.