OnDeinit in indicators - page 7

 

Developers, you have a mistake somewhere.

void OnDeinit(const int reason)
{
 //Comment("");
 Comment(ArrayInitialize(body_down,EMPTY_VALUE),"  ",body_down[0],"  ",body_down[1],"  ",body_down[2],"  ",body_down[3]);
}

Look at the screenshot of Comment after closing the terminal and opening it after 1 minute.

Number of initialized elements and that the first, second, third and fourth element of the buffer is empty.

Indicator counts only the 0th buffer. So why 2 candles?


 
Oh, yeah. The English help also says thatArrayInitialize does not return anything.

 
eevviill:

Developers, you have a mistake somewhere.

Look at the screenshot of Comment after closing the terminal and opening it after 1 minute.

Number of initialized elements and that the first, second, third and fourth element of the buffer is empty.

Indicator counts only the 0th buffer. So why 2 candles?


Because when you analyze for a new bar, you don't check the number of bars, but only check the time. When you start the terminal or break the connection with the server during the operation, before the missing (to the full history) bars arrive, a tick comes. At this tick, a new candle is created, but there is a gap (missing history) between it and the history in the terminal, which will be downloaded from the server and inserted into the system, and since you don't analyze the number of bars, you will think that there is no new bar in the next calculation, and that's when the second candle appears. After the opening of a new bar it will disappear.
 
mql5:
Because when you analyse for a new bar, you do not check the number of bars, but limit yourself to checking the time. When you start the terminal or break connection with the server during the operation, a tick comes before the missing (to the full history) bars arrive. At this tick, a new candle is created, but there is a gap (missing history) between it and the history in the terminal, which will be downloaded from the server and inserted in its place. After the opening of a new bar it will disappear.

And it doesn't matter that in deinit the zeroing occurs when the terminal is closed?

 

Vasily, run a test indicator that prints the number of bars.

//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

datetime lb;
int      rates_;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   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[])
  {
//---
   
//--- return value of prev_calculated for next call
   if(NewBar(PERIOD_CURRENT, lb))
    {
     Print("rates_total = ", rates_total);
     rates_ = rates_total;
    }
   return(rates_total);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//if(reason == REASON_CLOSE)
Print("OnDeinit сработал ", rates_);
}

bool NewBar(int tf, datetime &lastbar)
{
   datetime curbar = iTime(_Symbol, tf, 0);
   if(lastbar != curbar)
    {
     lastbar = curbar;
     return (true);
    }
   else return(false);
}//******************************************************************|

This is what it prints in the log.

0       22:52:04.765    Custom indicator Test\test EURUSD.z,M1: loaded successfully
0       22:52:04.795    test EURUSD.z,M1: initialized
0       22:52:04.795    test EURUSD.z,M1: rates_total = 75183
0       22:52:11.902    test EURUSD.z,M1: rates_total = 75184
0       22:52:28.712    test EURUSD.z,M1: uninit reason 9
0       22:52:28.712    test EURUSD.z,M1: OnDeinit сработал 75184
0       22:52:28.832    Custom indicator test EURUSD.z,M1: removed
0       22:57:10.366    Custom indicator Test\test EURUSD.z,M1: loaded successfully
0       22:57:10.396    test EURUSD.z,M1: initialized
0       22:57:10.396    test EURUSD.z,M1: rates_total = 75184
0       22:57:14.972    test EURUSD.z,M1: rates_total = 75185
0       22:57:59.167    test EURUSD.z,M1: rates_total = 75190 
// Локальное время наверное отстаёт от серверного на несколько секунд. На сервере было уже 58 минут.
0       22:58:34.237    test EURUSD.z,M1: uninit reason 1
0       22:58:34.237    test EURUSD.z,M1: OnDeinit сработал 75190
0       22:58:34.247    Custom indicator test EURUSD.z,M1: removed


Maybe this will tell you where the problem is coming from...

 
eevviill:

Please ask the developers for a simple answer.

When the terminal is closed, is the DeInit() function executed in the programs attached to the chart?

Strictly speaking, no.

OnDeinit is executed. DeInit will only be executed if you call it explicitly from OnDeinit.

If you are referring to the old deinit function, also keep in mind that function names are case-sensitive. That is, deInit is an unknown function, deinit is a standard entry point on deinitialization (like OnDeinit)

 
eevviill:

And it doesn't matter that in deinit the zeroing takes place when the terminal is closed?

Who cares? When the terminal is closed, these buffers are no longer needed, because they will immediately be released when the program is unloaded, which occurs immediately after OnDeinit

Try just changing the timeframe. OnDeinit will work, buffers will remain (because program is not unloaded in this case), but no one guarantees their contents from "past life"

Read at last the documentation! https://docs.mql4.com/ru/runtime/running and https://docs.mql4.com/ru/runtime/event_fire#deinit

 
stringo:

Who cares? When you close the terminal, these buffers are no longer needed, because they will immediately be released when the program is unloaded, which occurs immediately after OnDeinit

Try just changing the timeframe. OnDeinit will work, buffers will remain (as program unloading does not happen in this case), but no one guarantees their contents from "past life"

Read at last the documentation! https://docs.mql4.com/ru/runtime/running and https://docs.mql4.com/ru/runtime/event_fire#deinit

So, in my case, the buffers are already being split when the chart is reopened?
 
eevviill:
So in my case, the buffers are bifurcated when the graph is reopened?

You do not have buffer bifurcation.

This is how you fill your buffer yourself. The first time, when you draw it; the second time, when you get a portion of data. Both times you operate with the same Time[0] value.

 
stringo:

You are not having a buffer split.

This is how you fill your buffer yourself. The first time at rendering and the second time at data arrival. Both times you operate with the same Time[0] value.

OK.

Please correct English help and that's it.

Also English help says thatArrayInitialize returns nothing.

Reason: