Download MetaTrader 5

Global scoped variables don't reset after changing time frame?

To add comments, please log in or register
Wang Qi
721
Wang Qi  

Hi all,

I'm coding an EA.

I have a global scoped variable. The pseudo code looks like:

bool hasDoneSomething = false;

int OnInit()
{
blah blah
}

void OnTick()
{
  if(! hasDoneSomething) {
    hasDoneSomething = true;
    callAnotherFunction();
  }
}


I loaded the EA on the chart, it works fine and callAnotherFunction is called.

Then I switched to another time frame, in the log I saw the EA is uninitialized then initialized again. However, callAnotherFunction is not called.

After I assign hasDoneSomething to false in OnInit, callAnotherFunction works again.

So my question is, when are global scoped variables initialized? Is it correct that switching time frame (or maybe also change pair) doesn't reset the variables?

So can I understand like, global scoped variables only initialize once until I unload the EA manually, and OnInit/OnDeinit are called whenever time frame or pair changes on the same chart?

Thanks

whroeder1
15598
whroeder1  
Global/statics are initialized on load. A deinit/init cycle is not a reload. If you want them to reset, code it that way.
Wang Qi
721
Wang Qi  
WHRoeder:
Global/statics are initialized on load. A deinit/init cycle is not a reload. If you want them to reset, code it that way.


Can I understand it this way: assume an .ex4 is a Windows DLL. EA loading is like LoadLibrary on a DLL, so global variables are initialized. Init/deinit doesn't reload the DLL, but they call functions OnInit/OnDeinit in the DLL?

Sorry I take DLL as example, I'm more familiar with DLL's life time...

Ex Ovo Omnia
3299
Ex Ovo Omnia  
While EAs keep the global scope variables after the reinitialization, indicators do not. No idea what was behind this inconsistency.
Wang Qi
721
Wang Qi  
Thanks all. I'm glad that I have understood it now. That will help to avoid a bunch or potential bugs.
To add comments, please log in or register