Reliable backtesting with MetaTrader 4

Backtesting Defined

The term “backtest” in this context means using past data to find out the way your trading system would have functioned. In other terms; to assess whether the software could have generated loss or profit in the previous data.

It is good to note that when someone does a back test, he/she assumes that the future trends will be similar to past ones. Though please keep in mind that this is known to have both advantages and disadvantages.

There are several factors that reduce the accuracy and reliability of a backtest. They include factors that limit the performance of MetaTrader Strategy Tester (MetaTrader 4’s accessory program), and optimizing excessively to available data to the extent it prevents adjusting to new data (over-fitting).

There are several known ways to avoid getting into the trap of over-fitting, which will be discussed in separate articles. For now, let’s move on and take the first step while keeping in mind that backtesting is not the perfect solution.

Get Historical Data

The first thing you need when you want to run a backtest is the historical data of price changes in the Forex market.

Fortunately, FXDD provides the data free of charge (only that of 2005 afterwards is available) here: MetaTrader 1-Minute Data

The link opens a page called “MetaTrader 1-Minute data”, where historical data of various currency pairs (e.g. EURUSD and USDJPY) in 1-minute period is available. As you can generate historical data in different timeframes, such as 5-minutes and 1-hour, 1-minute data is all you need to download from the broker.

I would like us to download a 1-minute period data for USDJPY.

Upon extraction of this (downloaded) data, you will see a new file with the name “M1_USDJPY.hst”.

We’ll extract the file to some location on the computer before importing to MetaTrader 4.

As of the time of writing this article, historical data files of MetaTrader 4 are available in Bid prices only. Strategy tester automatically applies spread (Ask – Bid) to generate virtual Ask prices during backtesting. 

Setting Up MetaTrader 4

To improve the accuracy and reliability of the back test via “Meta Trader Strategy Tester”, it is recommended that you use sufficient amount of reliable historical data. The setup instructions given below will enable you to use most of the data.

  1. Open the MetaTrader terminal and click “Tools > Option” (Ctrl+O).
  2. Wait for the option dialogue to be displayed; open the Charts tab; and enter a very large number e.g. 99999999999 both in “Max bars in history” and “Max bars in chart” columns.

image1

This action results in the amount of data involved in the back test being automatically set to the largest available in the program.

(Note that MetaTrader 4 automatically replaces extraordinarily large numbers such as 99999999999 to the largest number available in it).

Import Past Data to MetaTrader 4

At this stage we already have data to use, and should import them into the program ‘MetaTrader 4’. Select “Tools > History Center” (F2 Key) and open History Center.

image2

Next click the import button below it so that the import window can open.

Click on “Browse” on the top right hand side of the window, select the file you had extracted on your computer, and click OK to save changes and close the window.

Please restart Meta Trader before proceeding to the next step!

Open the 1-Minute Period Data after Import

In the aforementioned processes we imported the 1-minute period chart to our Program. Our next step is “displaying our data on a chart”; this entails a few steps as outlined below.

Open Meta Trader and click on the “File” tab on the upper left side then then click “Open Offline” i.e. “File>Open Offline”. Wait for a window similar to the one below to be opened.

image3

Select “USDJPYecn, M1” from the first column of the table.

Provided this data is imported in the correct manner, the data contained therein should be one for 2005 and later.

If you notice that some data is missing, try restarting the program as this is likely to solve this issue.

Upon clicking the “Open” button, all 1-minute period chart data for the year 2005 and later will be shown on the chart.

Convert 1-minite Data to Timeframe of Your Choice

As most of you will not trade in 1-minute timeframe, you will have to convert the data to 30-minute period or any other period of your choice. For the purpose of illustration, we will derive 5-minute period data from the 1-minute USDJPYecn data. In the whole of this process we will use a script named “Period converter” – an inbuilt feature on Meta Trader.

image4

Below is a brief outline of this process:

  1. On the left hand side, click “Navigator” then “Script”
  2. Navigate the list below “Script” and double-click “period_converter”
  3. Now click “Inputs” (the third tab on this window), double click on the “Period multiplier factor”, and type “5” on the “Value” column to change from 1-minute period to 5-minute period. (If you want a 15-minute period – type 15, if you want a 30-minute period – type 30 etc.)
  4. In order to save and effect the changes click “OK”

Once you are through with the conversion, click “File>Choose” in order to display “Open Offline Chart”

image5

Examine the data to ensure that the data for “USDJPYecn, M5” is for 2014 and afterwards.

To open and view this file; double click it, or click once to select and then and click the “Open” button on the upper right part.

If you plan to test your strategy in multiple timeframes for the same currency symbol, please consider using PeriodAllConverter. This simple free script generates historical data in all timeframes available on MT4.

Run Strategy Tester

You have to utilize/deploy the “MetaTrader Strategy Tester” function when back testing using Meta Trade Strategy Tester.

To start the function, type “Ctl+R”, or go to the upper right side of the program, select “View” and a drop down list will appear from which you need to select “Strategy Tester”.

You will see a page similar to the one in the image show below:

image6

 

  1. Click once on “Strategy Tester” for the lower part to appear
  2. There are two strategies to test; select the one you would like to test – in this case Expert Advisor. In our case we would like to use “MACD Sample” accessory
  3. Next step is determining the Symbol (for the currency pair). Under normal circumstances, you will see a currency pair similar to the one in the active chart’s currency pair; otherwise, you will have to select manually
  4. Model: Each tick will give better/more realistic simulation results
  5. From, to: This button enables you to choose terms (in date format e.g. 2005.12.10) for testing strategies
  6. Period: Choose the period. Under usual conditions, the period displayed is same to that of an active chart
  7. Expert properties: This button enables you to change the parameter of the strategies through clicking it. But for now there is no need for changing the “Expert Properties” because we will operate it by its default settings
  8. Lastly, go to the lower left hand side of the page and click the “Start” button to perform the test. You have to wait for a while for the results of the test to be processed

Check the Test Result

In the graph below we see tracks that are made when someone deals a “$1000” with a “MACD Sample”.

image7

 

We may still make some improvements on this program. As you see, trading approximately 180 times has resulted in reduction of the initial deposit to $100. It is clear that graphs are reliable materials for judging the test results.

Our next stage is opening the “Report” tab:

image8

While it is relatively hard to notice in the upper image, here we see “-903.31” displayed in the fourth row (Total net profit). This implies that using this strategy in the same term would lead to a deficit of $903.31. Of course this screen has a number of other useful indicators e.g. drawdown; but we won’t be dealing with those now.

Note that every single trade is normally displayed on the “Result tab”. If you would like to view remarks for the trades, please check those in the “Journal tab”.

Initial Deposit and Expert Properties

The second part of this page has an example where an individual moves and invests 1000USD in MACD Sample. If you want to know how the initial deposit was, go to “Expert Properties” and fix the value.

Open “Expert Properties” window> “Testing Tab” then go to “Initial Deposit” column and fill in the amount of deposit.