Download MetaTrader 5

Visualize a Strategy in the MetaTrader 5 Tester

11 June 2012, 08:52
MetaQuotes Software Corp.
9 547

We all know the saying "Better to see once than hear a hundred times". You can read various books about Paris or Venice, but based on the mental images you wouldn't have the same feelings as on the evening walk in these fabulous cities. The advantage of visualization can easily be projected on any aspect of our lives, including work in the market, for example, the analysis of price on charts using indicators, and of course, the visualization of strategy testing.

Does everyone know the possibilities of the strategy tester?

As practice shows, not everyone. The advantage of the MetaTrader 5 trading platform over its competitors is not only a more convenient user-friendly interface, ample opportunities of the terminal and MQL5, a multicurrency tester with the possibility of cloud computing using the MQL5 Cloud Network and many other options. The advantage is also that all the tools that a trader needs are available in one place.

The purpose of this article is to show traders one more important feature of all the possibilities of MetaTrader 5 - namely visualization of strategy testing and optimization. Analysis of the behavior of an Expert Advisor on historical data and selection of the best parameters is not only a laborious analysis of figures, trades, balance, equity, etc. Sit down in a chair, make yourself comfortable, put on your "3D" glasses and let's start.

And I think to myself what a wonderful world chart...

When publishing Expert Advisors in the Code Base or the Market, authors usually attach a statistical report on their testing, as well as the balance and equity graphs. However, more interesting charts are available in the statistics on the "Results" tab of the strategy tester:

Charts of testing results

Based on these charts you can analyze the months, days or hours, when your EA trades best, when it actively enters the market. You can also evaluate the performance of the EA in terms of whether profitable positions are closed in proper time, whether the EA "sits out" losses based on the MFE (Maximum Favorable Excursion) and MAE (Maximum Adverse Excursion) distribution graphs:

MFE/MAE distribution charts

Further scrolling down the testing results, you will find another graph:

Distribution of profits and position holding time

The diagram shows the dependence of the position profit on its lifetime. This diagram will be very helpful for the upcoming Automated Trading Championship 2012. One of the ATC rules prohibits scalping. Test your Expert Advisor and make sure it complies with the rules.

A Historical Movie or Visual Testing

One of the revolutionary features of the strategy tester is its visual testing mode. Well, the analysis of deals by dates, plotting of charts and other "interesting" routine procedures constitute quite a complicated process. However, the strategy tester is like giving you a player remote control, you press "Play" and watch a historical movie. You can slow down or speed up the film, stop it and analyze the situation. In real time you see how charts are constructed based on the simulated historical data and how the Expert Advisor responses to the price changes.

The visualizer fully supports multicurrency, like the strategy tester. The Expert Advisor in the video uses 4 currency pairs for trading. All simulated prices are available in the Market Watch and on the charts.

2D Optimization Results

Before we introduce a three-dimensional image that has become quite popular among television manufacturers, have a look at the 2D visualization in the strategy tester. The rectangles that may seem strange at first glance, allow you to see the mutual influence of two optimization parameters on the optimization criterion (in our case it is the maximum balance value). The darker the shade of green, the higher the balance:

2D Optimization Results

We see that the relationship is of a wave-like character, and the maximum result is achieved when using the mean values ​​of the period and shift of the moving average. These are the results of optimization of the Moving Average EA, which is available in the standard distribution pack of the terminal. Even here you can find something interesting.

Now in 3D

A three-dimensional image provides even better visualization options. Below is the same dependence of the two parameters and the final result. You can switch to this advanced mode using the context menu of the Optimization Graph tab.

3D Optimization Results

After the three-dimensional optimization appeared in the MetaTrader 5 Strategy Tester, traders started to post their own examples of visualization of mathematical calculations on the forum:

Three-dimensional graphics are fully interactive - you can zoom in and out, rotate to a convenient angle, etc. An important feature is that any results of testing and optimization can be exported as images or XML/HTML reports.

Visualizing Optimization

