Trading

Guides

Updated Frequently


High Accuracy Testing & Optimization on MetaTrader 4 & 5

October 19, 2020
high-accuracy-testing-optimization-on-metatrader-4-metatrader-5-mt4-mt5-alphaheim.jpg

In order to perform optimizations and vet a potential trading strategy, it is imperative to ensure that the results obtained from strategy tests and optimizations on MetaTrader 4 & 5 are a true reflection of the EA. As seen in the image below, the default strategy tester for MT4 causes issues when testing scalping EAs. In the first image we use the standard tester, with default data, and in the second we port over to tick data. The initial perception is that of a flawless strategy – but this is simply derived from poor data analysis. In this article we will cover the steps to ensure accurate testing on both MT4 and MT5, and touch on some elementary data science to help filter potential strategies.

 

Broker Tick Data vs. Imported Tick Data

Imported Broker Forex Tick Data for MetaTrader 4 and MetaTrader 5 from Tick Data Suite

On MT4, back-testing on “Every tick” with a default environment is the highest accuracy possible when performing optimizations. This will deliver a similar result on MT5, but MT5 does offer a more accurate modelling method, namely “Every tick based on real ticks”.

The utilisation of “Every tick” modelling causes a variety of issues. Prices that are randomly simulated from bar data when the default tick data is downloaded from the broker through MetaTrader’s Strategy Tester. By a process of interpolation, it uses the bar price data together with the tick count to generate the prices for each bar so that they start at the bar open price, touch the bar high and low, ending at the close price.

Similarly, on MT5, when testing or performing optimizations on “Every tick based on real ticks”, and requesting the tick data from the broker directly, there is a scope for missing data. If there are numerous data mismatches, this could yet again lead to an improper testing result.

To summarise, the default testing environment will not cycle through every tick that was delivered at the specific points in history. This will be a lesser issue for higher timeframe EAs, but for scalping EAs, as in the image above, the results will divergence extensively.

Imported Tick Data vs Broker Tick Data for Backtesting and Optimizations on MetaTrader 4 and 5

 

Where to Download Quality Tick Data

Tick Story vs Tick Data Suite vs Tick Data Manager vs Custom Symbol Import on MetaTrader 4 MT4 and MetaTrader 5 MT5

The two third-party tick providers (intermediaries) allow for a user-friendly method for accurate testing with relatively low costs.

The steps below will outline how to utilise Tick Data Suite (TDS) for the entire process. A similar process can be followed for Tick Story.

TDS is a paid service, but does allow for a 14-Day free trial, available here.

Following the trial period, if you would like to sign up with a 10% discount, you may purchase through this link.

 

Download Tick Data

Once TDS has been downloaded, run “Tick Data Manager” from the Windows start menu.

Choosing the correct source is imperative for realistic results, and will vary according to the specific broker and trading account type. Before downloading years of data, it is good practice to download the most recent week, and take any arbitrary bars on the chart, and check closeness to the data provided on the terminal from your real account from your broker. The spreads should also be checked for closeness, and if using volumes, closeness of tick volumes too.

  1. Choose the source (broker)
    • Dukascopy has almost every forex, commodity, stock/equity, bonds and more available as CFDs
    • Alpari ECN1 and Alpari ProECN are both ideal for users who are planning on live trading on ECN accounts (orderbook).
    • The “Add new…” option will be covered in a future article for specific enthusiasts who plan on importing custom tick data to Tick Data Suite. This is useful for scenarios involving raw tick data from cypto exchanges such as Binance or Bitmex (applicable for Order Snipe users and subscribers).
  2. Click the download button on the far right of each desired pair’s row. Users can also scroll between different sources to download pairs that may be exclusive on specific sources/brokers.

How to Import Tick Data onto MetaTrader 5 with Custom Symbols for Optimizations and Backtests High Quality

 

Importing Tick Data on MetaTrader 4

MetaTrader 4 users are not required to import or export bars and ticks since Tick Data Suite integrates directly with MetaTrader 4. Users on MetaTrader 5 must import and export both bars and ticks which has both its pros and cons for many users.

How to Import Tick Data on MetaTrader 4 MT4 Tutorial for Backtesting and Optimizations High Accuracy

Once installation is complete, restart your MetaTrader 4 terminal then open the Strategy Tester and perform the following steps:

  1. Select the Symbol (in our example we used EURUSD).
  2. For Model select “Every tick “(the most precise method based on…)”.
  3. Click the checkbox for “Use tick data”.
  4. Then click on Forex Tick Data Download Free
  5. The following popup will appear with a minimum of 4 inputs to configure:
    • How to Import High Quality Tick Data on MetaTrader 4 MT4 Tutorial for Backtesting and Optimizations High Accuracy
      • Source: Select the source that has the pair which you downloaded on Tick Data Manager.
      • Symbol: Select the same symbol that was used on the main Strategy Tester (previous image).
      • GMT offset: Check your broker, for example IC Markets is +2.
      • DST: Check with your broker, for example IC Markets DST is based on US time.
  6. Click OK and you’re all done. There is no need to import or export tick and bar data files on MetaTrader 4.

 

