Global Scope Variables

 

I must be having a brain cramp because I cannot figure out what I've done incorrectly...

EA named MSZ...first 4 lines below:

#property copyright "Kris"
#property link ""

#include <stdlib.mqh>
#include <MSZ_Variable.mqh>

In the MSZ_Variable file, I have declared external and global variables. I thought that by declaring a variable in this file - outside of the init(), deinit() and start() functions - that the value would be retained from tick to tick. The first line below is just my note letting me know where the external variables start and the global ones begin.

//global variables
double UsePoint;
datetime CurrentTimeStamp; //Store the time stamp of the current bar (CheckOncePerBar)
string CurrentSessionOpen;
string CurrentSessionClose;

I cannot get the last 2 variables to hold a value from tick to tick. I don't even know how to start debugging this as the values change every tick no matter what I do. I've declared them as static (seems redundant), even hard-coded a value - the variables still revert to empty string. What am I missing? Ooh, just had a thought...do global variables have to be declared in the EA file itself and not in an Include file? That makes no sense, but I'll try it. Or maybe I have to initialize them in the init() function?

Any advice would be much appreciated.

Thanks.

 
  1. If you don't initialize string variables you can not read them, unlike int/double which default to zero. I always initialize them (even if just empty string,) either at declaration or in init()
  2. An include file is in the EA at the location of the include.
  3. init them in init, print them at the start and exit of start. You're changing them somewhere.
  4. make sure you haven't redeclared (directly or by the include) the variable in a function. That will hide the global.
  5. Otherwise post the code.