Download MetaTrader 5

Testing Visualization: Functionality Enhancement

18 January 2007, 17:21
Andrey Khatimlianskii
1 594

1. Motivation

Starting from build 196, MetaTrader 4 Client Terminal offers testing visualization function. It allows controlling the Expert Advisors' testing on a brand new level. Now, the trading programmer can watch every action of his or her Expert Advisor checking its operation on history!

You can read about how to use the visualizer in the article named Testing of Expert Advisors in the MetaTrader 4 Client Terminal: An Outward Glance. Our article describes how to make the strategy tester highly similar to the terminal.

So, what is the main specific of he tester? What lacks in visualization? The most important lack is perhaps the lack of "Trade" tab.

How many positions were opened? How soon will the pending order trigger? Has the StopLoss of the position moved? How much is the margin at the moment and how long is it to go to the MarginCall level? We find answers to these and many other questions in the "Trade" tab of the Terminal. But what can we do when testing?

This present article is devoted to solution of this problem.

2. Technical Aspects

We will start coding with problem definition, as usual. What result do we want to obtain?
- As said before, we need realization of something analoguous to "Trade" tab, i.e., to visualize information about all open positions and pending orders.
- Information must always be updated and be well readable.
- The program must be highly easy to use - its conection to the Expert Advisor may not need many efforts.

Now we can discuss technical aspects. How will the program be formed: as an indicator or as an external function library?

It is obvious that indicator in a separate window could be the best solution. It is always visible, does not hatch the price chart and is updated on every tick. But we then face a new problem when using the indicator - it takes all data about orders from the normal terminal, not from the 'virtual' (test) terminal, i.e., while testing, it will show information about the current state of the account without any reaction to the testing itself.

So we will have to find an alternative, a kind of a back way, to solve our problem.

We will still create an indicator, but it will only fulfil the function of a "chart subwindow creator". It will not show anything, i.e., we will always have a 'reserved' part of the chart. This is necessary so that the created subwindow could be saved under a unique name in template tester.tpl or in template <expert_name>.tpl.

Showing and updating of information will be assigned to a function library formed an included file.

3. Putting in Use

First of all, a chart template must be created that will use the visualizer. Just adjust the chart (colors, grid, scale), attach vTerminal.mq4 to the chart and use the "Save Template" command:

Chart Appearance before It Is Saved

Window "Save as Template"

For visualization purposes, the template named tester.tpl or <expert_name>.tpl will be used. It is this name that must be used to save the template.

4. Integration with the Expert Advisor

Functions that show information about positions are saved in the included file named VisualTestingTools.mq4, which is in the ...\MetaTrader 4\experts\include\ directory. First of all, it must be attached to the Expert Advisor - insert #include <VisualTestingTools.mq4> line before the start function. It will then appear as follows (example of CrossMACD_DeLuxe):

double _Commission = 0.0; string _Comment = ""; datetime _Expiration = -1;
#include <VisualTestingTools.mq4>
int start()
    if ( FastEMAPeriod >= SlowEMAPeriod ) 

    int _GetLastError = 0;

Now we can use the functions in any part of the program. Call for one of them - vTerminalInit() - will be placed in the Expert Advisor's init() function since it must be called only once for its task is to create objects to show information. Another one - vTerminalRefresh() - must be called at every tick. It will update information about open positions. Code of the EA that uses the functions will look like this:

double _Commission = 0.0; string _Comment = ""; datetime _Expiration = -1;
#include <VisualTestingTools.mq4>
int init()
    if ( FastEMAPeriod >= SlowEMAPeriod ) 
int start()
    if ( FastEMAPeriod >= SlowEMAPeriod ) 
    int _GetLastError = 0;

You can download Expert Advisor CrossMACD_DeLuxe_Visual.mq4 where all necessary changes have already been made.

5. Testing

Now let us look what we have got.
First, new external variables have been added in the Expert Advisor:

New external variables are available after the VisualTestingTools.mq4 file has been added to the Expert Advisor's code

Their destination is intuitive, but I would like to give more details about them:
- Rows - the maximum amount of lines shown in the display. For the most strategies, 10 lines will be enough;
- BigText - set true if you want to enlarge the fonts;
- SignalPoints - distance in points, starting from which fields Stop Loss, Take Profit and Open Price for pending orders will be highlighted. For example, if SignalPoints = 10, then the Stop Loss field will change its color when the price is 10 points from the Stop Loss of the position;
- MainColor - color of the terminal heading and of the bar informing about the account (Balance, Equity, ...).
- BuyColor - color of Buy-position information;
- BuyOPColor - color of the Open Price field, which will replace the main color when the current price appcoraches to the Open level of Buy Stop and Buy Limit ;
- BuySLColor - color, which will replace the main color when the price approaches to the Stop Loss level of Buy positions;
- BuyTPColor - color, which will replace the main color when the price approaches to the Take Profit level of Buy positions.
- SellOPColor, SellSLColor and SellTPColor - the same for Sell positions.

Let us set MainColor = Black and press OK. The Expert is now ready for testing. Before pressing the Start button, check "Visualization" and enjoy the results:

Until there are no open positions, our "terminal" shows only heading and the account status bar. Let us wait until a signal appears and the Expert opens a position.

A Sell position is opened as the first one. As you can see, the line is red.

Please direct your attention to the account status bar. It contains everything we get used to see in the "Trade" tab of the terminal. The entire information is updated every tick, so you can see how the position's profit changes:

When price approaches to the Stop Loss level of the position, the StopLoss field changes its color to that preset by user:

As to me, it is quite good.

6. Possible Problems

One more feature of using the tester.tpl with indicator vTerminal should be mentioned: During testing, the function that must find the indicator's window (WindowFind()) is unabled. This is why, the subwindow number is explicitly set in the code:

    win = WindowFind( "vTerminal" );
    if ( IsTesting() ) { win = 1; }

If the template contains other indicators to be drawn in a separate window, the entire information about them will be shown in the upper window. I.e., you have either to place vTerminal on the top or write win = subwindow number; instead of win = 1; .

No other problems were found when working with the indicator.

7. Conclusion

To draw a beautiful conclusion, I would like just to show some screenshots.

Translated from Russian by MetaQuotes Software Corp.
Original article:

Attached files |
vTerminal.mq4 (0.56 KB)
Last comments | Go to discussion (1)
iinself | 9 Feb 2008 at 19:16
good work but I get this error, please help:
2008.02.09 12:10:12    2008.02.07 23:59  CrossMACD_DeLuxe_Visual EURGBP.,H4: ObjectSetText( "Comment_49", "", 8"Arial", 0 ) - Error #4202
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.