Popular Posts

Editor'S Choice - 2019

How to Test an Expert Advisor in MT4 Tester - Detailed Instructions

Hello! Mechanical trading systems are as old as markets. With the development of computer technology and the Internet in the 20th century, it became possible to trade without leaving home, and in the beginning of the 21st century, with the advent of the MetaTrader platform, it was also in automatic mode. The resources of a modern desktop computer allow you to implement any, even the most complex algorithms, and the MetaEditor editor built into the MetaTrader terminal makes it possible to write a robot even to a person who is not familiar with programming. As a result, the Forex market is filled with various offers to buy miracle advisors and some of them are really worthy of attention. But how to understand whether it is worth using one or another Forex advisor on real accounts? Today I will tell you how to test a trading robot on historical data using the MetaTrader 4 program.

Test preparation

Today we will not analyze how to install the adviser in the terminal - for this there is a corresponding blog article. We assume that we have already installed the adviser. Now you need to think about the quotes that you will use. Most brokers do not have their own historical base, with the exception of Alpari and Ducascopy, while the rest use quotes provided by MetaQuotes. I don’t presume to say that these quotes are generally suitable for tests - they are of very poor quality (many gaps, errors and inaccuracies). How to download quotes from the company Ducascopy is the topic of a separate article, moreover, it is not so easy to make a beginner. Therefore, for expert advisor tests, we download the terminal from Alpari. Attention! To get access to the historical base of Alpari quotes, in the terminal you must be connected to a real account! Recently, this broker does not provide its quotation base for demo account owners.

It is because of differences in quotes that the tests of the same Expert Advisor for the same pair with the same settings and all other things being equal will most likely differ, sometimes very much.

First, we need to configure something, for which we go to the Tools -> Settings tab or press Ctrl + O

A window with the terminal settings will appear:

We select the Charts tab in the columns “Max. Bars of history” and “Max. Bars in the window” and fill them as in the picture above (by default there are 65,000 bars).

In order for the quotes for the pair we need to become available in the terminal to conduct a test on them, open the tab Service -> Archive of quotes or press F2.

The following window opens:

We select the pair we need and the period M1 and clicks the download button. After a while, the quotes will load, turn off the terminal and turn it on again. We go back to the archive, left-click several times on the M1 period of the pair we need until the gray battery shown before the period lights up in yellow-green. It remains to click the rest of the periods with the mouse so that the quotes are calculated for them. If you want to test the adviser on several currency pairs, upload quotes of the required currency pairs. Close the terminal and open it again. Then again enter the quotes archive and go through all the periods of the pair you need, several times left-clicking on each of them. All these shamanistic actions are needed in the latest versions of the terminal, since quotes are often loaded incorrectly. At this preparatory stage is completed.

Terminal tester. Main functionality

So, to start testing the adviser, open the strategy tester or press Ctrl + R.

The following panel will appear below in the terminal:

Let's dwell on each function in more detail.

The first thing you will see in the upper left of the panel is the adviser-indicator switch:

The new terminal builds now have the opportunity to look at the indicator in visual mode (which will be discussed below). I must say that this opportunity was before, but unofficially. Now, a separate button is assigned for testing indicators.

So, choose an adviser.

Under the number 1, we have a drop-down list with advisers available for testing. Here you will find only those advisers that are downloaded to your terminal. Number 2 - a drop-down list of currency pairs, select the one you need. Do not forget to upload quotes for her in the quotes archive. If you suddenly could not find the pair you need in the list, although you are sure that the broker is available for trading, turn on the market overview or press Ctrl + M:

Next, right-click directly in the navigator window and click "Show all characters":

At point 3, we dwell a little more. Here we can choose the testing model we need. For high-quality testing of a trading strategy, it is important to choose an adequate way of modeling the development of price bars. There are three options available:

- At opening prices (a quick method on mature bars, only for advisers with explicit control over the opening of bars)

Uses a rough estimate of strategy. For each candlestick, only one tick is generated. Dignity is the fastest way to verify. In this mode, the opening of the bar is modeled first (Open = High = Low = Close, Volume = 1), which allows the expert to accurately identify the end of the formation of the previous price bar. It is on this nascent bar that expert testing is launched. At the next step, a fully formed current bar is issued.

- Control points (a very crude method based on the nearest smaller timeframe, the results cannot be taken into account)

