Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

votes: 26
2017.05.29 15:40
Init_Sync.mqh (5.93 KB)view

If you change the timeframe or the name of the chart symbol in MetaTrader, all indicators on the chart will be unloaded from the chart and will be loaded onto it again. Unlike MT4, in MT5 the sequence of load/unload is not defined due to its internal architecture.

This feature sometimes causes problems which are not immediately obvious. These problems are related to the fact that OnInit of the new loaded indicator instance can be executed before the OnDeinit of the instance being unloaded.

Situations in which this problem is detected, are most often connected with the desire to transmit (explicitly/implicitly) some information from OnDeinit of the older indicator instance to OnInit of the new one. In other words, the new indicator instance should know about the existence of the old one, and should not load until the old instance is unloaded.

This library allows to apply this synchronization to any indicator.

Here is an example of a simple indicator

#property indicator_chart_window

#property indicator_buffers 1
#property indicator_plots   1
#property indicator_color1  clrRed
#property indicator_type1   DRAW_LINE

input int Input = 0;

double Buffer[];

int OnInit()
  SetIndexBuffer(0, Buffer);

void OnDeinit( const int Reason )

void OnChartEvent( const int id,
                   const long& lparam,
                   const double& dparam,
                   const string& sparam )

void OnTimer()

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[] )
  ArrayCopy(Buffer, open, prev_calculated, prev_calculated);  

If you launch this indicator and start switching its chart timeframes or symbol, the alternation of the lines "Init" and "Deinit" will be broken.

If we add the following line at the beginning of indicator code

#include <Init_Sync.mqh> // Synchronizes Init/Deinit of indicators

the proper alternation will be observed.

To apply this property to a desired indicator, add the above structure to its code.


// The library makes indicators' Init/Deinit synchronized.
// The indicator must contain int OnInit(), OnDeinit, OnTimer and OnChartEvent.
// If not used? leave empty.

#include <TypeToBytes.mqh> //
#include <crc64.mqh>       //

Additional mqh-files required for the library operation are available at the specified links.

Translated from Russian by MetaQuotes Software Corp.
Original code:

Color_Spread Color_Spread

The indicator shows the current spread using a color line.

Exp_XFatlXSatlCloud Exp_XFatlXSatlCloud

The Exp_XFatlXSatlCloud trading system is based on change of the trend direction displayed by the XFatlXSatlCloud indicator

XMA_BB_Pivot XMA_BB_Pivot

Two colored filled rectangles drawn between the values ​​of two Bollinger channels on one bar.

XMA_Keltner_Pivot XMA_Keltner_Pivot

Two colored filled rectangles drawn between the values ​​of two Keltner channels on one bar.