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.
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?
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!
* 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 :)