Variables being re-initialised after DeInit?

 

Hi there,

Following a similar pattern I've used in an EA, I have the following code at the top of an Indicator .mq4 file:

...

//---
#include <gwDeltaCache.mqh>
//---
CDeltaCache *DeltaCache;
bool isInitialLoad=true;

...

However, when I do something like change the timeframe, OnDeinit() is called and after that event method finishes, the lines containing the above variables are executed and DeltaCache becomes NULL and isInitialLoad is reset to true.

Obviously, these values are set/destroyed in my own code as they cache common data between timeframe/input parameter changes. I can't understand why they are being executed after OnDeinit is called? I guess the question is, do Indicators behave differently to EAs in this regard or, am I being incredibly stupid and missing something obvious! :)

Any help appreciated.

Cheers and thanks.

 
Geester:

Hi there,

Following a similar pattern I've used in an EA, I have the following code at the top of an Indicator .mq4 file:

However, when I do something like change the timeframe, OnDeinit() is called and after that event method finishes, the lines containing the above variables are executed and DeltaCache becomes NULL and isInitialLoad is reset to true.

Obviously, these values are set/destroyed in my own code as they cache common data between timeframe/input parameter changes. I can't understand why they are being executed after OnDeinit is called? I guess the question is, do Indicators behave differently to EAs in this regard or, am I being incredibly stupid and missing something obvious! :)

Any help appreciated.

Cheers and thanks.

When you change the time frame, the EA or Indicator is unloaded. That's why you see DeInit().

A new instance of the EA or Indicator is loaded on the clean chart.

What seems to be "re-initializing" is actually just initializing. There is no memory between the prior running EA/Indicator and the new one.

You could use Global Terminal Variables if you want to communicate states between instances.

Global Variables of the Terminal - MQL4 Reference
Global Variables of the Terminal - MQL4 Reference
  • docs.mql4.com
Global variables are kept in the client terminal for 4 weeks since the last access, then they will be deleted automatically. An access to a global variable is not only setting of a new value, but reading of the global variable value, as well. When testing and optimizing the Expert Advisors that use global variables, keep in mind...
 
Anthony Garot:

When you change the time frame, the EA or Indicator is unloaded. That's why you see DeInit().

A new instance of the EA or Indicator is loaded on the clean chart.

What seems to be "re-initializing" is actually just initializing. There is no memory between the prior running EA/Indicator and the new one.

You could use Global Terminal Variables if you want to communicate states between instances.

Hi @Anthony Garot

Many thanks for your reply. I am not seeing that behavior in the EA in which I use exactly the same pattern. To be sure, I ran the EA with a breakpoint on the variable lines. The breakpoint hits when first loaded but when changing timeframe, all works as expected and the variable lines aren't hit. In fact, I was originally provided with the isInitalLoad trick by @whroeder1 and this only works because the value is hit only once and you flip its value after first use in the OnInit() code.

Very strange indeed?

Files:
ea_code.png  44 kb
 
Anthony Garot:

When you change the time frame, the EA or Indicator is unloaded. That's why you see DeInit().

A new instance of the EA or Indicator is loaded on the clean chart.

What seems to be "re-initializing" is actually just initializing. There is no memory between the prior running EA/Indicator and the new one.

You could use Global Terminal Variables if you want to communicate states between instances.

Just a heads-up further to my earlier reply, I tried the same thing in another indicator and it too stopped at the variable initialization line after leaving the DeInit. It would seem that this is the default behaviour in indicators but not so in EAs. Jeez? Looks like I may have to use global variables, as you suggested!

Thanks again.

 
OnInit is called at every initialization. The EA is not reloaded at every initialization.
Initial load EA loaded: Globals, status reset OnInit
RecompileDeinitEA loaded: Globals, status reset OnInit
Chart change, parameters, account, templateDeinit*OnInit
Remove, chart close, terminal close, Init failedDeinit

* Indicators use to do the same thing, now they are always reloaded.

 
whroeder1:
OnInit is called at every initialization. The EA is not reloaded at every initialization.
Initial load EA loaded: Globals, status resetOnInit
RecompileDeinitEA loaded: Globals, status resetOnInit
Chart change, parameters, account, templateDeinit*OnInit
Remove, chart close, terminal close, Init failedDeinit

* Indicators use to do the same thing, now they are always reloaded.

Hi @whroeder1 - thanks for the heads-up. I discovered what you have stated above from https://docs.mql4.com/runtime/running#load and this has really cause an issue on this occasion. I basically have a custom object collection that I'm using as a cache and I wanted it to survive re-initializations. At the moment, I'm having to look a serializing/deserializing to a csv file but right now, depending on the time taken to read/write, I'm not even certain if the "juice is worth the squeeze", so to speak. Same technique works perfectly in EAs! Drat and double-drat, as Dastardly used to say in the cartoons :)

Cheers!

Program Running - MQL4 programs - MQL4 Reference
Program Running - MQL4 programs - MQL4 Reference
  • docs.mql4.com
Each script and each Expert Advisor runs in its own separate thread. All indicators work in the graphic interface thread. processing of ticks and history synchronization are also performed in graphic interface thread. Custom indicators work in the main interface thread. If a custom indicator has been called with the iCustom() function, this...
Reason: