Backtesting: How to Backtest a Trading Strategy

Backtesting is one of the most important parts of building a trading system. When it is done properly and its results are interpreted correctly, it helps you assess how effective a trading strategy or Expert Advisor is, so you can spot any flaws in your strategy and adjust or optimize it before using it in real, current trading conditions. This guide covers everything you need to know about backtesting and how to backtest a trading strategy.

What Is Backtesting?

Backtesting is defined as a method that lets a trader or investor test how effective their current online trading strategy is using historical data.

For a trader, backtesting therefore means using past data to evaluate a strategy’s performance. Depending on the results, they will know whether they can apply the strategy as it is going forward or whether they need to change it. Backtesting also lets you measure the risk and profitability of a trading strategy without risking real capital.

On related topics, our articles “Short Selling: All About the Short Selling Strategy” and “How to Download & Install MT5” are a good complement to this guide.

How Does Backtesting Work?

Backtesting is essentially based on the idea that what worked in the past will work in the future. However, it is very important to take many factors into account – such as the market environment, trading fees, withdrawal fees, profitability and risk level – before drawing conclusions about a strategy’s performance.

How to Test or Backtest a Strategy on MetaTrader

  1. Open the MT5 or MT4 Strategy Tester, depending on your version of MetaTrader (use the keyboard shortcut “CTRL+R”, click “View” and select “Strategy Tester”, or click the “Show / hide Strategy Tester” button)
  2. Choose the indicator, Expert Advisor (EA) or trading robot you want to test
  3. Select the timeframe on which you want to run the historical test (backtest)
  4. Choose the symbol or instrument you want to backtest (CFD, forex currency pair, etc.)
  5. Select “Every tick” as the model, because it is more reliable than the others
  6. Enter the period you want to test, i.e. the start and end dates
  7. Specify the deposit amount and the MetaTrader 4 or 5 leverage level you want
  8. Click “Start” to run the backtest

How to Backtest an Expert Advisor on MetaTrader

  1. Download as much historical data as possible for the asset you want to backtest on MetaTrader 4 or MetaTrader 5. This data is available from the “Tools / History Center” menu. To make MetaTrader use as much data as possible, click “Tools / Options”, then “Charts”.
  2. Enter the following value in “Max bars”: 9,999,999,999 to give MetaTrader access to as much data as possible.
  3. Open the MT4 or MT5 Strategy Tester from the “View / Strategy Tester” menu. From this window you can configure the settings for your forex backtesting on MT4 / MT5 or your crypto backtesting.
  4. Set the available options: asset, timeframe, model, dates, etc., plus visual mode to follow your MetaTrader 4 backtest live on a chart.
  5. For the model, select “Every tick” for a more reliable result.
  6. To test with values other than the defaults, simply change the settings by clicking “Expert properties”.
  7. Click “Start” to begin backtesting your Expert Advisor on MetaTrader 4 / 5.

How to Backtest an Indicator on MetaTrader

  1. Open the Navigator window by pressing Ctrl+N
  2. Click the “Expert Advisors” tab
  3. Create a new Expert Advisor (EA) by pressing the “Insert” key on your keyboard
  4. Rename the new EA in the creation wizard and click “Finish”
  5. Press “F5” to compile the EA and “F4” to return to the MetaTrader platform
  6. Open the Strategy Tester by pressing “Ctrl+R”
  7. Choose the EA you created
  8. Select the asset (currency pair, CFD, etc.) and the period of your choice
  9. Select “Visual mode” and choose the slowest speed by moving the slider to the left
  10. Start the simulation by clicking “Start”
  11. Add the indicators of your choice to the chart and use the slider to change the execution speed or pause.

How to Backtest a Trading Strategy in November

  1. Start by loading all the available historical data into the backtesting algorithm (make sure you specify the test period)
  2. Then select additional data from a different period (this lets you check how reliable your strategy is across different periods)
  3. Now set the parameters according to the complexity of the backtesting model that suits you (risk capital, starting capital, commission fees, benchmark, etc.)
  4. Next, configure your trading strategy settings (entry point, stop loss, take profit, etc.)
  5. Now run the backtest on all your test data. Use the information above to simulate trades over a given period.
  6. Once the backtest is complete, repeat the process on other data to make sure you have removed any randomness and that your strategy is profitable.

How to Backtest on ProRealTime

  1. Click the “ProBacktest” tab
  2. Choose your currency pair or, more generally, your asset
  3. Adjust the various trading and brokerage settings (fixed amount per order, margin, percentage, lot size, spreads, etc.) and risk management settings
  4. Optimize the values of the backtest variables you want in the “Variable optimization” box. The result shown in the “Optimization report” tells you which variables can make your system optimal
  5. Tick the “Keep window open” box to view the simulation
  6. Enter the execution period (start date and end date)
  7. Finally, click “Backtest my trading system”

