If you’ve been learning about trading, you’ve probably come across the term “backtesting”. The term refers to the practice of testing how trading strategies would play out in real-world conditions.
Backtesting can help traders confirm a particular strategy or model would work, without having to risk capital. If the backtest produces successful results, the trader may gain the confidence to go ahead with the trade. If not, this is a clear signal to rethink the strategy and check for erroneous assumptions driving the trading idea.
Due to its potential to help traders avoid mistakes and improve their results, backtesting has become a popular tool. In this article, we’ll take a look at the basics of how to perform backtesting on MetaTrader 5, one of the world’s most popular and versatile trading platforms.
What is backtesting? [1,2]
Backtesting allows traders to test and compare various trading strategies by running them against real world historical data. The idea is that if a trade performed well in the past, there’s a high chance it can also perform well in the future. The reverse is also true.
This assumption is based on the fact that while the dynamism of the stock market renders it impossible to predict future developments, it nonetheless tends to move in similar patterns as it did historically.
What can backtesting tell us?
When running a backtest, traders look for specific datapoints to help them determine the viability of their strategy or model. Some common backtesting measures include:
- Net Profit/Loss generated by the trade
- The total returns of the portfolio over a given time frame, taking into account trading fees and other costs
- The risk-adjusted return on the trade. This looks at the level of profit and the amount of risk taken to achieve it, compared to a risk-free investment
- Exposure, the degree of exposure to different segments of the market, or the percentage of capital exposed to the market
- Trade volatility, i.e., the dispersion of returns on the portfolio, expressed as maximum upside and downside
Is backtesting foolproof?
As useful and versatile as it is, backtesting is far from foolproof. There are several factors that could throw off the accuracy of a backtest, causing it to return glowing results that have no basis in reality. This can create a false sense of confidence that can cause traders to take on more risk than they should.
Two pitfalls to be aware of when backtesting include:
- Cherry-picking stocks that have performed well up into the current day. For a more reliable test, it is important to use a truly representative sample of stocks, including companies that eventually went bankrupt or were sold or liquidated.
- Data dredging, which refers to testing a large number of strategies and models against the same set of data. This could increase the number of invalid strategies passing the backtest only to ultimately fail in the real world.
To mitigate the risks posed by these pitfalls, it is recommended that traders test strategies using sets of data from both in-sample and out-of-sample time periods If the strategy succeeds across different time periods, the likelihood increases of its validity in the real world.
Backtesting with MetaTrader 5 – What is Strategy Tester? [3]
Strategy Tester is a built-in function in MetaTrader 5 (MT 5) that traders can use to perform backtesting. It is widely used to test out different Expert Advisers (EA), which are automated trading software popular among MT 5 users.
Traders can test their selected EAs using Strategy Tester, which operates based on historical quotes of currencies, stocks and other assets. EAs being tested follow their own algorithms to run through accumulated quotes and perform virtual transactions; this allows an evaluation of how the EA would have traded during the selected time period.
Some key capabilities of Strategy Tester include:
- Several testing modes. Users can select from different testing modes balancing between speed and accuracy. “Every tick” offers the highest accuracy, “1 minute OHLC” allows speedy but also accurate tests at the same time, while “Open prices only” is suited for a quick rough estimation.
- Graphical display of test results. Through various bar charts, the results of your backtest can be easily understood. Datasets displayed include profit/loss percentage ratio, number of profitable/loss-making deals, risk factor, expected payoff and more.
- Visual testing. This allows traders to track how the EA is performing in real time by slowing down or pausing the test. Also facilitates testing of additional custom indicators.
- Optimisation. Traders can choose and tweak input parameters to optimise the returns produced by the EA. This helps traders to discover the best set of inputs and fine-tune their strategies.
- Forward testing. To avoid “over-optimisation” or parameter fitting, traders can make use of the forward testing option. This divides the database of quotes into two separate parts – one optimised, one unoptimised. Should EAs perform well on both segments, this shows that the best parameters have been set. This eliminates the possibility of parameter fitting, further increasing the likelihood of success in the real world.
How to use Strategy Tester?
Preparation: Download and install MT 5
Before you can use Strategy Tester, you first need to download and install MT 5. For the purposes of this article, we will be using the Windows version.
Follow these steps to download and install MT 5.
1. Go to https://www.vtg-mkts.com/trading-platform/metatrader-5/
2. Click on the relevant button to download the installation package. You may choose from Windows, MacOS or Linux versions.
3. Once downloaded, double-click the installation package. Follow the on-screen prompts to complete the installation.
4. Once installation is complete, you’ll need to register for a Vantage account before you can start using MT 5 and Strategy Tester.
To register for a Vantage account:
1. Choose whether to register a live trading account or a demo account.
2. Once your account has been created, login to MT 5 using your credentials.
3. You can now proceed to use Strategy Tester, see the next section.
Setting up Strategy Tester
Once you’ve logged into MT 5, you can call out Strategy Tester via the top toolbar. Click View, then Strategy Tester. Alternatively, type Ctrl + R.
This will bring up the Strategy Tester window. You can click and drag the window to move it to a more suitable position on your screen. In the screenshot below, Strategy Tester has been moved to the top, as marked by the red rectangle.
Here’s a zoomed in shot of Strategy Tester.
As you can see, there are multiple icons that help you operate Strategy Tester. Traders can perform different backtests by clicking on the relevant icon.
There are also several tabs at the bottom of the window. Here’s what each of them does:
- Overview. This tab displays all the different backtests available.
- Settings. This tab allows you to select your EA, asset symbol, time period and other factors.
- Inputs. This tab displays different parameters reflecting the chosen EA.
- Agents. This tab shows you how many cores in your CPU are available for testing. You can also opt for MQL5 cloud network if you want to run the backtest using cloud computing.
- Journal. This tab displays your trading activities and events.
For the purposes of this article, we will only be going through the Single type of backtesting.
Start your first test: Single backtest on MT 5
From the Overview Tab, click on Single. This will switch you to the Settings tab, shown below.
Here’s a rundown of what each of these fields do, from top to bottom.
- Expert: Select the EA you want to test. You can choose from the EAs already pre-installed, or add one that you’ve purchased from online sources, such as the MetaTrader Market [4].
- Symbol: Here you can select the symbol you want to backtest. Here, we’ve selected the EUR/USD currency pair.
- Timeframe: Right next to the Symbol field is where you can select the timeframe you want to backtest.
- Dollar icon: Clicking on this will display the details of the asset you have selected.
- Date: Select from preset date ranges: Entire history of the asset or currency pair, Last month only, Last year only, or Custom period
- If you select Custom period, use both date dropdowns on the right to select the starting date and end date of your test.
- Forward: Select this if you want to perform forward testing on your selected time period (see above). Enabling forward testing creates an additional tab that shows the forward testing results at the end of your test. Note the following terms in the Forward dropdown:
- No: Forward testing is not used.
- 1/2: Use half of the specified time period for forward test.
- 1/3: Use one-third of the specified time period for forward test.
- 1/4: Use one-quarter of the time period for forward test.
rCustom: Use the dropdown to specify when the forward test should start.
- Delays: Select from several delay times to simulate delays between entering your trade and the trade being executed. The degree to which this happens in the real world depends on the speed and connectivity provided by your online broker. Long delays can result in higher slippage, negatively impacting your trade.
- Modeling: Select how ticks are generated, according to whether you want a faster but less accurate backtest, or a higher-accuracy test that would take longer.
- Deposit: Here is where you can enter your initial trade deposit and deposit currency. Allows you to run the test according to the capital you plan to put up.
- Leverage: Enter the leverage you plan to use when trading live.
- Optimisation: Here you can further finetune your backtest by getting Strategy Tester to run through a series of values for each of your settings until it finds the optimal ones. This is best left to advanced users, so we’ll ignore this for now.
- Visual mode: Next to the Optimisation field is an option called Visual mode. Tick it if you want to watch your backtest in action in the Strategy Tester Visualisation Box.
Start your backtest
Once you’ve entered the relevant information in the fields above, you’re ready to start your test. To do so, click the green Start button in the right hand corner.
Provided you’ve checked the Visual mode option, a new window will pop up. This is the Strategy Tester Visualiser (screenshot below) which displays your test as it takes place.
Once the test completes, you can click and drag the chart in black to reverse and forward through it. You can also use the Left and Right arrow keys for prolonged scrolling.
This screen lets you see how your chosen EA performed on the selected currency pair as the test progresses.
If you did not tick the Visual mode option, you can find the test in the main window of MT 5, as shown below. Here, you can use the mouse or arrow keys to navigate the chart as well.
Review your results
Once your test has been completed, you will find a new tab in Strategy Tester. This is the Backtest tab, and you can click on it to bring up detailed information about your backtest.
The top left corner gives you what is probably the most essential information about your results – your total net profit or loss. This can tell you at a glance whether your chosen EA succeeded or failed.
There’s a lot of information to be found, so make sure to scroll down to reveal them. Some additional data generated by your backtest include various graphs that show you information on profits and losses, trade entries by day, etc.
Once you’ve finished reviewing your test, you can go ahead and start another backtest, choosing a different EA and other parameters to see how your results would change.
Past tests will be saved in the Overview tab in Strategy Tester, and you can click on them to make adjustments and run a new backtest.
Conclusion
Backtesting is a powerful tool for traders to determine the right EAs and strategies for various assets. With the ability to model real world results based on historical data, backtesting is capable of providing a high degree of clarity and good quality data that is essential for gaining an edge when trading.
Join Vantage to unlock the power of backtesting with MT5. Explore various advanced trading bots and automate your trades with proven EAs. Expand your trading capabilities and increase confidence when you backtest your ideas to reduce risk and enhance profit potential. Sign up today!
References
- “Backtesting: Definition, How It Works, and Downsides – Investopedia” . https://www.investopedia.com/terms/b/backtesting.asp . Accessed 19 December 2024.
- “Backtesting – CFI”. Accessed 19 December 2024. https://corporatefinanceinstitute.com/resources/data-science/backtesting/. Accessed 19 Dec 2024.
- “Trading Strategy Tester – MetaTrader 5”. https://www.metatrader5.com/en/automated-trading/strategy-tester. Accessed 19 December 2024.
- “MetaTrader Market – MetaTrader 5”. https://www.metatrader5.com/en/automated-trading/mql5market. Accessed 19 December 2024.