Русский 中文 Español Deutsch 日本語 Português 한국어 Français Italiano Türkçe
Visualize a Strategy in the MetaTrader 5 Tester

Visualize a Strategy in the MetaTrader 5 Tester

MetaTrader 5Examples | 11 June 2012, 08:52
26 820 1

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 Ltd.
Original article:

Attached files |
multimovings.mq5 (10.95 KB)
simpletable.mqh (10.79 KB)
specialchart.mqh (7.79 KB)
framegenerator.mqh (15.08 KB)
Last comments | Go to discussion (1)
ddcmql | 14 Oct 2020 at 22:14
Is it possible to customize the graphs in the strategy tester report to be based on a custom optimization criteria? Eg my EA is optimizing for win rate, not profit. Can I have the graphs showing win rate by month, day of week, hour of day, month? Thanks!
The Golden Rule of Traders The Golden Rule of Traders
In order to make profits based on high expectations, we must understand three basic principles of good trading: 1) know your risk when entering the market; 2) cut your losses early and allow your profit to run; 3) know the expectation of your system – test and adjust it regularly. This article provides a program code trailing open positions and actualizing the second golden principle, as it allows profit to run for the highest possible level.
Get 200 usd for your algorithmic trading article! Get 200 usd for your algorithmic trading article!
Write an article and contribute to the development of algorithmic trading. Share your experience in trading and programming, and we will pay you $200. Additionally, publishing an article on the popular website offers an excellent opportunity to promote your personal brand in a professional community. Thousands of traders will read your work. You can discuss your ideas with like-minded people, gain new experience, and monetize your knowledge.
Limitless Opportunities with MetaTrader 5 and MQL5 Limitless Opportunities with MetaTrader 5 and MQL5
In this article, I would like to give an example of what a trader's program can be like as well as what results can be achieved in 9 months, having started to learn MQL5 from scratch. This example will also show how multi-functional and informative such a program can be for a trader while taking minimum space on the price chart. And we will be able to see just how colorful, bright and intuitively clear to the user trade information panels can get. As well as many other features...
Kernel Density Estimation of the Unknown Probability Density Function Kernel Density Estimation of the Unknown Probability Density Function
The article deals with the creation of a program allowing to estimate the kernel density of the unknown probability density function. Kernel Density Estimation method has been chosen for executing the task. The article contains source codes of the method software implementation, examples of its use and illustrations.