And finally welcome - work with optimization results! To process results, the trader had to prepare data, download them and process somewhere. Now you can do it on the spot during optimization! To demonstrate this possibility, we need several header files that implement the simplest examples of such processing.

Download the MQH files attached below and save them in folder MQL5\Include. Take any Expert Advisor and paste this block at the end of its code:

//--- Add a code for working with optimization results
#include <FrameGenerator.mqh>
//--- генератором фреймов
CFrameGenerator fg;
//| Tester function                                                  |
double OnTester()
//--- Insert here your own function for calculating the optimization criterion
   double TesterCritetia=MathAbs(TesterStatistics(STAT_SHARPE_RATIO)*TesterStatistics(STAT_PROFIT));
//--- Call at each end of testing and pass the optimization criterion as a parameter
//| TesterInit function                                              |
void OnTesterInit()
//--- Prepare the chart to show the balance graphs
   fg.OnTesterInit(3); //The parameter sets the number of balance lines on the chart
//| TesterPass function                                              |
void OnTesterPass()
//--- Process the testing results and show the graphics
//| TesterDeinit function                                            |
void OnTesterDeinit()
//--- End of optimization
//|  Handling of events on the chart                           |
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
   //--- Starts frames after the end of optimization when clicking on the header
   fg.OnChartEvent(id,lparam,dparam,sparam,100); // 100 is a pause between frames in ms

We use the Expert Advisor Moving Averages.mq5 available in the standard delivery pack. Paste the code and save the Expert Advisor as Moving Averages With Frames.mq5. Compile and run the optimization.

Thus, traders now can run visual optimization and process its results or display the required information during the optimization process.

Do You Want More?

We keep expanding the possibilities of the MetaTrader 5 trading platform by adding new tools to help traders. Post your comments and share your ideas on how to further improve visualization in the strategy tester. We will try to implement the most interesting and useful of them.

How to Use the Files

All files with the .mqh extension should be put in the MQL5\Include folder, that's where the compiler will look for them when compiling the Moving Average_With_Frames.mq5 Expert Advisor. The EA files can be put right in the MQL5\Experts folder or in any of its sub-folders.

Start the attached Exert Advisors yourself in the MetaTrader 5 Strategy Tester to make the reading of this article even more interesting. You will like it for sure.

Translated from Russian by MetaQuotes Software Corp.
Original article:

Attached files |
multimovings.mq5 (10.95 KB)
simpletable.mqh (10.79 KB)
specialchart.mqh (7.79 KB)
framegenerator.mqh (15.08 KB)
Step on New Rails: Custom Indicators in MQL5 Step on New Rails: Custom Indicators in MQL5

I will not list all of the new possibilities and features of the new terminal and language. They are numerous, and some novelties are worth the discussion in a separate article. Also there is no code here, written with object-oriented programming, it is a too serous topic to be simply mentioned in a context as additional advantages for developers. In this article we will consider the indicators, their structure, drawing, types and their programming details, as compared to MQL4. I hope that this article will be useful both for beginners and experienced developers, maybe some of them will find something new.

Here Comes the New MetaTrader 5 and MQL5 Here Comes the New MetaTrader 5 and MQL5

This is just a brief review of MetaTrader 5. I can't describe all the system's new features for such a short time period - the testing started on 2009.09.09. This is a symbolical date, and I am sure it will be a lucky number. A few days have passed since I got the beta version of the MetaTrader 5 terminal and MQL5. I haven't managed to try all its features, but I am already impressed.

False trigger protection for Trading Robot False trigger protection for Trading Robot

Profitability of trading systems is defined not only by logic and precision of analyzing the financial instrument dynamics, but also by the quality of the performance algorithm of this logic. False trigger is typical for low quality performance of the main logic of a trading robot. Ways of solving the specified problem are considered in this article.

Using text files for storing input parameters of Expert Advisors, indicators and scripts Using text files for storing input parameters of Expert Advisors, indicators and scripts

The article describes the application of text files for storing dynamic objects, arrays and other variables used as properties of Expert Advisors, indicators and scripts. The files serve as a convenient addition to the functionality of standard tools offered by MQL languages.