How to Backtest a Trading Strategy - Complete Guide 2024


    If you've been into trading for a while, you've probably heard of backtesting. In this article, you will find everything you need to know about it: what backtesting is and how it works, what it takes to do it, the optimization of a system, and common mistakes to avoid.

    What Is Backtesting and Why Is It So Important?

    Backtesting is a very useful procedure that allows you to evaluate the effectiveness of a trading strategy before live trading it with your money.

    This procedure requires three fundamental elements: a software application with backtesting functionality, a strategy codified into a trading system, and a historical series of past market data.

    The software, which is also called a "trading platform," enables you to simulate the performance of the strategy on historical market data and returns various types of reports that help you evaluate the system. Among these reports we find equity curves, drawdown charts, and numerical data relating to metrics such as net profit, average trade, number of trades, drawdown, etc.

    A correct interpretation of these reports helps you to understand if the tested strategy will be effective when you will go live with it.

    Single-Market and Portfolio Backtest

    Backtesting can be used to test a trading strategy on a single market or on a basket of different instruments.

    The choice between the two approaches obviously depends on your goal. If you want to understand if a strategy can exploit the characteristics of a specific market, you will opt for single-market backtesting. If, on the other hand, you want to evaluate a system on several instruments, you will carry out a portfolio backtest.

    Portfolio backtesting can be very useful for comparing the performance of the same strategy on multiple markets. Suppose you have developed a strategy for stocks. With portfolio backtesting, you can test it on all the stocks that make up the S&P 500 index instead of doing 500 single-market backtests on each stock that makes up the index. In this way, you will be able to compare.

    How to Perform a Backtest

    how to backtest a system

    To backtest a strategy you need a trading platform, the strategy code, and historical market data. As for the trading platforms that offer backtesting features, you can refer to our article on the best platforms for systematic trading. As for the historical datafeed providers, we advise that you read this article.

    But let's get to the actual steps of a backtest.

    1. The Data and the Chart

    First of all, you need to upload a series of historical data within the trading platform you are using. For learning how to select your historical data, we invite you to watch this video by Andrea Unger:

    2. The Trading System

    At this point, you need to apply the code of the system you want to test to a historical data series so that the instructions contained in the strategy are simulated on the past movements of the market.

    For example, if you have a trend-following system that buys when the market rises, apply that system to the historical data series, and all the buy operations that took place in the reference period will appear on the chart.

    3. The Parameters

    To backtest a strategy, you need to set some parameters for the software to perform the calculations.

    The parameters depend on the strategy code and include the backtest reference period, the time window for opening and closing the positions, the market session, the number of contracts or monetary amount for each position, possible filters on the entries, exit levels for stop loss or take profit, etc.

    4. The Results

    At the end of the calculations, you can access the section of the platform containing the reports. Here you will find various graphs, such as the cumulative profit curve, the equity line (including the curves of the long and short trades only), and the drawdown curve.

    Among the written reports there are the overall metrics of the strategy (profit, loss, drawdown, etc.), the trade analysis (average trade, the total number of trades, percentage of profitable trades, etc.), and the annual or monthly distribution of profits.

    Backtesting and Optimization

    how to optimize a trading system

    Some trading platforms allow you to perform optimizations on strategies or simulate their performance when certain parameters vary. This can help you identify the values that return the best results.

    Optimization is a form of backtesting in which, instead of testing the strategy with a single set of parameters, different tests are made using ranges of values and the resulting metrics are compared (profit, loss, average trade, drawdown, number of trades) to find settings that can improve the effectiveness of the strategy.

    Imagine having a trend-following strategy that opens long positions when the market rises but only in the presence of a strong enough trend. Such a strategy could use an indicator to measure the strength of the trend (for example the ADX) and buy only when the value of this indicator exceeds a certain threshold.

    To find the threshold value that works best, you can launch an optimization that tests the strategy with different values and choose the one that you believe produces the best performance.

    Optimizations can be used to determine the value of all inputs in a system, from the start and end times of the trading window to the stop loss and take profit values.

    Beware of Overfitting

    As you may have guessed, the optimization of a strategy can be extremely useful in deciding which parameters to use for live trading. However, evaluating the results of optimizations requires great attention.

    Systematic trading is a scientific approach as decisions about strategies are made on the basis of concrete data, i.e. the results of backtests and optimizations. Although the use of numerical data allows for greater objectivity, the decisions taken when choosing the parameters are basically discretionary and therefore require common sense and realism.

    Optimizing a strategy by opting exclusively for the parameters that produce the absolute best performance can lead to the creation of ineffective strategies.

    This is for two main reasons. The first is that the backtest, as the name implies, is a test in which the results obtained depend on the past movements of the market. These results are statistically significant but obviously cannot guarantee future replicability.

    In fact, there is no certainty that the market will continue to move as it did in the past, so systems that are too optimized on past data may not be able to adapt to present and future market conditions.

    Another reason is that the parameters that offer the best results are not always reliable. If a certain parameter works better than another one, there should always be a specific reason. From this point of view, it is therefore crucial to make sure that it is not the result of chance or of an error in the historical data.


    Backtesting is an important part of developing a successful trading strategy, provided it is executed correctly and the results are evaluated with common sense.