Download MetaTrader 5

Testing Visualization: Account State Charts

27 July 2007, 12:22
Andrey Khatimlianskii
2
1 100

Introduction

I think, it is not only my wish to view the information about the account state during testing in the visualization mode in a more informative form.
What was the amount of free margin before opening the last position? Where is the balance curve directed to? How large was the drawdown on equity for the last day?

Some of these questions were discussed in the articles Testing Visualization: Functionality Enhancement and Testing Visualization: Trade History. But they do not contain any tool for quick and easy viewing the information on the account. In this article we will discuss programs for drawing charts of the account state during testing in the visualization mode. As an example we will examine Balance and Equity charts. The article also contains instructions on building charts of other account attributes.



Is it Easy?

One would think, what can be easier than writing an indicator, displaying Balance? The only buffer is fulfilled by the last known Balance value upon the receipt of bars. This is the algorithm.
But there is one problem: the indicator, displayed on the testing visualization chart, does not have the access to the state of the tested account. All functions, responsible for this information, return the values of the real account.
In this article we will learn to avoid this "peculiarity". For this purpose we need:

  • receive the information about the account state from the Expert Advisor;
  • pass it to the indicator and visualize it.

For passing the relevant data we will use Global variables of the terminal, and for their visualization - standard functions of custom indicators.



It is easy!

We need to add into the Expert Advisor, which will be tested, a code of saving the Balance into the Global variable. All we need is add one line in the beginning of the function start():

int start()
{
   // saving the last value of the balance into the global variable
   GlobalVariableSet( "vGrafBalance", AccountBalance() );
 
   // Expert Advisor code
 
   return(0);
}

The indicator code is not much more difficult:

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue
 
double balance[];
 
int init()
{
    IndicatorShortName( "vGrafBalance" );
    IndicatorDigits( 2 );
 
    SetIndexStyle( 0, DRAW_LINE );
    SetIndexBuffer( 0, balance );
    SetIndexLabel( 0, "Balance" );
}
int start()
{
    balance[0] = GlobalVariableGet( "vGrafBalance" );
    return(0);
}

That is all! Now we can compile the indicator, the new version of the Expert Advisor and start testing!

After clicking the Start button and attaching an indicator to the chart, we will see something like this:




You see, the balance curve reflects real changes - at the closing of each trade its value changes.



What else can be done?

All other graphs are built the same way:

  • add into the Expert Advisor a line, updating the value of the necessary Global variable;
  • create a simple indicator, reading the value of this variable and reflecting it on the chart.

Note, that the variable name for each characteristic must be unique, i.e. it must not be identical with the names of other variables.
Besides, it should be identical with the name of the variable, read by the indicator.


The second example shows how to add the Equity chart to the Balance chart.
The code of the Expert Advisor:

int start()
{
   // saving the last value of the balance into the global variable
   GlobalVariableSet( "vGrafBalance", AccountBalance() );
   // saving the last value of the equity into the global variable 
   GlobalVariableSet( "vGrafEquity", AccountEquity() );
 
   // Expert Advisor code
 
   return(0);
}

The indicator code:

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
 
double equity[];
 
int init()
{
    IndicatorShortName( "vGrafEquity" );
    IndicatorDigits( 2 );
 
    SetIndexStyle( 0, DRAW_LINE );
    SetIndexBuffer( 0, equity );
    SetIndexLabel( 0, "Equity" );
}
int start()
{
    equity[0] = GlobalVariableGet( "vGrafEquity" );
    return(0);
}

Example of use:




We may attach one indicator to the another one to view the correlation of two characteristics:




Conclusion

The article describes the process of creating indicators, visualizing the account state. We have analysed the simplest indicators - Balance and Equity indicators.
You can create any other graph: percentage of profitable trades, expected payoff, Average lot or Profit of a position in points - everything is available.

The information can be beautifully presented!

Translated from Russian by MetaQuotes Software Corp.
Original article: https://www.mql5.com/ru/articles/1487

Last comments | Go to discussion (2)
MQL4 Comments
MQL4 Comments | 15 Sep 2007 at 16:51
I think your idea of indicator of equity and balance is very usefull.

But I have 2 problems.

One is I cannot write the indicator with both lines in one window (as you presented at the ned of your article).
Could you paste the code of the indicator of both balance and equity in one window?

The second problem is that the indicators work only when the testing is run in normal mode.
When Itry to run the indicator in the fast mode (using "skip to" function) the indicators are not drawn.
I guess it is possible to solve this problem by implementing the indicator into the expert advisor body.
But I have no idea how to do it.

Maybe you have any idea?


Thank you in advance for your help.
Andrey Khatimlianskii
Andrey Khatimlianskii | 16 Sep 2007 at 23:04
paszo:
One is I cannot write the indicator with both lines in one window (as you presented at the ned of your article).
Could you paste the code of the indicator of both balance and equity in one window?
Use this code:
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
 
double balance[], equity[];
 
int init()
{
    IndicatorShortName( "vGrafBalance&Equity" );
    IndicatorDigits( 2 );
 
    SetIndexStyle( 0, DRAW_LINE );
    SetIndexBuffer( 0, balance );
    SetIndexLabel( 0, "Balance" );
 
    SetIndexStyle( 1, DRAW_LINE );
    SetIndexBuffer( 1, equity );
    SetIndexLabel( 1, "Equity" );
}
int start()
{
    balance[0] = GlobalVariableGet( "vGrafBalance" );
    equity [0] = GlobalVariableGet( "vGrafEquity"  );
    return(0);
}

paszo:
The second problem is that the indicators work only when the testing is run in normal mode.
When Itry to run the indicator in the fast mode (using "skip to" function) the indicators are not drawn.
I guess it is possible to solve this problem by implementing the indicator into the expert advisor body.
But I have no idea how to do it.

Maybe you have any idea?
Unfortunately, it is impossible to solve this problem.
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.