How do you backtest a trading strategy?
To backtest a trading strategy, you write down exact rules for entries, exits, stops and position size, apply those rules to historical price data, and record every trade the rules would have taken, including costs. You then measure the results (win rate, average win and loss, expectancy, profit factor and maximum drawdown) and check them on data you did not use to build the strategy. A backtest cannot prove a strategy will work, but it can quickly show you one that does not.
Step 1: Write rules that leave no room for judgement
If two people could read your rules and take different trades, the rules are not precise enough. Define:
- Market and timeframe: for example, EUR/USD on the 1-hour chart.
- Trading hours: any session filter, such as London and New York only.
- Setup conditions: what must be true before a trade is considered, such as price above the 200-period moving average.
- Entry trigger: the exact event that opens the trade, such as a close above the previous candle's high.
- Stop loss and target: where each goes and how it is measured. See stop loss and take profit for common methods.
- Trade management: any trailing, partial exits or time stops.
- Position size: usually a fixed percentage of the account risked per trade.
Strategies built from clear rules, such as breakout or mean reversion systems, are easier to test than ones based on discretion.
Step 2: Choose manual or automated testing
| Manual backtesting | Automated backtesting | |
|---|---|---|
| How it works | You scroll through past charts candle by candle and log each trade | You code the rules and software runs them over the data |
| Best for | Discretionary or pattern-based strategies | Fully rule-based, indicator-driven strategies |
| Speed | Slow, especially for large samples | Fast, thousands of trades in seconds |
| Main risk | Hindsight bias and inconsistent judgement | Overfitting and coding errors |
| Common tools | Chart replay features and a spreadsheet | TradingView Pine Script strategies, MetaTrader Strategy Tester, Python |
When testing manually, use a replay mode that hides future candles. Seeing what happened next makes it very easy to "find" setups that you would not have taken in real time. Our trading platforms comparison covers which platforms offer replay and strategy testing.
Step 3: Use enough data, and the right data
- Sample size: aim for at least 100 trades as a rough minimum, and more for strategies with low win rates. Twenty trades tells you almost nothing.
- Different conditions: include trending and ranging periods, high and low volatility, and ideally several years.
- Clean data: check for gaps or bad ticks, and for futures, understand how contract rollovers are handled in continuous charts.
- Survivorship bias: for stock strategies, a dataset that only includes companies listed today will exclude the ones that went bankrupt or were delisted, which flatters results.
Step 4: Record every trade, including costs
Log the date, direction, entry, stop, target, exit, result in R (multiples of your risk) and a screenshot if testing manually. Subtract realistic costs for spread, commission, slippage and overnight financing. Short-term strategies are especially sensitive to costs, because they are a large share of a small target.
Step 5: Measure the results
The key metrics are:
- Win rate: winning trades divided by total trades.
- Expectancy: (win rate x average win) - (loss rate x average loss), the average result per trade in R.
- Profit factor: gross profit divided by gross loss. Above 1 is profitable.
- Maximum drawdown: the largest peak-to-trough fall in the equity curve.
- Longest losing streak: useful for setting realistic expectations and risk per trade.
Worked example: reading a backtest
A strategy produces 100 trades: 42 winners averaging 1.8R and 58 losers at 1R each.
- Win rate: 42 / 100 = 42%
- Gross profit: 42 x 1.8R = 75.6R. Gross loss: 58 x 1R = 58R.
- Net result: 75.6R - 58R = 17.6R
- Expectancy: (0.42 x 1.8) - (0.58 x 1) = 0.756 - 0.58 = 0.176R per trade
- Profit factor: 75.6 / 58 = 1.30
Now add costs of 0.1R per trade. Each win becomes 1.7R and each loss 1.1R. Gross profit falls to 42 x 1.7 = 71.4R, gross loss rises to 58 x 1.1 = 63.8R, the net result drops to 7.6R and expectancy to 0.076R per trade. The profit factor falls to 71.4 / 63.8 = 1.12. The strategy is still positive, but the edge has more than halved, which shows why ignoring costs is one of the most common backtesting mistakes. At 1% risk per trade, 0.076R is about 0.076% of the account per trade on average. You can model growth from a given expectancy with the compounding calculator.
Step 6: Check for overfitting
Overfitting means tuning a strategy so closely to past data that it captures random noise rather than a real pattern. Warning signs include many parameters, very specific settings (a 17-period average that works but 15 and 20 do not), and results that collapse on new data. To guard against it:
- Split your data. Build the strategy on one period (in-sample) and test it unchanged on a later period (out-of-sample).
- Check that nearby parameter values give similar results.
- Use walk-forward testing, where you repeatedly optimise on one window and test on the next.
- Avoid look-ahead bias, where the rules accidentally use information that was not available at the time, such as a candle's close before it has closed.
Step 7: Forward test before going live
After a successful backtest, trade the strategy on a demo or very small account for a period of weeks or months. Forward testing reveals execution problems, real slippage and whether you can actually follow the rules under pressure, which is the theme of the academy lesson on backtesting and trading psychology. Compare live results with the backtest. If they differ a lot, find out why before increasing size.
Trading involves substantial risk of loss and is not suitable for every investor. Backtested and hypothetical results have limitations and do not guarantee future performance. Read our full risk disclaimer before trading.