Trader’s Tech – Writing Your Own EA Part 48 – Global Variables

Forex Robot

It appears that I may be wishing for the cool rainy Indiana weather this week as Texas hits the third digit. “But it’s a dry heat.” 😉

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 47 we started our discussion of MT4’s Global Variables (GVs.) This time we’re going to talk about how we can use them to our advantage.

After reading our last installment, you may be asking what GVs can do for you? GVs have a few prominent uses. The first is to store values between runs of your EA. In some of the more complex money management systems, you will want to keep track of numbers between runs of the EA. You will also want to save information that could be lost if you were to shut down your computer or your computer were to crash while the EA was running.

The second use for GVs is passing data between EAs and pairs. You may have a strategy that needs information about what other pairs are doing or what other trades are happening. You can pass data between EAs using GVs.

Thirdly, as I mentioned earlier, you can’t use (lower case) global variables to pass data to included functions. I don’t like using GVs for that purpose, but they can be used that way if need be.

And lastly, but definitely not leastly, 😉 I use GVs to allow me to change the value of certain EA parameters during the course of running the EA without having to stop the EA to change external variables.

As I mentioned before, I’ve got a few functions that I’ve written that help me use GVs my way. I mostly use GVs to store data between EA runs and to protect the data in the case of a crash. If I have a LOT of data to store, I store it in a file, but that’s a discussion for another day. I have these functions in my “generic EA” file. That’s the file with which I start all my EAs (no I don’t use the EA wizard.) Again, my “generic EA” file is a discussion for another day.

void SetGV(string VarName,double VarVal)
   string strVarName = StringConcatenate(Prefix,Symbol(),"_",VarName);

      Print("###Set GV ",strVarName," to ",VarVal);

   } //void SetGV(string VarName,double VarVal)

double GetGV(string VarName)
   string strVarName = StringConcatenate(Prefix,Symbol(),"_",VarName); 
   double VarVal = -99999999; 
      VarVal = GlobalVariableGet(strVarName);
         Print("###Get GV ",strVarName," Value=",VarVal); 
      } return(VarVal); 
   } //double GetGV(string VarName)

The function SetGV() prepends the Prefix (assigned at the beginning of the EA code – future discussion) and Symbol() to the GV name and assigns the passed value to the GV. If the GV doesn’t exist, MT4 creates it automatically.

The function GetGV() returns the value of the named GV if it exists and returns a value of -99999999 if it doesn’t exist. I picked that value for the obvious reason that it’s unlikely to be used as the actual value of a GV. As before, the function prepends the Prefix and Symbol() to the GV name.

Also if the boolean variable debug is true, it prints information regarding the GVs to the Experts file. Debug is a normal part of my EAs. In fact, an example of the fourth use of a GV is I use a GV by the name of %prefix%%symbol%_Debug so I can start the “debugger” during the running of an EA without having to stop the EA to set an external variable. If have a simple function called CheckDebug():

bool CheckDebug()
      if(GlobalVariableGet(StringConcatenate(Prefix,Symbol(),"_debug")) == 1)
         debug = true;
         debug = false;
      }  //if(GlobalVariableCheck(...

Then, during the debugging and testing of the EA, I include the following line in my init() and start() functions:

debug = CheckDebug();

So the EA checks for the debug value every tick. This will allow me to turn the debug on and off without disturbing the EA. I then disable this line (comment it out) for the production version of the program to keep it from slowing the everything down.

These are simple functions, but allow you to use GVs with a minimum of fuss. They won’t work for all circumstances (for example – accessing data from another EA), but they are very useful for quick storage and retrieval.

That’ll wrap us up for today. Next time we’ll talk about my “generic” EA starting code.

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

  • IsItCoffeeYet

    Hi Hans, Glad we can be of help. You should also check out using files for transferring data between EAs and charts. I cover some of that starting here:


  • Hans

    Up til now I have been using pending orders which were never going to be filled, to pass info between EA’s. Until today… Never knew about Global Variables. Duh! This is going to make programming so much easier! Great educational site!

  • Hi Dave,
    Yes. It blew me away when I realized that myself. It makes up for some of MQL4’s failings.

  • Dave Hanna

    I like it! I never new about Global Variables in MT4. I like the idea of being able to enable/disable functionality on the fly.