Deinit Reason Code reporting wrong code (Bug)
It's not a bug, see Program Running:
Indicators are unloaded in the following cases:
- when detaching an indicator from a chart;
- terminal shutdown (if the indicator was attached to a chart);
- loading of a template, if an indicator is attached to a chart;
- closing of a chart, to which the indicator was attached;
- change of a profile, if the indicator is attached to one of charts of the changed profile;
- change of a symbol and/or timeframe of a chart, to which the indicator is attached;
- change of input parameters of the indicator.
So is this another change from MT4, in mt4 when you change the timeframe the reasoncode is REASON_CHARTCHANGE thus allowing you to write specific code in this case.
Seems strange to me as to why there is a change of meaning of the code from metatrader 4 and 5
It's not a bug, see Program Running:
Indicators are unloaded in the following cases:
- when detaching an indicator from a chart;
- terminal shutdown (if the indicator was attached to a chart);
- loading of a template, if an indicator is attached to a chart;
- closing of a chart, to which the indicator was attached;
- change of a profile, if the indicator is attached to one of charts of the changed profile;
- change of a symbol and/or timeframe of a chart, to which the indicator is attached;
- change of input parameters of the indicator.
This is not correct. REASON_REMOVE is completely different from REASON_CHARTCHANGE.
In fact there is a horrible bug in MQL5 where you usually get REASON_CHARTCHANGE when you change timeframe but sometimes you get REASON_REMOVE.
I created the following log file with a fresh M30 chart, then changed timeframe 4 times H1 => W1 => M15 => M30
RE 0 00:16:12.008 blah2 (USDZAR,H1) OnDeinit_Deinitalization reason code =3
IQ 0 00:16:12.008 blah2 (USDZAR,H1) OnDeinit_UninitReason =Symbol or timeframe was changed
HQ 0 00:16:14.435 blah2 (USDZAR,W1) ReasonCodes: 111
KP 0 00:16:14.435 blah2 (USDZAR,W1) OnDeinit_Deinitalization reason code =1
KR 0 00:16:14.435 blah2 (USDZAR,W1) OnDeinit_UninitReason =Program blah2.mq5 was removed from chart
LF 0 00:16:40.031 blah2 (USDZAR,M15) ReasonCodes: 333
EQ 0 00:16:40.031 blah2 (USDZAR,M15) OnDeinit_Deinitalization reason code =3
HH 0 00:16:40.031 blah2 (USDZAR,M15) OnDeinit_UninitReason =Symbol or timeframe was changed
As you can see the third change from W1 to M15 shows the DeInit reason as REASON_REMOVE when it was just another timeframe change (REASON_CHARTCHANGE)
This means you cannot rely upon MQL5 to tell you correctly what it is doing from the DeInit reason code.
A BUG if ever there was one (and by the way, this NEVER happens on MQL4)
I will raise this issue with the support desk. In the mean time I will have to use files to pass variable state across chart changes, very annoying
Did you get an answer from ServiceDesk ?
I did, I raised issue #1492194 however I got a response similar to the one here.
I have since replied, politely pointing out that they seem to be confusing "unloading" with DeInit "reason" codes. https://www.mql5.com/en/docs/constants/namedconstants/uninit
I also asked them to run this issue past the development team.
I'm now waiting for a reply. I will keep you posted here.

- www.mql5.com
I did, however I got a response similar to the one here.
I have since replied, politely pointing out that they seem to be confusing "unloading" with DeInit "reason" codes. https://www.mql5.com/en/docs/constants/namedconstants/uninit
I also asked them to run this issue past the development team.
I'm now waiting for a reply. I will keep you posted here.

- Free trading apps
- Free Forex VPS for 24 hours
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
The below code has been created pretty much from the help file.
It is clear there are some issues, attach the indicator and change timeframe, the reason code reported is "REASON_REMOVE" where it should be "REASON_CHARTCHANGE"