The article describes in detail the process of testing and optimization of advisers in the tester of the trading platform MetaTrader 4 and MetaTrader 5. The necessity and demand for this kind of material has long been foreseen. Many novice users of the trading platform MetaTrader 4 and MetaTrader 5 do not understand the essence and sequence of actions when working with experts.
Almost every day (without exaggeration) the forum raises questions of novice users - how to install an Expert Advisor in the terminal, how to launch an Expert Advisor in work, what is optimization and how to implement it in the MetaTrader 4 and MetaTrader 5 tester, what is a forward test, P.
The proposed article provides answers to these questions simply and intelligently, and gives an opportunity to go a little more professionally, on a concrete example, to this fascinating work. For further, more detailed acquaintance with the testing and optimization processes, as soon as the material is presented, references are given to the accompanying articles and pages of the MQL4-community site.
The main window of the strategy tester
For testing, we chose our universal trading system The X
The first thing you need to include a strategy tester:
After opening the strategy tester, you see the Strategy Tester tab
Fields for filling:
Expert Advisor - From the list of advisers choose the one you will test.
Symbol - Symbol for testing
Model - Testing Model
For testing, you can choose one of three methods for modeling historical data:
- At the opening prices (fast method on the formed bars)
Some mechanical trading systems do not depend on the features of intra-bar modeling, they trade on the formed bars. The fact that the current price bar has fully formed, you can find out by the appearance of the next. It is for such experts that this simulation mode is intended.
In this mode, the opening of the bar is first modeled (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-generated current bar is issued, but testing is not performed on it!
- Control points (the nearest smaller timeframe is used)
The method of modeling control points is designed for a rough assessment of experts who trade inside the bar. For this method, it is necessary to have historical data of the nearest smaller period (timeframe). In some cases, the data of a smaller timeframe does not completely cover the time range of the timeframe being tested. If there is no data of a smaller timeframe, the development of the bar is generated based on predefined wave patterns, as was the case in the previous, third version of the MetaTrader 3 client terminal.
As soon as the historical data of the smaller timeframe appear, the interpolation is applied already to these data. However, the existing OHLC prices of the smaller timeframe act as control points. In most cases, the results of testing experts by the method of control points can be taken into account only as estimates, and not as final ones. Such results have an intermediate evaluation character.
- All ticks (based on all the smallest available periods)
This mode allows you to most accurately model the price movement within the bar. Unlike "control points", the ticketing method uses not only the nearest smaller timeframe, but also all available smaller timeframes to generate data. In this case, if for a time range there are more than one timeframe data simultaneously, the data of the smallest timeframe is used for generation. Just as in the previous method, control points are generated based on OHLC data of the least available timeframe. To generate price movement between control points, interpolation is also used based on predefined templates, so it is highly desirable to have minute data covering the entire test range. 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 quotations is fixed.
It is necessary to take into account the very large possible volume of generated data. This can affect the consumed resources of the operating system and the speed of testing.
Use Date -
The date range allows you to test the Expert Advisors not on all available data, but only on the selected time interval. It is convenient if necessary to explore a separate part of the historical data. Limiting the date range can be used not only when testing an expert, but also when generating a test sequence of bars (a file of simulated data used for testing). Very often, there is no need to generate data from the whole history, especially in the case of simulation modeling, when the amount of unused data can be very large. Therefore, if the possibility of using a date range was enabled at the initial generation of the testing sequence, then bars outside the specified range are not generated, but simply overwritten into the output sequence. The data is not excluded from the sequence, so that it remains possible to correctly calculate the indicators on the entire history obtained. It should be noted that the first 100 bars are also not generated. This restriction does not depend on the set date range.
To enable the date limit, you must select the "Use dates" checkbox and specify the required values in the "From" and "To" fields. After all the settings have been made, you can press the "Start" button and start testing. After you start testing at the bottom of the window, you can view the estimated time for completing this process.
Visual Mode - Visualization mode allows the user to monitor the test on a separate terminal graph. When this mode is enabled, a visualization window opens in the terminal, in which the schedule and the transactions opened by the advisor are displayed.
Period - Timeframe for testing. If your advisor works on bars or on indicators, you need to choose the timeframe to be used for real trading.
Spread - Test spread. In the MT4 Terminal, the spread can only be fixed. You can set this spread in this field.
Attention : The results will differ from the real account if your real account contains a floating spread.
Attention : I do not recommend choosing spread = 0 because, as with each tesing, the spread will be set from the current spread of the broker, which can be floating.
Optimization - Check this box if you want to optimize the Expert Advisor in the Strategy Tester, but before that you need to configure the Expert Advisor for optimization.
Test parameters selection window
Initial Deposit - The initial balance of your account for testing.
You can specify here the amount that is sufficient for the expert's work.
Positions - You can select the type of items that will be opened. You can only select Purchases or only Sales. If you have chosen only purchases - the adviser will be prohibited from opening sales positions.
Optimization - block for managing the type of optimization, for example, on balance sheet, profit factor, drawdown.
In this tab you configure the expert for testing.
Only the Value field can be configured, the remaining fields are only for optimizing the Expert Advisor.
Attention : If your Expert Advisor has indicator settings and you are going to watch the test in visualization mode, please be careful.
The indicator settings in the Expert Advisor should be the same indicator settings on the chart.
In other words: the Advisor trades on the indicator that is set in him in external variables.
If you decide to plot the indicator on the chart, please set it up the way it was done in the Expert Advisor.
Otherwise, you will have a question: Why is there no signal on the indicator, and the adviser has opened or not opened the deal .
To start testing, press the START button
To monitor the progress of the test, you can open the Results tab and observe the testing
After testing, go to the Reports tab to view the results of testing your Expert Advisor in the Strategy Tester.
In the Graph tab, you can observe a graph of the balance and equity changes during the testing period.
In order to save the test report, go to the Results tab and right-click on the data, you will see the menu for saving the report:
After that, open the Expert Advisor settings and configure the fields for optimization:
Start - The start value of the parameter that you want to optimize.
Step - The step of changing the value of the parameter that you want to optimize.
Stop - The final value of the parameter that you want to optimize.
Be sure to tick before the parameter that you are going to optimize
In total, the optimizer must pass 6 * 6 passes = 36 results.
You can see the optimization results in the Optimization Results tab
After optimization, you can look at the 3D optimization chart:
If you want to get a report on the tested values and the result of optimization, you need to select the right results with the right button and press the Set Inputs Parametr button, after that you can click the START button again and get the full test report.
In order to save the set of settings that you protested, you need to open the settings of the advisor in the strategy tester, and click the SAVE - Save button
In the future, you can download these settings to your Expert Advisor on a real chart.
Testing and optimization in MetaTrader 5
1. Why are the results of the tests of the same name different in different DCs?
- Different test results in different DCs are explained by different quotes. Each broker has its own quotation providers. Hence the difference in the test results.
- Each broker has its spread and its specification of constructs, so the results may differ drastically.