The control point modeling method is intended for a rough assessment of experts trading inside the bar. This method requires the availability of historical data from the nearest smaller timeframe. In some cases, the available data of a smaller timeframe does not completely cover the time range of the tested timeframe. In the absence of data of a shorter timeframe, bar development is generated based on predefined wave patterns.
As soon as historical data of a shorter timeframe appears, interpolation is already applied to this data. However, the accurately existing OHLC prices of the smaller timeframe act as milestones. In most cases, the test results of experts using the control point method can be taken into account only as estimates, and not as final. Such results are of an intermediate evaluative nature.

- All ticks (the most accurate method based on all available smaller timeframes)

This mode allows you to most accurately simulate the price movement inside the bar. In contrast to the “control points”, the tick-based method uses data not only for the nearest smaller timeframe, but also for all available smaller timeframes for generation. At the same time, if more than one timeframe data simultaneously exists on a certain time range, then data of the smallest timeframe are used for generation. As in the previous method, control points are generated based on the OHLC data of the smallest available timeframe. Interpolation based on predefined patterns is also used to generate price movements between control points, so minute data covering the entire testing range is highly desirable. A situation is possible when several identical ticks are generated in a row. In this case, duplicate quotes are filtered, and the volume of the last of such quotes is fixed.

When testing for all ticks, the volume of generated ticks can be quite large, so the terminal can consume quite a lot of resources.

To test the adviser, we always choose the method of all ticks. Yes, this is the slowest, but also the most reliable method. Many people use the control of closing the bar in their advisers, that is, they specially wait for the moment of opening a new candle and opening orders is made only at that moment. But often advisers use stops, take, trawls, which can work at any time inside the candle. Using the method at opening prices is possible only for advisers who do not use trailing stop, stop loss and take profit, but open and close positions at the time of opening a new candle, and there are very few such advisers.

Point 4 - use the date. We put a tick and select the desired start and end dates for testing. If the check box is not checked, testing is carried out throughout the history of quotes downloaded to the terminal. The tester will not be able to test on a period for which there are no quotes in the quotes archive, that is, you will not be able to do a test since 1300 if you do not have quotes for this period.

Point 5 is a visualization, which we will talk about later.

Settings on the tester panel on the right:

Period - select a period for testing the adviser. Available periods up to D1. W1 and MN1 are not available for testing. In addition, if you have not downloaded the quotes history of the desired period, you will not be able to perform the test.

Spread - you can set any value or use the current spread for the pair. This has been done for convenience - for example, at night and on weekends the current spread is usually overpriced and if you are testing an adviser at this time it makes sense to set the spread manually. If you have selected the current spread, the test results can vary greatly depending on the time of day and day of the week, especially when testing on all ticks.

The "Change Expert" button is only available if you have the source code of an adviser (file with the mq4 extension). It opens the advisor code editor where you can make the necessary changes to the adviser.

The "Open Chart" button opens a chart with indicators and deals applied on it made by the adviser during the test (you can click after the test is completed).

Symbol Properties Button

You can’t change anything here, this is just reference information on the currency pair used.

Expert Advisor Button

By clicking on the button, you will see the window shown at the top. Three tabs are available: Test, Input, and Optimization.

Test tab.

Here you can enter the deposit used for the test and the deposit currency. Also, if you wish, you can choose the direction of transactions, for example, allow the expert to trade only in purchases or only in sales. Optimization settings will not be considered in this article. As well as the tabs "Optimization".

Input Tab

Here are all the control variables of the expert himself, his settings. By the way, the window is scalable - if you drag the lower right corner with the mouse, you can increase or decrease it in size. Together with experts, usually files with settings that have the * .set extension are usually delivered. Moreover, most often for each pair there is its own configuration file. To download the correct settings for the desired pair, click the "Download" button and select the desired file. Often, after installing the expert in the terminal, they are not in the right folder. After clicking on the "Download" button, we find ourselves in the tester folder (I have it C: Users Silentspec AppData Roaming MetaQuotes Terminal FE03BE71CD8F9E8F4C70E0FDAFC997E5 tester). If the necessary files were not there, go to the FE03BE71CD8F9E8F4C70E0FDAFC997E5 MQL4 Presets folder, most likely the files are there. So, select and load the desired configuration file. After loading, we need to find the advisor's money management parameters and set a fixed lot of 0.1 - in this case, each dollar of profit or loss will be equal to 1 old point. What is it for - I will tell below.

Testing advisor. Test results

Now we are ready for the test. Check the settings again and press the "Start" button. After some time, the test will be performed, which we will be notified by a sound signal, similar to that issued by a rubber children's toy with a squeaker.