How to Backtest on MT5 (MetaTrader 5)

  1. Choose the currency pair or asset traded and the period the backtest will cover
  2. Add all the indicators you will use to the chart
  3. Scroll the chart back to the point in time where you want to start
  4. Choose a combination of time periods that include different market environments (uptrend, ranging, neither trending nor ranging, etc.)
  5. Go back in time on your chart by turning off auto-scroll
  6. Press “Enter” to enter the start date
  7. Specify the date you want to go back to on the chart, then press “Enter” to confirm
  8. Scroll to the historical data you want to start from and press “F12” to move the chart forward
  9. Place a trade when a setup triggers and record the results in an Excel spreadsheet. It is important to test as much data as possible
  10. Check the results (win rate, number of trades, maximum drawdown, etc.)
  11. If you are satisfied with the first pair / asset, test a different asset or currency pair
  12. Always make sure you record each round of 5 backtests on a separate spreadsheet.

Backtesting: Why Backtest a Trading Strategy?

  • To determine its risk level and profitability
  • To know how the strategy performs in a real market situation
  • To identify the strategy’s weaknesses
  • To fine-tune the strategy if it has any flaws
  • To evaluate and optimize the trading strategy before putting it into practice

What to Do Before Backtesting a Trading Strategy

  • Determine beforehand whether you are a discretionary trader or a systematic trader. Backtesting suits systematic traders better than traders who decide their entries and exits based on their own judgment
  • Identify any biased data that could hurt your model’s returns
  • Avoid optimization bias, i.e. the situation where some traders inflate their results to cover up their system’s weaknesses
  • Protect yourself against look-ahead bias, which is the accidental inclusion of future data in your simulation. It is therefore important to double-check your data and methodology several times to avoid this miscalculation
  • Avoid survivorship bias, which occurs when your strategy backtesting does not take into account all the assets you plan to trade
  • Also avoid psychological tolerance bias, which develops when your backtests are run over the long term while you plan to trade short term
  • Choose your backtesting software carefully (ideally the best one): you can run a stock market backtest with your broker, choose free backtesting software, or use any other trading platform or software of your choice to run backtests.
  • Set a budget to pay for the backtesting platform of your choice on a regular basis if it is a paid one.
Tableau originalDiversifiez votre patrimoineInvestissez dans l’artDes tableaux originaux, pièces uniques signées par l’artiste. Un actif tangible, loin des écrans et des cotations.Voir les tableaux →

How to Evaluate Backtesting Results

  • Always take the analysis context into account, including the assets in the portfolio, the market environment, the specific features of the strategy, etc.
  • Determine your risk/reward ratio, i.e. your level of risk and your profit / return targets
  • Check that the trading strategy’s results are in line with the goals you set
  • Remember to always add a benchmark – for example the S&P 500 – after backtesting.

What Are the Backtesting Strategies?

  • Out-of-sample testing: this involves splitting real price data into three parts: the first third is used to build the system, the second to optimize it and the third to apply it.
  • Bootstrapping: also known as data resampling, this backtesting strategy involves testing a few data points drawn from the full dataset and resampling them as many times as possible (nn) until the system is reliable.
  • Monte Carlo simulation: with this backtesting strategy, a computer generates the simulated data used to test the system. Monte Carlo simulation has a twofold advantage: you can create an unlimited amount of data, and you can optimize or test the system many times.

What Are the Backtesting Methods?

  • Automated backtesting: this requires programming skills or the use of backtesting software. In other words, automated backtesting is carried out by a program
  • Manual backtesting: analyzing charts and historical data, placing trades… everything is done by hand with this method.

There are also backtesting methods based on Value-at-Risk (VaR), such as Monte Carlo simulation, variance-covariance, the proportion of violations (failure rate), the Markov test, etc.

Manual Backtesting: Pros and Cons

Pros

  • Helps you better understand your market setup
  • Helps you better grasp your strategy and any weaknesses it may have
  • Makes live trading easier
  • Doesn’t require much preparation
  • Offers more flexibility
  • Seems better suited to beginner traders
  • Doesn’t really require solid coding skills

Cons

  • A long and tedious method
  • Risk of making mistakes, especially at the beginning
  • Historical data is often limited

Automated Backtesting: Pros and Cons

Pros

  • Makes the test run quickly
  • Makes it easier to optimize your strategy
  • Proves the strategy’s effectiveness over time
  • Lets you easily test different instruments or periods
  • Uses historical data that is often unlimited

Cons

  • Takes a lot of time to produce results that let you judge how consistent a strategy is
  • Requires coding skills

Which Software or Tools Can You Use to Backtest Trading Strategies?

  • MetaTrader and its built-in Strategy Tester. With this multi-threaded tool, you can easily run an MT4 backtest or an MT5 backtest
  • ProRealTime and its ProBackTest tool
  • Trader Workstation and its Portfolio Manager tool, designed for stock portfolio managers
  • Forex Tester: software mainly for forex backtesting
  • TradingView: a backtesting platform for stocks and currencies
  • MetaStock: backtesting software for beginners and intermediate traders
  • TrendSpider, with its trading backtest tool called Strategy Tester
  • Backtest Manager, which is fairly easy to use
  • NinjaTrader: trading software whose tool known as Profit Finder is versatile and flexible
  • thinkorswim and its thinkBack tool for backtesting options. Many other backtesting programs exist.

