Download MetaTrader 5

Trouble with Global Variable

To add comments, please log in or register
MisterDog
381
MisterDog  

I can't retrieved a Global Variable in an EA. First I am setting a Global Variable with a script. Then I am attempting to read the Global Variable in an EA. Here is the code and errors. What is wrong?

script...

int start()
{
   int testVar;
   GlobalVariableSet(testVar, 12345);     // set the global variable
   int xyz = GlobalVariableGet(testVar);  // get the global variable just as a test
   Print("Script: ",xyz);                 // prints: "Script: 12345"
   return;
}

EA...

      int abc = GlobalVariableGet(testVar);  // get variable
      Print("Error: ",GetLastError());       // prints, "Error: 4058". global variable not found
      Print("Result: ",abc);                 // prints, "Result: 0"
Keith Watford
Moderator
10565
Keith Watford  
Do you have another expert running that deletes GVs?
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI  

Most probably is like that.


@ MisterDog

After setting up a Global Variable with a script, open GV inside terminal and see if the GV is set or not.

You track and do troubleshooting first.

MisterDog
381
MisterDog  

This is the first time I've looked at a global variable in the terminal. I found it by pressing F3. Yes -- the value of "12345" is there.

And guess what I found out. The EA has to be loaded and running for this to work! Somewhere in the process the EA got unloaded. Thanks for the education everyone.

Alain Verleyen
Moderator
32396
Alain Verleyen  
MisterDog:

This is the first time I've looked at a global variable in the terminal. I found it by pressing F3. Yes -- the value of "12345" is there.

And guess what I found out. The EA has to be loaded and running for this to work! Somewhere in the process the EA got unloaded. Thanks for the education everyone.

The first parameter if GlobalVariableSet/Get is a string, it's probably better to not use an int.
To add comments, please log in or register