Trader’s Tech – Writing Your Own EA Part 77 – Chart Objects

Forex Robot

If you’re new to this series and want to check it out from the beginning, you can find that here. And look here for a list of all the programming articles.

In Part 76 we started our discussion about Chart Objects. We’ll continue that here.

I like to put status displays on my EAs that give me all the information I want regarding the active trade or other information regarding the chart. To do that I use a semi-generic function I call ShowStatusItem():

void ShowStatusItem(string strName,string Title,string Data,int Row,color Color=Black)
    {
    string name;

    name = StringConcatenate(Prefix,"status_lbl",strName);
    ObjectCreate(name,OBJ_LABEL,0,0,0);
    ObjectSetText(name,Title,StatsSize,StatsFont,StatsColor);
    ObjectSet(name,OBJPROP_CORNER,1);
    ObjectSet(name,OBJPROP_XDISTANCE,StatsColB);
    ObjectSet(name,OBJPROP_YDISTANCE,StatsStartRow + (StatsRowSize * Row));
   name = StringConcatenate(Prefix,"status_",strName);
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,Data,StatsSize,StatsFont,Color);
   ObjectSet(name,OBJPROP_CORNER,1);
   ObjectSet(name,OBJPROP_XDISTANCE,StatsColA);
   ObjectSet(name,OBJPROP_YDISTANCE,StatsStartRow + (StatsRowSize * Row));
   } //void DisplayStatusItem(string Name,string Title,string Data,int Row)

 

To use this function, you need to provide four or five parameters. The first is the name you want to assign to the status line. Each line contains a title and a value – two objects – and this name will be used with both. The function manipulates the name to conform to my object naming standard. The name used for the title object will start with the EA’s prefix (as assigned at the beginning of all my EAs), then the type of object – in this case status, then the abbreviation lbl to designate this as the label or title of the status line, then the name sent as a parameter to the function. The name for the data object will be the same without the lbl designation.

The second parameter is a string containing the text of the title you want to display describing the data.

Thirdly, send the data itself – formatted as a string. That means you’ll have to use the conversion function DoubleToStr() to format a double value to display. As I’ve mentioned before, look for a discussion of the conversion functions in a future episode.

Fourthly, send an integer telling the function on what row you want this particular status line to appear. The row locations are described by external variables below.

Lastly, if you want a color other than default black, you will send it as the last parameter. If you leave the parameter out, the text will default to black.  The way I’ve written this function, both the title and the data will appear in the same color. This could be modified by adding another color parameter to describe different colors for title and data.

I use a group of variables (usually external variables) to position the status display:

extern bool     DisplayStatus = true;
extern string   StatsFont="Verdana"; 
extern int      StatsSize=10;
extern int      StatsTitleSize=8;
extern int      StatsCorner=1;
extern int      StatsColA=5;
extern int      StatsColB=90;
extern int      StatsColTitle=140;
extern int      StatsStartRow=14;
extern int      StatsRowSize=14;

 

StatsFont is pretty self-explanatory.

StatsSize is the size of the data, StatsTitleSize is the size of the title.

StatsCorner describes the corner of the chart in which the status will be displayed.

StatsColA will be the nearest column to the edge of the chart, StatsColB will be the farther. StatsColumnTitle is not used in this function, but this allows me to have 3 columns in my status display. The values are in pixels. On the right-most corners (1 & 3), this describes the right edge of the column as the text will be right justified.

And here’s where the magic begins. 😉 StatsStartRow is the vertical position of row 0. StatsRowSize is the size of each row. When we designate row 4 for example, the function multiplies the value of StatsRowSize by 4 and then adds the StatsStartRow value.

I like to make these variables external to allow the user to change the size and positioning of the status display at runtime to better suit the chart appearance.

The reason I choose to place the status display in the upper-right corner of the chart (corner 1.) In my opinion, it’s very annoying to have to read text through candles or view candles through text. Sometimes it’s unavoidable since there are few points on the chart that won’t have candles at some time or other. But you can control the location of the right edge of the candles:

Trader's Tech - Chart ShiftClick on the Chart Shift toolbar button to activate MT4’s Chart Shift. This will shift the leading edge of the candles to the left to allow you to have text on the chart without interference with the candles. You can then use the chart shift handle to drag the leading edge of the candles closer or further from the right edge of the chart window. There is a limit as to how far you can drag it to the right, but it will still leave tons of space for status display text.

That’s a wrap for today. Next time we’ll continue our discussion of chart objects with a talk about graphical objects.

Thanks for your attention and please follow me on Twitter and LinkedIn.

Tim

The following two tabs change content below.
Winners Edge Trading was founded in 2009 and is working to create the most current and useful Forex information and training available on the internet.

Latest posts by admin (see all)

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news