It is time to look into the lower left corner of the tester:

Here we can notice the tabs "Settings", "Results", "Graph", "Report" and "Log".

In the "Results" tab, a list of all transactions made by the adviser during the test is available.

On the "Chart" tab, you can admire the profitability curve of the adviser.

If the adviser has not completed a single transaction, you should look at the "Journal" tab. In it you will find a description of everything that happened during the test. It is likely that there is some kind of error in the adviser. The decoding of the error number can be found in the Error Codes section.

In the "Report" tab, all the statistics of the expert’s work on the selected period of time are available:

Bars in history - the number of bars in history, shows the depth of the history on the basis of which the simulation was performed.

Ticks modeled - the number of modeled ticks, shows the size of the modeled sequence. Each sequence record represents a bar status (OHLCV) at a given point in time. Depending on the timeframe, modeling method, and on the availability of historical data of smaller timeframes within the bar, a different number of bar states can be modeled.

The quality of the simulation is the quality of the simulation.

Chart mismatch errors - errors that occur when modeling ticks for different timeframes. If there is at least one such error, delete the entire history from the terminal and download again. You can delete it like this: File -> Open the data directory -> A window opens with the terminal folder -> history folder -> Select the type of account we need (the one you are currently using) -> Close the terminal and delete all files with the extension *. hst. Next, upload quotes again in the quotes archive.

A socket with a signaling indicator of the quality of quotes (I have it green, so for example I found it on the Internet):

Gray indicates missing quotes, red only quotes for the current period, green indicates quotes for previous periods, and the brighter the green, the lower the periods available. With the availability of the M1 period, the indicator will be like mine - bright green.

Initial deposit - the deposit with which testing was conducted.

Spread - the spread with which testing was conducted.

Total profit - how much was earned during the work of the adviser

Total loss - how much has been lost.

Net profit - profit that was earned by an expert for a given period. If the test is done by lot 0.1, then this profit in the deposit currency is equal to the number of old points earned. The same is true for all other parameters indicated in the currency. Net profit = Total profit - Total loss.

Profitability - profitability, shows the relationship between total profit and total loss. It is calculated by the formula Profitability = Total profit / Total loss.

Waiting expectation - the mathematical expectation of winning.

Absolute drawdown is the difference between the initial deposit and the smallest balance value during the testing process.

The maximum drawdown is the maximum difference between one of the local upper extrema of the balance change graph and subsequent lower extrema.

The following figure shows the main stages of the change in the maximum drawdown during testing. The final value of the maximum drawdown is indicated by thickened arrows.

Relative drawdown shows the ratio of the maximum drawdown to the value of the corresponding local upper extremum.

I think that the rest of the test data, such as the average profitable trade, the maximum number of continuous losses and so on, are quite understandable and no explanation is needed here.

If you right-click on a report, you can save this report as an html file:

At the top of the report are the main data on the conditions of the test - period, currency pair, testing model, advisor parameters, etc. Below are the statistics of the test and the graph of the yield curve. Further in the form of a table is a list of all completed transactions.

Render mode

This mode provides the ability to literally see in accelerated mode how the adviser would have worked in the past with those changes in quotes that took place. For example, if the input and output signals of the Expert Advisor are based on the signals of some indicator, then you can set the desired indicator on the visualization chart and then the appearance of transactions and closing of transactions will be even more clear.
In other words, visualization helps to understand and feel the logic of the advisor algorithm, since everything will happen before your eyes.
In addition, visualization is also used when they want to see the nature of the origin of a particular site in the passage of the adviser (the moment the drain begins, or, conversely, the profit period itself).

Having driven the robot in visualization mode, you can understand the principle of its operation and you will know what you can expect from it in the future. This is a very useful tool, especially for advisor developers.


This article examined the main functionality of the strategy tester of the MetaTrader 4 terminal and the features of downloading quotes. We also got acquainted with the advisor test results and the visual testing mode. I want to note that these are just the basics of working with advisers. The advisor testing method discussed in the article is suitable for advisors on periods from H1 and higher. For scalpers working on smaller periods, this testing method is conditionally suitable; it is purely informative. If you are going to earn with the help of advisers, you must also master the optimization of advisers. It will also be useful to gain deeper knowledge about testing and optimization of advisers with higher modeling quality, unavailable, unfortunately, in the standard version of the terminal.

Watch the video: How to test a Metatrader robot Expert Advisor (November 2019).

Leave Your Comment