Immediate AI

Trading Guide

How To Backtest A Trading Strategy (Without Fooling Yourself)

A step-by-step guide to backtesting a trading strategy: writing clear rules, choosing data, including costs, avoiding overfitting and reading the results.

By the Immediate AI Team · Updated · 9 min read

Backtesting means running a trading strategy over historical market data to see how it would have performed. Done well, it is the cheapest way to throw out bad ideas before they cost real money. Done badly, it produces beautiful charts that fall apart the moment you trade live.

This guide walks through a practical process that works for stocks, crypto, forex or any other market, and highlights the traps that catch most beginners.

Step By Step

How To Backtest A Trading Strategy

Step 1: Write Rules A Computer Could Follow

A backtest can only test what is written down. "Buy when it looks strong" cannot be tested. "Buy when the closing price crosses above the 50-day moving average and volume is above its 20-day average" can.

Write down your market filter, entry rule, stop loss, exit rule and position size. If any rule needs your judgment in the moment, the backtest will not reflect how you actually trade.

  • Market and timeframe: for example, large-cap US stocks on the daily chart.
  • Entry: the exact condition that opens a trade.
  • Stop loss: where you admit the idea is wrong.
  • Exit: a target, a trailing stop or a time limit.
  • Size: the percentage of the account risked per trade.

Step 2: Choose Enough Of The Right Data

Use data that covers different market conditions: rising, falling and sideways periods. A strategy tested only during a strong bull market tells you very little about how it handles a crash.

Make sure the data matches how you will trade. If you trade the four-hour chart, test on four-hour data. For stocks, watch out for survivorship bias: testing only on companies that exist today ignores the ones that went bankrupt, which makes results look better than reality.

Step 3: Include Realistic Costs

Every real trade pays something: a spread, a commission, a funding fee or slippage when the price moves between your decision and your fill. Strategies that trade often are especially sensitive to these costs.

Add a cost to every simulated trade. A useful habit is to run the test again with costs doubled. If a strategy only survives with perfect fills, it does not have much of an edge.

Step 4: Split Your Data To Avoid Overfitting

Overfitting happens when you tune a strategy so closely to past data that it learns the noise instead of a real pattern. The warning sign is a strategy with many parameters, each set to a very precise value, that only works on the exact period you tested.

To guard against it, split your history in two. Build and adjust the strategy on the first part (in-sample). When you are finished, run it once on the second part (out-of-sample) without changing anything. If performance collapses, the strategy was probably overfitted.

  • Prefer fewer rules over more rules.
  • Prefer parameter values that work across a range (for example 40 to 60 days) over one magic number.
  • Test on more than one market if the logic should apply broadly.

Step 5: Read The Results Properly

Total return is the least useful number on its own. Look at how the return was earned and what it cost you along the way.

  • Maximum drawdown: the largest drop from a peak. Could you live through it without quitting?
  • Number of trades: a few dozen trades is not enough to trust the results.
  • Win rate and average win versus average loss: a low win rate can be fine if winners are much larger than losers.
  • Longest losing streak: plan for a live streak at least as long as this.
  • Consistency: are profits spread across years, or do one or two trades carry everything?

Step 6: Forward Test Before Using Real Money

A backtest is evidence, not proof. The next step is a forward test in a paper trading simulator, where the strategy meets live data it has never seen. Run it for several weeks or a meaningful number of trades and compare the results with your backtest.

Some difference is normal. A large gap means something in the backtest was unrealistic, such as costs, fills or data quality. Fix the cause before going further.

Key Takeaways

  • Only clearly written rules can be backtested.
  • Test across different market conditions and include realistic costs.
  • Keep an out-of-sample period to catch overfitting.
  • Judge results by drawdown and consistency, not just total return.
  • Always forward test with paper trading before risking real money.

This guide is for education only and is not financial advice. Trading involves risk, including the loss of your capital. Read our Risk Disclaimer.

Get Started

Practise These Ideas Without Risking Money

Use the paper trading simulator and backtesting tools to test what you learned.