What Useful Results or Information Does a Backtest Provide?

  • Volatility: a measure of how much a security (stock, bond, etc.) varies from its average price. Volatility is expressed as a percentage;
  • Annualized return: the strategy’s annual rate of return; the annual return percentage over a period longer or shorter than 12 months;
  • Risk-adjusted return: the ratio between an investment’s return and the risk associated with it, or more simply the ratio between profitability and volatility;
  • Win/loss ratio: the ratio between the number of winning trades and the number of losing trades, without taking into account the amount won or lost;
  • Market exposure: the amount of capital allocated to a particular asset or asset class;
  • Net profit and/or loss: the net value of gains and/or losses.

Backtesting vs Paper Trading / Demo Trading

  • Backtesting and paper trading (or demo trading) are both methods traders use to test new strategies
  • Backtesting and paper trading share the same goal: increasing profits. But while one bases the strategy’s future success on the past, the other lets you check your strategy in real time
  • Demo trading has greater predictive value than backtesting, because the former focuses on the present and the latter on the (sometimes distant) past
  • Backtesting seems as important as paper trading insofar as it shows how your strategy will perform under different market conditions
  • Backtesting is better suited to automated trading
  • Demo trading is more a day trading practice than a testing method
  • To trade on a demo account, it is important to already have a day trading or scalping strategy
  • Demo trading can take a day, a week or even longer to optimize a strategy; with backtesting, a few minutes can be enough to test a large amount of historical data for your strategy
  • Paper trading lets you spot slippage that can occur in real time, which is not the case with backtesting
  • Like paper trading, demo trading lets you trade in real conditions with virtual funds
  • Market conditions such as widening spreads, for example, are not always taken into account in backtesting
  • Note, however, that you can run a backtest on a demo account with suitable trading software or an app.

Backtesting vs Scenario Analysis

  • Scenario analysis is used to assess changes in a portfolio’s value when an adverse event occurs. It can also be used to anticipate a worst-case scenario.
  • Unlike backtesting, which uses real historical data to predict a strategy’s weaknesses or success, scenario analysis relies on hypothetical historical data to estimate likely outcomes.

Trading Backtest Example

  • Take the example of buying asset AAA on the first weekly close above the 20-week moving average. The same asset is then sold under the same conditions over a similar period
  • Here, the backtest covers the period from 2019 to the present.
  • The results are as follows: buy at $4,000 – sell at $8,000; buy at $8,500 – sell at $8,000; buy at $9,000
  • These results show that the strategy is profitable. However, since the data analyzed covers a short period, the ideal way to optimize the strategy would be to go back well before 2019.
  • In any case, to make a strategy truly profitable, you need to consider as much historical data as possible, add more technical indicators, more metrics, etc.
  • The success of a trading strategy depends on several factors, mainly your risk tolerance and investment objective.

Which Biases Should You Avoid in Backtesting?

  • Backtesting bias: biases of any kind should be avoided when building a model to test. For greater objectivity, the idea is to use an unbiased sample and test the data over several different periods;
  • Look-ahead bias: this means including information in the model being backtested that will not be available when the strategy is deployed. You should avoid all hypothetical information
  • Optimization bias or data-snooping bias: this means adding several parameters to your algorithm and fitting them to existing data. It is better to use fewer parameters and test them over different periods and markets
  • Survivorship bias: this means using only the assets – for example stocks – that have survived in your backtest, while ignoring those that are no longer listed. The idea is to include the S&P 500 stocks in your strategy.

What Is the Ideal Backtesting Scenario?

  • Choose data samples over a long enough period to include different market conditions.
  • Select as much historical data as possible. For stock trading, for example, in addition to data on current companies, you should include data on companies that went bankrupt, were liquidated or were sold.
  • Choose backtesting software that takes all trading costs into account, however small, to avoid an unpleasant surprise in profitability at the end of the backtesting period.
  • Run out-of-sample tests and performance tests to check your system’s effectiveness before investing real money.

Tips for Backtesting Trading Strategies Effectively

  • Always take into account all stock market trends during the test period. Ideally, backtest trading strategies under different market conditions;
  • Don’t test your trading strategy on specific assets and then apply the results to another asset. For example, if your trading strategy backtest is run on tech stocks, it may not work or may not give relevant results for an ETF backtest;
  • Favor low volatility to minimize risk. This approach to managing your holdings makes transitions easier;
  • Take care to increase the average number of bars held, in order to reduce brokerage fees and boost overall returns;
  • Keep your market exposure below 70%; this helps reduce risk and makes it easier to move in and out of a given asset;
  • Increase the average gain and the risk-reward ratio to take more positions and reduce brokerage fees;
  • Consider not only the annualized return but also the risk-adjusted return;
  • Customize your backtesting as much as possible. That said, pay particular attention to position size during the backtest, commission fees, tick size, interest rates, entry and exit rules, etc.
  • Avoid over-optimization, i.e. the tendency some traders have to inflate their results to the point that their system no longer works in the future;
  • Always make sure a strategy works before applying it.
Suivez-nous sur Telegram