Importing Tick Data on MetaTrader 5

Users on MetaTrader 5 must import and export both bars and ticks which has both its pros (sets can be saved and imported on other machines) and cons (manual update when adding/updating ticks). The following guides in this section are only applicable for MetaTrader 5 users since Tick Data Suite integrates directly with MT4.

 

Exporting Tick Data From Tick Data Suite

Exporting Bars from Tick Data Suite to MetaTrader 5

How to Import OHLC Data on MetaTrader 4 and MetaTrader 5 for Backtests and Optimizations

  1. Click on the Tick Data Suite Tick Data Manager for MetaTrader 4 MetaTrader 5 button on the far right side of a specific pair.
  2. On the popup window click on the Export Bars Tab Tick Data Suite for MetaTrader 4 and 5 tab.
  3. Adjust the following parameters:
    • Format: MetaTrader
    • GMT offset: Check your broker, for example IC Markets is +2
    • DST: Check with your broker, for example IC Markets DST is based on US time
  4. Click on Download Forex Tick Data For MetaTrader
    • Then select the M1 timeframe (see example below)
      • Export Bars Tick Data Suite MT4 MT5 Tick Data OHLC

  5. Click on Forex Tick Data Download Free

 

Exporting Ticks from Tick Data Suite to MetaTrader 5

How to Import Tick Data on MetaTrader 4 MT4 and MetaTrader 5 MT5 for Backtests and Optimizations alphaheim Tutorial

  1. Click on the Tick Data Suite Tick Data Manager for MetaTrader 4 MetaTrader 5 button on the far right side of a specific pair.
  2. On the popup window click on the Export Tick Data Manager Export Bars Tick Data Suite MT5 MT4 Tick Data OHLC tab.
  3. Adjust the following parameters:
    • Format: MetaTrader 5
    • GMT offset: Check your broker, for example IC Markets is +2
    • DST: Check with your broker, for example IC Markets DST is based on US time
  4. Click on Export Tick Data Suite Export Bars Tick Data Suite MT5 MT4 Tick Data OHLC

 

Creating a Custom Symbol on MetaTrader 5

  1. Right click on the Market Watch window and click on “Symbols”
    • Symbols Importing Tick and Bars on MetaTrader Tick Data OHLC
  2. Click “Create Custom Symbol”.
    • Create Custom Symbols MT5
  3. Within the Custom Symbol window, do the following in order:
    1. Use the following as a reference for the next few points:
      • Custom Symbol Tick Data on MetaTrader Forex Crypto Import
    2. On the Copy From: field, select the symbol that you would like to use.
      • Note: this will copy EURUSD symbol settings from the current broker.
    3. When the symbol is inputted and loaded within the column, rename the pair to have _TDS
      • Adding more than 4 characters at the end of the custom symbol (i.e. PAIR_123) will result in issues on strategy tester.
    4. Click “OK” when all symbol settings have been confirmed.

Importing Bar & OHLC Data on MetaTrader 5

  1. Now click on the Symbols dropdown input and select the new custom pair you created (under the Custom category within the dropdown).
      • Once the new custom symbol has been selected, make sure that the timeframe input beside it is set to M1.
      • Then click on “Import Bars”
      • Tick Data for Backtesting on MetaTrader 4 MetaTrader 5 Optimization
    1. Click on “Browse”.
      • Download High Quality Tick Data Real Ticks
    2. Select the bar export file. You will be able to distinguish the difference since the file suffix will contain _M1.csv.
      • In our example this is the distinguishing factor: EURUSD_GMT+2_US-DST_M1.csv
        • Download Forex Bar Data OHLC Tick Data for MetaTrader 5 MetaTrader 4 MT5 MT4
    3. Once the import is finished, click “OK”.
      • Crypto OHLC and Tick Data for MetaTrader 5 MetaTrader 4 Download Volume

 

Importing Tick Data on MetaTrader 5

  1. Once importing bar data is complete, navigate over to the “Ticks” tab.
    • Alpari Forex Tick Data for MetaTrader 5 MetaTrader 4 Download Free
  2. Make sure that the custom symbol is inputted on the symbol input field. Then click on “Import Bars”.
    • How to Import Tick Data on MetaTrader 5 MT5 MetaTrader 4 MT4
  3. Click on “Browse”.
    • Import Forex Tick Data on MetaTrader 4 Tick Data
  4.  Select the tick export file. You will be able to distinguish the difference since the file suffix will not contain _M1.csv.
    • In our example this is the file we used: EURUSD_GMT+2_US-DST.csv
    • Download Forex Bar Data OHLC Tick Data for MetaTrader 5 MetaTrader 4 MT5 MT4
  5.  Once the import is finished, click “OK”.
    • Download and Import Forex Tick Data for MetaTrader 4 and MetaTrader 5
  6. Click “OK”.
    • How to Import Tick Data on MT5 and MetaTrader 4 MT4
  7. Go to the Symbols window once more and make sure the new custom chart is activated.
    • If the chart shows a white dollar sign icon, double click to make it enabled.
      • Disabled chart: Tick Data Import for MetaTrader MT4 MT5 Free Tick Data
      • Enabled chart: Import High Quality Tick Data on MetaTrader MT5 MT4 MetaTrader 4 and MetaTrader 5
  8. Click OK and you’re all done.
  • Tick Data Download for MetaTrader Forex and CFD Pairs

 

