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

Back to topics list To post a new topic, please log in or register
avatar
31
wqking 2015.12.04 14:27
 

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


avatar
12985
WHRoeder 2015.12.04 14:36 #
 
Global/statics are initialized on load. A deinit/init cycle is not a reload. If you want them to reset, code it that way.
avatar
31
wqking 2015.12.04 14:39 #
 
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...

avatar
1109
Ovo 2015.12.04 18:47 #
 
While EAs keep the global scope variables after the reinitialization, indicators do not. No idea what was behind this inconsistency.
avatar
31
wqking 2015.12.05 05:05 #
 
Thanks all. I'm glad that I have understood it now. That will help to avoid a bunch or potential bugs.
Back to topics list  

To add comments, please log in or register