Init_Sync - MetaTrader 5 Library | Forex Indicators Download - MT4/MT5 Resources

If you change the time frame or name of a chart symbol in MetaTrader, all indicators on the chart will be unloaded from the chart and loaded onto the chart again. Unlike MT4, in MT5 due to its internal architecture, the order of loading/unloading is not defined.
This feature can sometimes cause problems that are not immediately obvious. These issues are related to the fact that the OnInit of a newly loaded indicator instance can be executed before the OnDeinit of the unloaded instance.
The situations in which this problem is detected are usually related to the desire to transfer (explicitly/implicitly) certain information from the OnDeinit of the old indicator instance to the OnInit of the new indicator instance. In other words, the new indicator instance should be aware of the existence of the old indicator instance and should not be loaded until the old instance is unloaded.
The library allows applying this synchronization to any indicator.
Here is an example of a simple indicator
# Property indicator chart window #Property indicator buffer 1 #Property Indicator Chart 1 # PropertyIndicatorColor1RBC #Property indicator type 1 draw line Input integer input = 0 ; double buffer[]; integer initialization () { setIndexBuffer ( 0 , buffer); print ( "Initialization" ); Return ( initialization successful ); } Blank solution initialization ( constant integer reason) { print ( "delete" ); } BlankChartEvent ( const integer ID, constant long &l parameter, constant double &d parameter, constant string and spam) { } Blank timer () { } integer calculation ( const integer rate_total, constant integer previously calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], constant length &tick_volume[], const long &volume[], const int & spread[] ) { arrayCopy (buffer, open, prev_calculated, prev_calculated); return (rate_total); }
If you launch the indicator and start switching its chart timeframe or symbol, the alternation of the "Init" and "Deinit" lines will be broken.
If we add the following line at the beginning of the indicator code
#include //Init/Deinit of synchronized indicators Appropriate alternations will be observed.
To apply this property to the desired indicator, add the above structure to its code.
// This library synchronizes Init/Deinit of indicators. // The indicator must contain int OnInit(), OnDeinit, OnTimer and OnChartEvent. // If not used? Leave blank. #include// https://www.mql5.com/en/code/16280 #include // https://www.mql5.com/en/blogs/post/683577
Other mqh files required for library operation can be found at the specified link.
Attachment download
📎 init_sync.mqh (5.93 KB)
Source: MQL5 #18138
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •