Easy way of adding comments to the chart

You don’t have to write programs in MQL for a long time to start complaining for the method of putting comments on the chart. In case of a bigger portion of data that we want to display on the chart, an elegant solution will be to create our own helper functions.

Below, I show you two sample functions, which I use myself in my projects:

string writeBuffer="";

void write(string text1="",string text2="") {

 if(text2!="") {
  writeBuffer=StringConcatenate(writeBuffer,text1,": ",text2,"\n");
 } else {
  writeBuffer=StringConcatenate(writeBuffer,text1,"\n");
 }

}

void writeOut() {
 Comment(writeBuffer);
 writeBuffer="";
}

How to use it and how can we benefit from it? Using it is very simple and flexible. Lets see it in the sample code:

extern int sl=80; // sample EA parameter

int start() {

 write(); // add an empty line
 write("Test of displaying comments"); // add a simple text
 write("Stop loss",sl); // add a line with a variable

 f(); 

 writeOut();
}

void f() {
 write("f() function");
}

// of course at the end we add the code from the first listing,
// which contains variable and function definition

Solution which uses write and writeOut functions is good, because:

  • Such code looks much better…
  • …and takes less space than adding consecutive values to the variable one by one
  • We don’t have to remember any scopes of variables and we don’t have any problems with displaying comments from called functions
  • It simplifies printing of variables (we don’t have to manually add signs such as “:”)

Just don’t forget to call writeOut() at the end of your start function ;)

Share with your friends:

Send us your comments

Name (required)

Email (required)

Message

  • Facebook