Backtesting: How to Test a Trading Strategy and the Pitfalls That Fake the Results
Backtesting replays a trading strategy on past prices to see what it would have done. Here is the method step by step, the five pitfalls that make a backtest look better than the strategy, and a worked example on the S&P 500 ETF where the best rule of 2022 to 2023 came 16th of 17 afterwards.
On this page
Backtesting is a simulation: you take a trading strategy with fixed rules, apply it to past prices bar by bar, and record every trade it would have made. The result is what the strategy would have earned, lost and risked over that period, if you had followed it exactly. A backtest does not predict the future; it tells you whether a set of rules had any edge in the past, and how painful it was to follow.
The 5 and 20 day crossover on the daily S&P 500 ETF, May to October 2026
- Price, daily candles (hollow up, filled down)
- SMA(5) of the close (solid line)
- SMA(20) of the close (solid line, second color)
- Fast average crosses above the slow one
- Fast average crosses below the slow one
How to backtest a strategy, step by step
- Write the rules down before you look at results. Entry signal, exit signal, stop, position size, and which market and timeframe. If a rule needs judgment, it cannot be backtested; turn it into a test you can compute.
- Get clean data. Prices adjusted for splits and, for a total return, dividends; enough history to cover rising, falling and flat markets.
- Split the history. Keep a recent part aside, the out-of-sample period, and do not look at it while you build the rules.
- Replay bar by bar, with only the information available at each bar. A signal computed on a close can only be traded after that close, at the next open at the earliest.
- Include costs. Commissions, the bid and ask spread, and slippage, on every entry and every exit.
- Measure more than the return. Number of trades, share of winners, largest drawdown, time in the market, and the result of simply buying and holding over the same period.
- Run the frozen rules once on the out-of-sample period. That result, not the in-sample one, is your estimate.
The three periods of a strategy test
You can do this by hand on a chart, in a spreadsheet, or in software; the tools are covered on our strategy tester page, and the other tools of chart reading in our technical analysis section.
A worked example: moving average crossovers on the S&P 500 ETF
The rule: hold the ETF when its fast simple moving average of closes is above its slow one, stay out otherwise, as in a moving average crossover. A signal on a close is traded at the next open; costs are 0.05% per trade, each way. Data: the daily S&P 500 ETF, dividend-adjusted prices. When the fast average is already above the slow one on the first day of a period, as it was for the 5 and 20 day pair in both periods, the test buys at the second open of the period; a position still open at the end is valued at the last close.
In the in-sample period we tried 17 pairs: fast averages of 5, 10, 20 and 50 days with slow averages of 20, 50, 100, 150 and 200 days, the fast always shorter. The best was 5 and 20 days. We then ran the same pair, unchanged, on the out-of-sample period.
| 5 and 20 day crossover, S&P 500 ETF | In-sample, January 3, 2022 to December 29, 2023 | Out-of-sample, January 2, 2024 to October 28, 2026 |
|---|---|---|
| Return after costs | +14.4% | +28.5% |
| Trades (winners) | 14 (7) | 22 (8) |
| Buy and hold, first close to last close | +2.6% | +68.7% |
| Rank among the 17 pairs | 1st | 16th |
The five best pairs of 2022 and 2023, and the best of 2024 to 2026
- In-sample, 2022 to 2023
- Out-of-sample, 2024 to October 2026
The pair that looked best in 2022 and 2023 was among the worst afterwards, and the pair that ranked first afterwards, 5 and 150 days, had come only 10th of 17 in the in-sample period. The ranking built on 2022 and 2023 did not carry over to the next period: that is what overfitting looks like, and only a period kept aside can show it. The example is an illustration of the method, not a strategy we recommend.
Five pitfalls that make a backtest look better than it is
1. Overfitting
Trying many variants and keeping the best one finds the variant that fitted the noise of that period. Every parameter you tune adds to the problem. The defenses: fewer parameters, an out-of-sample period, and suspicion of any result that only works for one exact setting.
2. Lookahead bias
Using information that was not available at the time of the trade. The classic case is acting on a signal at a price that came before the signal was known. A signal computed on a bar's close can be traded at the next bar's open at the earliest; even a fill at the signal bar's own close assumes you acted on a price that was only final once the bar was over.
When an order triggered at the close can fill
In our example, buying and selling at the open of the bar whose close gives the signal, which no trader could do, turns the out-of-sample result of the 5 and 20 pair from +28.5% to +37.9% with the same costs. Swing points, which are only confirmed several bars after they form, are another trap: a rule that uses a swing low on the day it formed is looking into the future, as our page on the Aroon indicator and ZigZag shows with dated examples.
3. Ignoring costs
A strategy that trades often pays the spread and commissions every time. Without costs, the 5 and 20 pair made +31.3% out-of-sample instead of +28.5%; with fewer trades the gap is smaller, with daily trading it can erase the whole edge.
The same 5 and 20 day rule, out-of-sample, counted four ways
4. Survivorship bias
Testing a stock strategy on today's index members leaves out the companies that were removed, went bankrupt or were bought, and the losers they would have produced. Use data that includes the delisted names, or test on instruments that existed through the whole period, such as an index ETF.
5. Too few trades
With 14 trades over two years, as in our in-sample period, a handful of trades decides the result: too few to tell skill from luck. More markets, more history or a faster timeframe give a larger sample, and the conclusions should wait for it.
Backtesting, forward testing and paper trading
A backtest runs the rules on the past. Forward testing runs the same frozen rules on new data as it arrives, without changing them, either on paper or with a small position. It is slower, but it cannot be overfitted to data you have not seen yet, and it tests something the backtest cannot: whether you actually follow the rules. A trailing stop or a time exit, written in advance, is easier to follow in both. Simulated accounts for this step are compared on our page of paper trading apps.
Paper trading keeps one source of the backtest's optimism: a simulated order often fills in full at the quoted price, while a live order that is large for the market uses up the best price and fills partly at worse ones.
Paper fill versus live fill of a large buy order
- Bars
- sell orders waiting at each price level (asks), sized by quantity
- Solid line
- the price a paper account fills at
- Dashed line
- the higher average price of the live fill
How we test chart patterns
The figure pages of this site, candlestick patterns included, are backtested with the same principles: a fixed detection rule for each pattern, only the data available at each bar's close, with every measurement starting on the next bar, assumed costs per market, and three comparisons: random bars of the same asset, the excess return over the rest of the market, and the same trade on random dates of the same asset in the same conditions. The whole pipeline is also run on random prices, where no pattern can have an edge, to check that it does not invent one. The method is described in how we test patterns.
Backtesting software
The best backtesting software depends on your market and on whether you write code; the detailed comparison is on our strategy tester page. The platforms most often used for it:
| Platform | What it offers for testing |
|---|---|
| TradingView | Strategies are scripts in its Pine Script language, whose results can be read on past data (backtesting) and on new data as it arrives (forward testing). |
| MetaTrader 5 | Its built-in Strategy Tester runs an automated strategy on historical quotes and searches for the best input parameters, which is where overfitting starts. |
| MetaStock | The end-of-day version, MetaStock D/C, backtests strategies on past data; the real-time version, MetaStock R/T, runs on Refinitiv XENITH data. |
| NinjaTrader | The platform covers charting, backtesting and trade simulation. |
| Interactive Brokers | Offers paper trading with simulated funds alongside its Trader Workstation platform, which is forward testing rather than backtesting. |
| TrendSpider | Its Strategy Tester backtests trading strategies on its charts. |
| Trade Ideas | Its back tester is called OddsMaker. |
Every tool runs a rule with lookahead bias or an overfitted setting as readily as a sound one: the checks above are yours to make.
Learning to backtest
New Trader University sells an online course on the subject, Backtesting 101, which walks through backtests of moving average strategies. We have not taken it and do not rate it.
Frequently asked questions
What is backtesting in trading?
Applying a trading strategy with fixed rules to past prices, bar by bar, to see the trades it would have made and what they would have earned or lost.
What's the difference between backtesting and forward testing?
A backtest runs the rules on past data. Forward testing runs the same frozen rules on new data as it arrives, on paper or with a small position, which also tests whether you follow them.
How many times should I backtest a strategy?
Build and tune it on one period as often as you need, then test the frozen version once on a period you kept aside. Each extra look at that period turns it into another in-sample test.
Is backtesting reliable?
Only as reliable as its rules and data. Lookahead bias, overfitting, missing costs and survivorship bias all make a backtest look better than the strategy. In our example on the S&P 500 ETF, the best of 17 rules in 2022 and 2023 ranked 16th afterwards. A structured course such as Backtesting 101 is one way to practice it; we have not taken it.
What is the best backtesting software?
There is no single best one: the right tool depends on your market and on whether you write code. TradingView, MetaTrader 5, MetaStock, NinjaTrader, TrendSpider and Trade Ideas all include a backtester; we have not ranked them. The comparison is on our strategy tester page.
Can you backtest by hand?
Yes: scroll a chart bar by bar, hide the future, and record each trade in a spreadsheet. It is slow but forces you to apply the rules as you would live. To practice it on moving average strategies, the course Backtesting 101 covers them.
