Trader’s Tech – Writing Your Own EA Part 53 – Generic Start Code

Forex RobotIf 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 52 we covered the deinit() and DeleteAllObjects() functions of our Generic Start Code. In this installment, we’ll move on to the DrawVersion() function.

As I’ve said, I like to keep track of the version of the EA and display it on the chart. There’s nothing too fancy about my versioning. I just increment the number each time I make a change to the EA. A major number of 0 (0.xx) represents an alpha or beta test version of the EA and I start my production versions with 1.00 – although in practice you rarely see a 1.00 because it seems I’m always making changes. I find that one of the hardest things to do is freezing the feature set and saying “this is the release version.” There’s always something else to do with the code.  That seems to be a common thread among the programmers with whom I’ve worked in the past.  Another reason why I think that programming is more art than science.

void DrawVersion()
   string name;
   name = StringConcatenate(Prefix,"Version");
   } //void DrawVersion()


The DrawVersion() function displays the version number in the lower left corner of the chart. To display the version, we use MT4 Object functions. Handling objects with MQL4 is a little tedious because of all the parameters that must be assigned. But it gives you very granular control of exactly where the object appears. We will use the following built-in Object functions:

ObjectCreate(string name, int type, int window, datetime time1, double price1, datetime time2=0, double price2=0, datetime time3=0, double price3=0) – This function creates the MT4 chart object, names it and, if it’s a drawing object, describes it’s location on the chart. Since our object is a label, we only tell it which window number on the chart to use (0 is the main chart) and we give it 0 for the date/time and price. The integer variable type, as is true with most MQL4 functions, has a group of constants that can be used to describe it so you don’t have to remember what number is for what type. These constants begin with OBJ_.  You can look here for the list of available constants. As I’ve mentioned before, when the function definition initializes a parameter, you can leave that parameter empty and the default value will be used.

ObjectSetText(string name, string text, int font_size, string font=NULL, color text_color=CLR_NONE) – This function sets the text for our label. We use the name assigned in the ObjectCreate() function to identify the object, then set the text, font size, font type and color. I tend to use Verdana as my font type and a size of 8 for the version. As for color, it depends on the chart color. When I’m unsure of the chart color, I use GoldenRod for text because it shows up fairly well on most chart colors.

ObjectSet(string name, int index, double value) – This function sets various properties for the object. As before, use the name of the object to identify it. The index is an integer value that tells the function what property you wish to set. As with other integer values, there are a group of constants assigned to help you. These constants begin with OBJPROP_. You can find the list of constants here.

The three properties we must set for our object are:

OBJPROP_CORNER – the corner of the chart to begin measurement for the placement of the label. The corners are 0-Upper Left, 1-Upper Right, 2-Lower Left and 3-Lower Right.

OBJPROP_XDISTANCE – the horizontal distance from the specified corner in pixels.

OBJPROP_YDISTANCE – the vertical distance from the specified corner in pixels.

Experiment with locations when you place your labels. In a future installment, I’ll discuss tricks I use in object placement.

That’s it for today. Next time we’ll talk about the HeartBeat() function.

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




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.

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news