How to backtest a trading strategy: steps, free tools, mistakes
Write exact rules, use clean data, charge realistic costs, fill orders only at prices you could have reached, then check the result on data you never used while building it. Free tools handle single tests well. Testing many variants honestly is the harder part.
By Soren Huang · Published · updated · Editorial policy
Contents
- How do you backtest a trading strategy properly?
- What should you decide before the first run?
- How exact do the rules need to be?
- How much and what kind of data do you need?
- Which costs should a backtest charge?
- When and at what price should orders fill?
- What can free backtesting software do?
- Which mistakes make a backtest look better than it is?
- How do you read the first results?
- How do you test a strategy on data it has never seen?
- What can a backtest not tell you about the future?
- Frequently asked questions
- References
How do you backtest a trading strategy properly?
Write rules exact enough for a computer to follow. Test them on clean historical data. Charge realistic commission and slippage. Fill orders only at prices you could have reached. Then check the result on data you never used while building the strategy. Free tools handle single tests well. Testing many variants honestly is the harder part.
A backtest is a simulation that applies fixed trading rules to historical prices and records the trades those rules would have produced. A flight simulator is a good comparison. A simulator that replays recorded weather shows how the plane handled storms that already happened. It does not show tomorrow's storms. A backtest works the same way. It is useful because the rules meet real past conditions, and it has limits for the same reason.
A proper backtest follows six steps in this order:
- Rules: define the entry, exit, stop and position size with no room for judgment.
- Data: choose the market, the bar size, the session and the date range, and check the data for errors.
- Costs: set commission, exchange fees and slippage per contract or share.
- Execution assumptions: decide when and at what price each order fills.
- Run: run the test once with these settings fixed.
- Review: read the trades, the drawdown and the results by year, then test on unseen data.
What should you decide before the first run?
Decide every setting that changes the result before you run the test. Settings you adjust after seeing the result become part of the fit.
How exact do the rules need to be?
Write the rules so that two people would place the same trades. "Buy when the trend looks strong" is not a rule. "Buy at the next bar's open when the 20-bar average closes above the 50-bar average" is a rule. Include the exit and the stop. Many strategies differ more in how they exit than in how they enter.
How much and what kind of data do you need?
Use enough history to include rising, falling and sideways periods, plus at least one sharp sell-off. For futures, decide how contract months are joined. A continuous contract is a price series that joins successive contract months into one chart, and the way the joins are adjusted changes past prices. For stock backtesting, use prices adjusted for splits and dividends, and include companies that were later delisted. Leaving them out is called survivorship bias, and it makes past results look better than they were. The guide to backtest data quality: gaps, bad ticks, time zones and rolls covers the checks in detail. Also decide whether your bars cover the day session only or the full session. A strategy tested on day-session bars and traded overnight is a different strategy.
Which costs should a backtest charge?
Slippage is the difference between the price the backtest assumes and the price you actually get. Market and stop orders usually fill worse than the trigger price. Charge costs on both entry and exit. Then compare the cost with the average trade, as explained in trading costs in backtests: commission, slippage and the average trade.
Hypothetical example
A futures strategy makes 400 trades with a gross result of $14,000, or $35 per trade. Commission and fees are $5 per round trip. Slippage is one tick worth $10 on entry and one on exit. The round-trip cost is $25, so the net is $10 per trade, or $4,000 in total. Double the slippage and the round-trip cost becomes $45. The net turns into a loss of $10 per trade.
When and at what price should orders fill?
Next-bar execution means that a signal calculated on a bar's close is filled on the following bar, because that close is already over when you know the signal. Also decide how stops and limit orders fill. A common choice is that a limit order fills only if price trades through the limit, not when it just touches it.
What can free backtesting software do?
Free backtesting software is good for testing one idea at a time on a chart. The work gets harder when you want to test many variants, keep records of them and set data aside for an honest check.
| Free option | What it does well | What to plan for |
|---|---|---|
| TradingView's strategy tester | Runs a Pine Script strategy on the chart's symbol and timeframe. It shows a performance summary and a list of trades. Commission and slippage are set in the strategy properties. By default, orders from a bar's close fill at the next bar's open. | Each report covers the current input values. The amount of history depends on the plan and the timeframe. |
| Spreadsheets | Clear for simple daily-bar rules. You see every calculation. | Intraday data and stops inside a bar take a lot of manual work. |
| Open-source Python libraries | Free and flexible. They can test any rule you can code. | You write and check the fill and cost logic yourself. |
| Chart replay (manual testing) | Trains your eye and your execution, one bar at a time. | Slow, and hard to repeat exactly the same way. |
Whichever free tool you use, check three things. Are costs switched on? Do signals fill on the next bar? Can you hold back a period of data that you never look at while tuning? If you try variants by hand, also write down how many you tried. That count matters later.
The tools can also work together. A strategy found in AlphaPrime (Valynox) can be exported as Pine Script v6 source code and run in TradingView's strategy tester as an independent second check. AlphaPrime also exports to MultiCharts, TradeStation and NinjaTrader. The supported-platform export page lists all four. Data feeds and fill rules differ between programs, so compare the two trade lists rather than only the totals.
Which mistakes make a backtest look better than it is?
Three mistakes cause most inflated backtests: look-ahead bias, ignoring costs and overfitting. All three push the result in the same direction, upward.
| Mistake | What it looks like | How to avoid it |
|---|---|---|
| Look-ahead bias | The signal uses a bar's close and the trade fills at that same close. Or the test uses a day's high before the day has ended. | Fill on the next bar. Use only values that were final at the moment of the decision. |
| Ignoring costs | Many small trades with a smooth equity curve. | Set commission and slippage before the first run. Rerun with double slippage. |
| Overfitting | Very specific parameters, and a result that collapses when you move them slightly. | Keep the rules simple, check nearby parameter values, test on unseen data. |
Look-ahead bias is using information in the test that was not available when the trade was placed. Overfitting is tuning a strategy so closely to past data that it captures random noise instead of a pattern that repeats. Overfitting gets worse the more variants you try. Bailey and colleagues (2014) show that when enough configurations are tested, one of them will look strong on the data used to build it (the in-sample period), even on data with no real pattern.
How do you read the first results?
Read the trades and the drawdown before the net profit. A large total built on a few trades or a single year says little.
- Number of trades: a few dozen trades give a very rough estimate. More trades give a steadier one.
- Average trade after costs: it should be several times the round-trip cost, so that worse fills do not erase it.
- Maximum drawdown: the largest fall from an equity peak to a later low. Ask whether you could sit through it with real money.
- Results by year and by side: look at long and short trades separately. A strategy that made all its money in one year or on one side depends on that period.
- Largest trades: remove the best two or three trades and see what remains.
Expectancy is the average amount a strategy wins or loses per trade. It is calculated like this:
Expectancy = (W × average win) − (L × average loss)
Here W is the share of trades that win, for example 0.40 for 40%. L is the share that lose, so 0.60 in that case. The average win and the average loss are measured in money after costs. The win rate and expectancy calculator does this from your trade list.
How do you test a strategy on data it has never seen?
Set aside a period of history before you start. Run the finished strategy on it once, without changing anything. Out-of-sample data is history the strategy never saw while you designed or tuned it, and it is the closest a backtest gets to a fresh start.
Hypothetical example
A strategy is built on the first 8 years of data. There it shows 320 trades, a net result of $24,000, an average trade of $75 after costs and a maximum drawdown of $6,000. On the last 3 years, which were never used, it shows 90 trades, a net result of $1,800, an average trade of $20 and a maximum drawdown of $5,500. The edge per trade shrank by about three quarters, while the drawdown stayed almost the same. That points to a fit that was mostly noise, even though the unseen result is still slightly positive.
One unseen period is a single test. Walk-forward analysis repeats the cycle. It tunes on one window, tests on the next window, moves forward and joins the unseen pieces into one result. If you open the unseen data, change the rules and test again, it is no longer unseen.
Researchers call this problem data snooping. White (2000) describes a test of whether the best rule found in a search beats a benchmark once the size of the search is taken into account. The Sharpe ratio is the average return divided by how much returns swing, a score for return per unit of risk. Harvey and Liu (2015) show how to lower that score to allow for the number of variants tested.
What can a backtest not tell you about the future?
A backtest tells you how fixed rules behaved on one path of past prices. It does not tell you what the next path will look like.
- Market changes: volatility, liquidity and the behaviour of other traders change over time. Exchange rules and trading hours change too.
- Your real fills: fast markets, larger order sizes and gaps over closed sessions can fill worse than any assumption you set.
- Events that are not in the data: a type of shock the history never contained is not tested.
- You: the backtest follows every signal through every drawdown. A person might stop after the fifth loss in a row.
What this means if you use AlphaPrime: AlphaPrime's backtesting evaluates every candidate on your own historical bar data with fees, slippage, next-bar execution and the trading session you choose. Each step above is a setting you fix once, so the same rules apply to every candidate before any of them is ranked.
Frequently asked questions
Can I backtest a trading strategy for free?
Yes. Chart platforms such as TradingView include a strategy tester, spreadsheets work for simple daily rules, and open-source Python libraries can test any rule you can code. In each case, switch on commission and slippage, fill signals on the next bar, and keep a period of data aside for a final test.
How many trades does a backtest need?
More trades give a steadier estimate of the average trade. A few dozen trades is a very rough minimum, and strategies with many parameters need more. Also check that the trades are spread across different years, not bunched in one period.
Does a profitable backtest mean the strategy will make money?
No. A backtest shows how fixed rules behaved on past prices with the costs you assumed. The result is more believable when it holds on data the strategy never saw, when nearby parameter values give similar results, and when it still works with doubled slippage.
What is look-ahead bias in backtesting?
Look-ahead bias is using information in the test that was not available when the trade was placed. The most common case is filling a trade at the same bar close that produced the signal. Filling on the next bar removes it.
References
- Bailey, D. H., Borwein, J., López de Prado, M., & Zhu, Q. J. (2014). Pseudo-Mathematics and Financial Charlatanism: The Effects of Backtest Overfitting on Out-of-Sample Performance. Notices of the American Mathematical Society, 61(5), 458–471.
- White, H. (2000). A Reality Check for Data Snooping. Econometrica, 68(5), 1097–1126.
- Harvey, C. R., & Liu, Y. (2015). Backtesting. The Journal of Portfolio Management, 42(1), 13–28.
What AlphaPrime does here
Strategy validation adds walk-forward analysis on unseen data, and the Performance Report shows equity, drawdown, trades, periodic performance and long and short analysis.

