Home / Backtesting

Backtesting

How to Backtest a Trading Strategy

A backtest applies a written rule to past prices: 100 trades leave the win rate uncertain by about 10 points, and costs of 0.2% per round trip ($2 on $1,000) must be included.

Direct answer

Write the entry and exit as exact rules, run them over historical data with realistic costs, and report the result alongside the sample size and what could bias it. A backtest is only as trustworthy as its stated limitations.

The five steps

1 · Define

State entry, exit, stop and position size as conditions a machine could check, not as a feeling.

2 · Run

Apply the rule across historical bars with spread, commission and slippage included, not ignored.

3 · Measure

Report win rate, expectancy, drawdown and sample size together. One number alone misleads.

4 · Stress

Test out of sample and across regimes. A rule fit to one period is not yet a strategy.

5 · Disclose

Publish the limitations with the result, especially when the result is negative.

What makes a backtest lie

Look-ahead bias

Using information that would not have been available at the time of the trade.

Survivorship bias

Testing only on instruments that still exist today, which excludes every failure.

Overfitting

Tuning a rule's parameters until it fits the past perfectly and the future not at all.

Ignored costs

Spread, commission and slippage that would have erased the edge in live trading.

Small samples

A result from a few dozen trades is closer to noise than to evidence.

Multiple testing

Trying many rules and reporting only the one that happened to work.

This is the same discipline behind the candlestick work: 125 formations are written as executable rules with a stated trend filter and published thresholds, every rule is checked for look-ahead, and the engine is run against a browser mirror until the two agree on every detection. When a measured run is found to have been computed on a different version of the engine, it is set aside and redone rather than published with a caveat - which is what happened to the first one here. Read that study →

Create your account to see the custom crypto strategies and indicators.

Crypto strategies, crypto indicators, breakout signals and custom bots, in one account.

Create your account

How the backtesting on this site is actually run

The five steps above are the general discipline; the guide covers the failure modes in detail. This is what the engine behind the formation pages does specifically, because a method described in prose is not checkable and one described exactly is.

The choices made once and applied to every formation
DecisionWhat was chosen
EntryThe open of the bar after the formation completes - the earliest price actually available
ExitThe close of the Nth bar. No stop, no target: the raw forward move, not a system
DirectionReturns signed to the formation’s own claim, so a bearish rule that falls scores positive
BenchmarkAn arbitrary entry on the same instrument, timeframe and period, sample size matched
CorrectionBenjamini-Hochberg across the whole family, not per formation
SampleDe-overlapped, because consecutive detections share forward windows

Two implementations, checked against each other

A result is only reproducible if a second implementation agrees. The Python engine and the browser mirror were run against each other across D1, H4 and H1: 81,301 detections, 0 differing. Every rule was checked for look-ahead, and the swing primitive carries a negative test - that a pivot is invisible one bar before it confirms.

A worked example of a backtest going wrong

The most useful thing on this page is not the method, it is what happened when the method caught something.

A completed run measuring all 107 candlestick formations was ready to publish. It was set aside instead: the candlestick rows had been computed with the still-forming bar included, and the engine fingerprint had moved when eighteen chart formations were added. The formation definitions, thresholds and invalidation conditions were unaffected - those describe what is tested, not what was measured - so they remain published. The hit rates did not.

Where this goes wrong

The cost of catching a forming-bar leak late is one study thrown away. The cost of not catching it is a wrong number that gets quoted for years. A site that reports a figure for every formation regardless of whether it survived scrutiny is easier to read and worth less.

What a published result here will carry

When the new run completes, every figure appears with its sample size, a benchmark measured on the same bars, a confidence interval, the correction applied across the family of 125, and the rule that produced it - precisely enough that someone else can run it and get the same number. Anything that cannot carry all six is not published.

What costs do to a backtest

Spread, commission and slippage are paid on every round trip, so they matter most for strategies that trade often and aim for small moves. A backtest that leaves them out is measuring a market with no friction.

Illustrative arithmetic: the same $1,000 trade, gross move of 0.5%, with a cost of 0.2% per round trip
ItemAmount
Gross result of the trade$5.00
Cost of 0.2% per round trip$2.00
Net result$3.00
Share of the gross move lost to costs40%

At that cost level a strategy needs a gross edge of more than 0.2% per trade before it is positive at all, and the margin grows thinner as the move per trade shrinks. This is why the engine reports the measured spread cost beside every result.

Five questions to ask of any backtest, including this one

  1. How many trades? Around 100 trades pins a win rate to about 10 points either side. A result built on 20 trades says very little.
  2. Was the future visible? A signal that uses the close of a bar cannot be traded at that bar’s open. Look-ahead is the commonest error and it always flatters the result.
  3. Was the best version chosen after the fact? Trying 50 parameter sets and publishing the best one is not a test of the rule.
  4. What happened out of sample? The most recent period should be held back and tested once. If it disagrees with the rest, believe the held-back period.
  5. Were costs included, and are they realistic? The cost of a round trip belongs in every line of the table.

Where this goes wrong

A backtest describes what a rule did on past prices. It cannot say what the rule will do next, and nothing on this page should be read as saying so.

Common questions

What is a backtest?

Running a written rule on past prices to see what it would have done, with fees and slippage included. It is hypothetical, because no real money was traded.

How many trades does a backtest need?

At least 100. With 30 trades a 50% win rate is uncertain by about 18 points; with 100 trades, by about 10.

Does a good backtest mean the strategy will work?

No. It shows the rule fitted the past. A result on data the rule never saw, then paper trading, is stronger evidence, and neither guarantees future results.