Bug with _UninitReason, it returns always 0

 

In MT4, the _UninitReason survives the change of periods, and it has the value of 3, as per the documentation, which is useful to change the behavior of an indicator.

In MT5, the reason is erased.

here is the log of this indicator which prints only the Reasons. The doc says when the period is changed,

https://www.mql5.com/en/docs/constants/namedconstants/uninit

_UninitReason

must be 3, but it's 0 when the indicator is relaunched, and so it's useless. 

2021.12.26 18:24:53.061    _UNINIT (GBPUSD,M5)    OnInit 0  0   5
2021.12.26 18:24:53.061    _UNINIT (GBPUSD,M5)    OnCalculate 0  0   5
2021.12.26 18:24:53.656    _UNINIT (GBPUSD,M5)    OnCalculate 0  0   5
2021.12.26 18:24:58.694    _UNINIT (GBPUSD,M5)    OnDeinit_Uninitalization reason code = 3
2021.12.26 18:24:58.694    _UNINIT (GBPUSD,M5)    OnDeinit_UninitReason = Symbol or timeframe was changed
2021.12.26 18:24:58.704    _UNINIT (GBPUSD,M15)    OnInit 0  0   15
2021.12.26 18:24:58.704    _UNINIT (GBPUSD,M15)    OnCalculate 0  0   15
2021.12.26 18:24:58.704    _UNINIT (GBPUSD,M15)    OnCalculate 0  0   15

In MT4, the log is this

2021.12.26 18:28:50.193    Custom indicator _UNINIT EURUSD,M1: loaded successfully
2021.12.26 18:28:51.351    _UNINIT EURUSD,M1: OnInit 0  0   1
2021.12.26 18:28:51.351    _UNINIT EURUSD,M1: initialized
2021.12.26 18:28:51.351    _UNINIT EURUSD,M1: OnCalculate 0  0   1
2021.12.26 18:28:54.146    _UNINIT EURUSD,M1: uninit reason 3
2021.12.26 18:28:54.146    _UNINIT EURUSD,M1: OnDeinit_Uninitalization reason code = 3
2021.12.26 18:28:54.146    _UNINIT EURUSD,M1: OnDeinit_UninitReason = Symbol or timeframe was changed
2021.12.26 18:28:54.146    _UNINIT EURUSD,M5: OnInit 3  3   5
2021.12.26 18:28:54.146    _UNINIT EURUSD,M5: initialized
2021.12.26 18:28:54.228    _UNINIT EURUSD,M5: OnCalculate 3  3   5
2021.12.26 18:28:54.233    _UNINIT EURUSD,M5: OnCalculate 3  3   5
2021.12.26 18:28:54.239    _UNINIT EURUSD,M5: OnCalculate 3  3   5
2021.12.26 18:28:54.245    _UNINIT EURUSD,M5: OnCalculate 3  3   5
2021.12.26 18:28:54.246    _UNINIT EURUSD,M5: OnCalculate 3  3   5
2021.12.26 18:28:54.313    _UNINIT EURUSD,M5: OnCalculate 3  3   5
2021.12.26 18:28:54.316    _UNINIT EURUSD,M5: OnCalculate 3  3   5
2021.12.26 18:28:54.316    _UNINIT EURUSD,M5: OnCalculate 3  3   5

//+------------------------------------------------------------------+
//|                                                      _UNINIT.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_minimum 1
#property indicator_maximum 10
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
Print("OnInit ",UninitializeReason(),"  ",_UninitReason,"   ",_Period);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
Print("OnCalculate ",UninitializeReason(),"  ",_UninitReason,"   ",_Period);
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| get text description                                             |
//+------------------------------------------------------------------+
string getUninitReasonText(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)
  {
//--- The first way to get the uninitialization reason code
   Print(__FUNCTION__,"_Uninitalization reason code = ",reason);
//--- The second way to get the uninitialization reason code
   Print(__FUNCTION__,"_UninitReason = ",getUninitReasonText(_UninitReason));
  }
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Uninitialization Reason Codes
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Uninitialization Reason Codes
  • www.mql5.com
Uninitialization Reason Codes - Named Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
You have two good options to save this value.

TerminalGlobalVariables

A custom object on the chart.