Skip to content

OnDeinit

OnDeinit

The function is called in indicators and EAs when the Deinit event occurs. It is used to deinitialize a running MQL5 program.

void  OnDeinit(
   const int  reason         // deinitialization reason code
   );

Parameters

reason

[in] Deinitialization reason code.

Return Value

No return value

Note

Deinit event is generated for EAs and indicators in the following cases:

  • before a re-initialization due to the change of a symbol or a chart period the mql5 program is attached to;
  • before a re-initialization due to the change of the inputs;
  • before unloading an mql5 program.

The reason parameter may have the following values:

ConstantValueDescription
REASON_PROGRAM0The EA has stopped working calling the ExpertRemove() function
REASON_REMOVE1Program removed from a chart
REASON_RECOMPILE2Program recompiled
REASON_CHARTCHANGE3A symbol or a chart period is changed
REASON_CHARTCLOSE4Chart closed
REASON_PARAMETERS5Inputs changed by a user
REASON_ACCOUNT6Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings
REASON_TEMPLATE7Another chart template applied
REASON_INITFAILED8The OnInit() handler returned a non-zero value
REASON_CLOSE9Terminal closed

EA deinitialization reason codes can be received by the UninitializeReason() function or from the predefined _UninitReason variable.

Sample OnInit() and OnDeinit() functions for the EA

input int fake_parameter=3;      // useless parameter
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Get the number of a build where the program is compiled
   Print(__FUNCTION__," Build #",__MQLBUILD__);
//--- Reset reason code can also be obtained in OnInit()
   Print(__FUNCTION__," Deinitialization reason code can be received during the EA reset");
//--- The first way to get a deinitialization reason code
   Print(__FUNCTION__," _UninitReason = ",getUninitReasonText(_UninitReason));
//--- The second way to get a deinitialization reason code
   Print(__FUNCTION__," UninitializeReason() = ",getUninitReasonText(UninitializeReason()));
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- The first way to get a deinitialization reason code
   Print(__FUNCTION__," Deinitialization reason code = ",reason);
//--- The second way to get a deinitialization reason code
   Print(__FUNCTION__," _UninitReason = ",getUninitReasonText(_UninitReason));
//--- The third way to get a deinitialization reason code
   Print(__FUNCTION__," UninitializeReason() = ",getUninitReasonText(UninitializeReason()));
  }
//+------------------------------------------------------------------+
//| Return a textual description of the deinitialization reason code |
//+------------------------------------------------------------------+
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;
  }

See also

OnInit,Event handling functions, Program running, Client terminal events, Uninitialization reason codes, Visibility scope and lifetime of variables, Creating and deleting objects

Last updated on