Spread, Delays and Slippage

The vast majority of brokers utilise variable spreads, and this should be mirrored within the testing environment for optimizations. For optimizations on MetaTrader 5, this is automatically handled when importing the data. For optimizations on MetaTrader 4, the utilisation of variable spreads can be done from within the testing window under “Tick data settings” and checking the “Use variable spreads” box.

The trading delay is the time reported in the bottom right of your terminal. This figure can be reduced, and should be under 20ms for users utilising a VPS close to the trading server of a given broker. Regardless of this value, the identical value, on average, should be mirrored within the tester. This is not possible in MT4, but for MT5 users, under the Strategy Tester window, the delay can be specified. This will allow for a more realistic assessment of strategies that enter during periods of high volatility.

Slippage is a factor less relevant for higher timeframe strategies, but is good practice to emulate. Slippage is referred to as the difference between the intended entry price and the actual entry price. As this is never confirmed, the testing environment can only randomly allocate slippages. To enable for MT4, follow the same steps for variable slippage, but check the “Enable slippage” box instead. For MT5, adjacent to the “Delays” input you can vary the settings for slippage emulation.

 

MetaQuotes vs. Broker MetaTrader Terminal

There are notable differences between the MetaTrader terminal provided by MetaQuotes from their website, and the counterpart delivered by your specific broker.

For example, the symbol properties, i.e. the trading conditions such as spread, trading times and many others, are predefined upon download. These predefinitions are likely to be different from that which is directly relevant to your broker.

It is possible to match the properties manually, such that the environment downloaded from MetaQuotes matches your individual broker, but the easiest and most reliable methodology would follow from a direct download from your broker.

 

Testing on Demo Accounts

It is common practice to test a strategy on a demo account prior to release on a real account. Whilst this is a respectable precaution, there should be an additional testing phase on the real trading account.

This is primarily due to the different order book structure (LOB) of the real and demo environments. There will be slight variations in price and volume, which may alter the outcome of your strategy. For this reason, it is recommended to trial a strategy on a real account before increasing the capital.

 

Remarks on Modelling (Testing & Optimizations)

 

Overfitting

There is a statistical argument that financial markets follow a random walk, but if you look for a specific pattern, at some point, you will find one. This is especially relevant within EAs utilising deep neural nets during optimizations on MetaTrader 5, for which the training set can provide an unrealistic view of projected performance.

One way of reducing the risk of overfitting is to split to testing period into a back-testing period and a forward-testing period (walk forward test). This will allow for a strategy to optimise itself over the given back-testing period, and treat the walk forward test as new information. In MetaTrader 4 optimizations, this split must be performed manually, but for MetaTrader 5 optimizations, this can be easily achieved by setting the “Forward” input from the Strategy Tester window.

Following the back-test and walk-forward, it would be good practice to favour results with consistency as opposed to excessive returns in either period. For example, if the given EA is utilising dynamic risk management, then assessing average monthly/yearly returns would be a good starting point. Similarly, the maximum drawdown should be consistent between both results.

This is simply the key for ensuring that optimizations on MetaTrader 4 & 5 are performed properly.

 

Sample size

For any given test, it is crucial to apply a weight to the number of trades within the test. For example, for any two results, with different number of trades, but all other conditions remain constant, it would be sensible to favour the result with a greater number of trades.

 

Calmar Ratio

There are a variety of useful statistical measures for portfolio analysis. Beyond the return and drawdown, it is sensible to consider the alpha, beta, Sharpe ratio and various other metrics. A measure we like to assess is the Calmar ratio, calculated as below. This allows for an interpretation of the risk weighted performance of the test. The interpretation of the result largely depends on the specific risk appetite. We do not generally consider any results less than 1, and generally seek results greater than 5.

 

Links for Backtesting & Optimizations on MetaTrader

Medium Link for this Optimization on MetaTrader Article
14 Day Free Trial for High Quality Tick Data from TDS
10% alphaheim Exclusive Discount for Tick Data Suite
All alphaeim EAs, Indicators & Dashboards
alphaheim Trading Tools for MetaTrader

 

MetaTrader Trading Indicators & EAs

For a collection of our indicators and EAs, check out the links below:
Trend Bias Indicator
Dual Grid Expert Advisor
Triangular Moving Average Indicator
Trend Divergence Indicator
Volume Insight & Divergence Indicator
Dynamic Trend Duo Expert Advisor
All alphaeim EAs, Indicators & Dashboards
alphaheim Trading Tools for MetaTrader