Deinit Reason Code reporting wrong code (Bug)

 

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"

//+------------------------------------------------------------------+
//|                                                  testdeinint.mq5 |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window

input int par=7;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
//---
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| get text description                                             |
//+------------------------------------------------------------------+
string getUnitReasonText(int reasonCode)
  {
   string text="";
//---
   switch(reasonCode)
     {
      case REASON_ACCOUNT:
         text="Account was changed";break;
      case REASON_CHARTCHANGE:
         text="Symbol or timeframe was changed";break;
      case REASON_CHARTCLOSE:
         text="Chart was closed";break;
      case REASON_PARAMETERS:
         text="Input-parameter was changed";break;
      case REASON_RECOMPILE:
         text="Program "+__FILE__+" was recompiled";break;
      case REASON_REMOVE:
         text="Program "+__FILE__+" was removed from chart";break;
      case REASON_TEMPLATE:
         text="New template was applied to chart";break;
      default:text="Another reason";
     }
//---
   return text;
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  Print("ReasonCodes: ", UninitializeReason(), reason, _UninitReason);
//--- The first way to get the deinitalization reason code
   Print(__FUNCTION__,"_Deinitalization reason code =",reason);
//--- The second way to get the deinitalization reason code
   Print(__FUNCTION__,"_UninitReason =",getUnitReasonText(_UninitReason));
  }
Documentation on MQL5: Standard Constants, Enumerations and Structures / Named Constants / Uninitialization Reason Codes
  • www.mql5.com
Standard Constants, Enumerations and Structures / Named Constants / Uninitialization Reason Codes - Documentation on MQL5
 

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;


 
Rosh:

It's not a bug, see Program Running:


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 

 
Rashid Umarov:

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;


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

 
James Cater:


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 ?
 
Alain Verleyen:
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.

Documentation on MQL5: Standard Constants, Enumerations and Structures / Named Constants / Uninitialization Reason Codes
Documentation on MQL5: Standard Constants, Enumerations and Structures / Named Constants / Uninitialization Reason Codes
  • www.mql5.com
Standard Constants, Enumerations and Structures / Named Constants / Uninitialization Reason Codes - Reference on algorithmic/automated trading language for MetaTrader 5
 
James Cater:

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.

Thanks.