How to Backtest a Trading Strategy, Step by Step
How to backtest a trading strategy honestly: fixed rules, 100+ trades, out-of-sample data, real costs, results in R and a forward test on demo.

A strategy you have never tested is a guess with a name. Testing it on past charts is cheap, and done honestly, it rejects most bad ideas before they cost you money.
Quick answer: To backtest a trading strategy, write exact rules first, apply them to past charts trade by trade, and record every result in R, after costs. Collect at least 100 trades, keep a later stretch of data aside as an out-of-sample check, measure expectancy and drawdown, then forward test on a demo before risking real money.
What a backtest can and cannot tell you
A backtest applies a fixed set of trading rules to historical prices and records what would have happened. If the rules say “buy when price closes above the 50-period moving average after a pullback, stop below the last swing low, target twice the risk”, the backtest finds every place on the chart where that happened, in order, and logs each result as a win, a loss or a scratch.
Its main job is to reject ideas cheaply. A method that loses money on five years of past data rarely starts winning when you trade it live, and finding that out on old charts costs you a few evenings rather than part of your account. Its second job is to give you numbers to compare against later: a typical win rate, an average win and loss, an expectancy per trade, and the size of the losing runs you should expect.
What a backtest cannot do is prove a strategy will make money. It measures the past, with perfect hindsight about which data you used, and it leaves out things that only happen in real time: your hesitation, the trade you skip because you are tired, the fill that slips three pips on a news spike. That is why a backtest is the first of several stages, not the last.
The Academy lesson on testing a strategy explains the three stages after the backtest in more detail. This guide concentrates on doing the backtest itself properly, because almost every error that makes a strategy look better than it is gets made here.
Step 1: write the rules before you look at results
You cannot backtest a feeling. “Buy strong pullbacks in an uptrend” will produce different trades every time you test it, depending on your mood and on what you can see happened next. Before you scroll back a single candle, write the rules down so precisely that another person, or a computer, would take the same trades.
A testable strategy answers seven questions:
| Question | Vague | Testable |
|---|---|---|
| Market and timeframe | “Majors” | EUR/USD and GBP/USD, 4-hour chart |
| Trend filter | “Uptrend” | Price above the 50-period moving average, and the average rising over 10 candles |
| Setup | “A pullback” | Price touches or crosses the 20-period moving average |
| Trigger | “When it turns” | A 4-hour candle closes back above the 20-period average |
| Stop | “Below support” | 3 pips below the lowest low of the pullback |
| Exit | “Take profit when it looks done” | Target at 2R; or close at the end of the fifth day |
| Size and limits | “Not too big” | 1% risk; no new trade within 2 hours of a high-impact release |
The right-hand column looks fussy, and it should. Every vague word in the left-hand column is a place where hindsight can creep in. Writing the rules first is also the only protection against the most common way people fool themselves: adjusting the rules after each losing trade until the results look good. Our guide to writing a trading plan and the Academy lesson on choosing and writing your own strategy both include templates.
Before you start the backtest
0 of 8
Manual vs automated backtests
There are two ways to run the test, and both are legitimate. Which one fits depends on whether your rules can be written as code and how much of your method relies on judgement.
Manual backtesting
A manual test uses the chart you already trade on. Most platforms have a replay or bar-by-bar mode that hides the candles to the right; if yours does not, scroll back, cover the right side of the screen, and move forward one candle at a time. Each time the rules trigger, note the date, pair, entry, stop, target and result in a spreadsheet or the log further down this page.
Manual testing is slow. A hundred trades on a 4-hour strategy can take 10 to 20 hours. In exchange, it tests the method you will actually trade, including the judgement calls, and it trains your eye to see the setup. Its weakness is hindsight. You know roughly what EUR/USD did in 2022, and it is easy to “not notice” a setup that you vaguely remember failing. Hiding the future and logging every trigger, even the ones that look ugly, is the discipline that keeps a manual test honest.
Automated backtesting
An automated test runs your rules as a program, usually in a platform’s strategy tester. On MetaTrader the program is an expert advisor; other platforms use their own scripting languages. It can process years of data in minutes, and it never forgets a trade.
Speed creates its own trap. When a test takes a minute, it is tempting to run hundreds of versions with slightly different settings and keep the one that looks best. That is curve fitting, covered below. Automated tests also depend on the quality of the price data and on how the tester assumes orders are filled. A tester that fills every stop exactly at the stop price, at a fixed spread, on data with gaps, will flatter any strategy. Our guide to the forex VPS covers what changes when an automated strategy goes live, and the Academy lesson on expert advisors explains what a robot’s backtest can and cannot show.
How many trades a trading strategy backtest needs
Thirty trades feels like a lot of work, and it tells you surprisingly little. The reason is luck. Every set of results mixes the strategy’s real edge with random variation, and over a few dozen trades the random part is large.
Take a strategy that wins half its trades in the test, with winners worth 2R and losers −1R. Its expectancy in the test is 0.5 × 2 − 0.5 × 1 = +0.5R per trade, which would be excellent. But how sure can you be that the true win rate is near 50%? A standard statistical rule of thumb gives a 95% range of about ±1.96 × √(0.5 × 0.5 ÷ n), where n is the number of trades.
Turn those ranges into expectancy and the problem becomes plain. With 2R winners, expectancy is three times the win rate minus one. After 30 trades, the true expectancy could be anywhere from about −0.04R (a losing method) to +1.04R (an exceptional one). After 100 trades the range narrows to about +0.2R to +0.8R, and after 300 trades to about +0.33R to +0.67R. Four times the trades roughly halves the width of the range.
That is why we suggest at least 100 trades before you draw any conclusion, and more if the strategy’s edge is small. A method with an expectancy nearer +0.1R needs several hundred trades before the range clears zero. Our guide to trading expectancy works through more examples.
The simulator below makes the same point visually. Every run uses identical rules with a real edge; only the luck changes. Some runs of 100 trades end well up, others spend most of the time below the start.
Each run uses the same settings with different luck. Run it several times: the shape changes, the drawdowns don't go away.
Getting enough trades
If your strategy trades twice a month on the daily chart, 100 trades means four years of history per pair. Testing on two or three similar pairs helps, as long as the rules are identical on each. Dropping to a lower timeframe to get more trades changes the strategy, because spreads become a larger share of each trade, so treat it as a separate test.
In-sample and out-of-sample data
Even with 100 trades, a backtest can mislead you if you tuned the rules on the same data you are judging them on. The fix is to split your history before you start.
- The in-sample period is where you build and adjust the rules. Change settings, try filters, look at the losers.
- The out-of-sample period is a later stretch you leave untouched. When the rules are final, you run them on it once and accept the result.
A common split is the first 70% to 80% of the history in-sample and the last 20% to 30% out-of-sample. The numbers matter less than the rule that you look at the out-of-sample data once. If you run it, dislike the result, change a setting and run it again, the out-of-sample data has become in-sample data, and you no longer have an independent check.
Two more tests catch curve fitting early. First, check neighbouring settings. If a 20-period average gives +0.3R per trade, an 18 or 22-period one should give something similar. If the result collapses when you move a setting by one or two, the “edge” was probably a coincidence in the data. Second, look at different market conditions. A trend-following rule tested only on a strongly trending year will look far better than it will in a year of ranges. Make sure your history includes both, and look at the results for each separately.
Costs and slippage: where many backtests fail
A backtest that ignores costs measures a strategy nobody can trade. Every real trade pays the spread, often a commission, sometimes overnight swap, and in fast markets some slippage. These costs are fixed in pips, so they hurt short-term strategies with small stops far more than slower ones.
The cleanest way to include them is in R. Divide the total cost per trade in pips by the stop in pips, and subtract the result from every trade.
Use realistic numbers rather than the tightest spread your broker advertises. Spreads are wider in the quiet hours after the New York close, around rollover and during major releases. If your strategy often enters at those times, use the spread you would actually get then. Add a pip or so of slippage to stop exits during news, since stops fill at the next available price. Our guides to forex trading costs and slippage show how to estimate both.
Recording results in R: expectancy and drawdown
Record every trade as a multiple of the amount risked, called R. A full stop is −1R. A winner that reaches a target twice as far as the stop is +2R. A trade closed early halfway to target is +1R. R lets you compare trades with different stop sizes, on different pairs and account sizes, and it keeps the test about the rules rather than about money. The lesson on reward-to-risk covers it from the start.
From the column of R results, four numbers tell you most of what you need:
- Expectancy: total R ÷ number of trades. The same figure comes from win rate × average win − loss rate × average loss.
- Win rate and average win: together they show how the edge is built. A 35% win rate is fine if the average win is near 3R.
- Maximum drawdown: the largest fall in your running total of R, from a peak to the lowest point before a new peak.
- Longest losing streak: the most losses in a row. It tells you what you must be able to sit through.
Notice that the curve in the figure has a real edge and still spends nine trades going down. When you trade the strategy live, those are the weeks you will be tempted to abandon it. A backtest that shows you this in advance is doing its job. Our guide to drawdown in trading explains how drawdown in R turns into a fall in your balance, and why recovering takes a larger percentage gain than the loss.
Log your own results in the backtest log below. It calculates the same statistics and also shows a range for the true expectancy, which is the honest answer to “is my sample big enough yet?”
Backtest log
Plausible range of the true expectancy
Enter each backtest trade in R, after costs. The plausible range shows how far the true expectancy could be from what your sample shows; while the range still crosses zero, the test has not proved an edge.
Forward testing on a demo account
When the backtest passes, trade the same rules in real time on a demo account, using the same risk per trade you plan to use live. This is a forward test. It checks three things a backtest cannot: real spreads and fills at the moment you trade, whether the setups appear at times you can actually act on, and whether you follow the rules when you cannot see the right side of the chart.
Run the forward test for a fixed number of trades, decided in advance; 30 to 50 is a reasonable first stage for most swing strategies, followed by more if the numbers are borderline. Then compare it with the backtest. The question is not whether the forward test matched the backtest exactly; with so few trades it will not. The question is whether the forward results sit inside the range the backtest suggested, and whether you broke any rules. A forward test at +0.1R with every rule followed is more encouraging than one at +0.4R built on three trades you were not supposed to take.
The Academy lesson on demo with real rules sets this up as a 30-trade challenge, and our guide to forex demo accounts explains how demo fills can differ from live ones. Keep recording in R in your trading journal, so the forward test and the backtest use the same units.
Treat results you see elsewhere with the same caution. In the United States, the CFTC requires trading advisers who show simulated results to warn that hypothetical performance has limitations, because those results are produced with the benefit of hindsight and without real money at risk. Your own backtest has exactly the same limitations. Trading with leverage can lose money quickly, and a good backtest reduces that risk without removing it.
Common backtesting mistakes
Changing the rules during the test
Every change creates a new strategy and resets your sample to zero. Note ideas for changes in a separate list, finish the test with the original rules, then test the new version from the start on fresh data.
Letting hindsight pick the trades
In a manual test, skipping a setup because you “would not have taken that one” is the quickest way to inflate the results. Hide the future candles and log every trigger the rules give you.
Testing only the good years
A strategy tested on one strong trend tells you how it behaves in strong trends. Include ranging and volatile periods, and check the results for each.
Leaving out costs
A small edge can disappear entirely once spread, commission and slippage are included, especially with stops under 15 pips. Subtract costs in R from every trade.
Calling 30 trades a result
Thirty trades is a start. Collect at least 100 before you judge the strategy, and look at the range of plausible expectancy, not just the average.
Frequently asked questions
How many trades do you need to backtest a strategy?
We suggest at least 100 trades before drawing any conclusion, and several hundred if the expected edge is small. With 30 trades, the plausible range of the true win rate is so wide that a winning and a losing strategy can look the same. Four times as many trades roughly halves the width of that range.
Is backtesting worth it?
Yes, mainly because it rejects bad ideas cheaply. A method that loses on several years of past data rarely starts winning live. A good backtest does not prove a strategy will make money, though: it leaves out real-time execution and your own discipline, so it should be followed by a forward test on a demo account.
What is the difference between backtesting and forward testing?
A backtest applies your rules to historical prices, with the future hidden as you step through the chart. A forward test applies the same rules in real time, usually on a demo account, so it includes real spreads, real fills and your behaviour when you cannot see what happens next. Both should be recorded in R.
Can you backtest a strategy manually?
Yes. Use your platform’s replay or bar-by-bar mode, or scroll back and hide the candles to the right, then step forward one candle at a time and log every trade the rules give you. It is slow, around 10 to 20 hours for 100 trades on a 4-hour chart, but it tests the method you will actually trade.
What is out-of-sample testing?
Out-of-sample testing means keeping a later stretch of history aside while you build and adjust the rules, then running the final rules on it once. Because the rules were never tuned on that data, it gives a fairer picture of how they may behave on new prices. If you change the rules after seeing it, it stops being out-of-sample.
What is a good expectancy for a backtest?
Any expectancy that stays positive after realistic costs, over at least 100 trades and in out-of-sample data, is worth forward testing. Many workable swing strategies sit somewhere around +0.1R to +0.3R per trade after costs. Very high figures, such as +1R per trade, usually point to curve fitting, missing costs or too few trades.
Free course
Learn this step by step
“Trading Strategies: How the Main Methods Work” is a free BrokerCut Academy course: 12 short lessons (about 123 minutes), quizzes, interactive tools and a certificate when you finish.
Enrol free


