High Accuracy Testing & Optimization on MetaTrader 4/5
Walkthrough & Detailed Guide for Backtesting & Optimizations
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
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.
Where to Download Quality Tick Data
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.
- 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).
Then 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.
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.
Once installation is complete, restart your MetaTrader 4 terminal then open the Strategy Tester and perform the following steps:
- Select the Symbol (in our example we used EURUSD).
- For Model select “Every tick “(the most precise method based on…)”.
- Click the checkbox for “Use tick data”.
- Then click on “Tick Data settings”.
- The following popup will appear with a minimum of 4 inputs to configure:
- 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.
- 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 Bars from Tick Data Suite to MetaTrader 5
- Click on the “…” button on the far right side of a specific pair:
- On the popup window click on the “Export bars” tab:
- 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
- Click on the “Timeframes” button:
- Then select the M1 timeframe then click “OK” (see example below).
- Click on the “Start export” button.
Exporting Ticks from Tick Data Suite to MetaTrader 5
- Click on the “…” button on the far right side of a specific pair:
- On the popup window click on the “Export ticks” tab:
- 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
- Click on the “Start export” button.
Creating a Custom Symbol on MetaTrader 5
- Right click on the Market Watch window and click on “Symbols”
- Click “Create Custom Symbol”.
- Within the Custom Symbol window, do the following in order:
- Use the following as a reference for the next few points:
- 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.
- 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.
- Click “OK” when all symbol settings have been confirmed.
Importing Bar & OHLC Data on MetaTrader 5
- 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”.
- Click on “Browse”.
- 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
- Once the import is finished, click “OK”.
Importing Tick Data on MetaTrader 5
- Once importing bar data is complete, navigate over to the “Ticks” tab.
- Make sure that the custom symbol is inputted on the symbol input field. Then click on “Import Bars”.
- Click on “Browse”.
- 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
- Click “OK”. Keep all settings at their default values since any offset or formatting would’ve been accounted for from Tick Data Manager.
- Once the import is finished, click “OK”.
- 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:
- Enabled chart:
- Click OK and you’re all done.
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.
MetaTrader Trading Indicators & EAs
To view 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