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 46 we wrapped up our discussion of back testing and data. This installment, we're going to return to programming and discuss another cool feature of MQL4; Global Variables.
For clarification, you've probably heard me use the term global variables to refer to the variables that are declared at the beginning of the program whose scope is global to the whole program. For example:
static datetime LastTradeTime = 0; double AdjPoint;
These variables are declared in our EA just under the extern variables. From a programmer's point of view these are what I consider global variables since their scope includes all the functions in the EA (except, annoyingly, included functions cannot access them.)
Well, MT4 has a classification of variables they call Global Variables whose scope includes the whole platform; any EA, script or indicator running on the platform can access them. I think I would have called them Platform Variables or some such thing, but what do I know. 😉 Anyway, try not to get the two confused, they are very handy but are used very differently. In general, I will call MT4's (upper-case) Global Variables or GVs and I will refer to our EA globals as (lower-case) globals or global variables. If you're not sure which one I mean or I make a boo-boo, don't hesitate to call me on it. 🙂
You can look at the currently active Global Variables (GVs) in the system by clicking on your MT4 platform and hitting the F3 key. Alternatively, you can click Tools in the menu and then select Global Variables. You will get a dialog box that shows the Variable name, the current Value and the Date/Time the variable was created. I created a variable for the purposes of demonstration. I accepted the default name, but, as usual, I'm much more descriptive with variable names. Note that all GVs are type double. Since MQL4 converts seamlessly to int, you can easily store variables of type int and type bool as GVs.
Always keep in mind that GVs are platform-wide in nature and you must name your variables accordingly. I always start with my assigned abbreviation for the EA (I'll discuss that in the future) and follow with the symbol for the current chart. That way, you can have multiple EAs running that use the GVs as well as multiple pairs.
As you can see, you can add or delete a GV right from the dialog box. In addition, MQL4 includes several built-in functions for manipulating GVs. I've got a couple of my own that simplify things somewhat. My functions make a couple assumptions as to how the GVs will be used, so they are time-savers for me, but are not as versatile as MQL4's functions.
bool GlobalVariableCheck(string name) - Checks for the existence of the named GV.
bool GlobalVariableDel(string name) - Deletes an existing GV.
double GlobalVariableGet(string name) - Returns the value of the named GV.
string GlobalVariableName(int index) - Allows you to go through all the GVs in a loop and get the names.
datetime GlobalVariableSet(string name, double value) - Sets the value of a GV. If it doesn't already exist, it creates it. Returns the date/time of creation
bool GlobalVariableSetOnCondition(string name, double value, double check_value) - sets the value of a GV to "value" if, upon checking, it contains the value "check_value". If the GV exists and the value setting is successful it returns True - otherwise False
int GlobalVariablesDeleteAll(string prefix_name=NULL) - Deletes GVs. Be very careful with this function. You can never be sure another EA is not using the GVs and you could break it by deleting its GVs. That's why I always start my GV names with the EA abbreviation, so I can delete all the GVs without messing up any other EAs.
int GlobalVariablesTotal() - returns the total count of GVs currently active in the system.
Next time, we'll talk about how to use these handy GVs.
Thanks for your attention and please follow me on Twitter and LinkedIn.
Please leave a comment below if you have any questions!
Also, please give this strategy a 5 star if you enjoyed it!