Init() and DeInit() execution sequence - page 23

 
fxsaber:

In principle, you could do with a single line.

Made it one line.
 
Nikolai Semko:


Where is it unambiguous?

Try this primitive example. You will understand the "uniqueness" when switching the TF.

In this example, an object with coordinates of current time and price is created in OnInit. In OnCalculate this object moves together with the price.

In OnDeinit it is simply (logically) removed.

When you switch the TF, it turns out that the object appears and then disappears.
Why does this happen?
Because sometimes OnDeinit of the old TF deletes what has already been created in OnInit of the new TF. It is not a bug! What should the programmer who created this example think and did not read this branch?



What is the point of using a primitive example of a two-timers?

Use an example of the VERY correct code instead.

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_plots   0

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {

   datetime t = TimeCurrent();
   double pr  = SymbolInfoDouble(Symbol(),SYMBOL_BID);
   if(ObjectFind(0, "InitDeinit") < 0)                 // ПРОВЕРКА СУЩЕСТВОВАНИЯ ОБЪЕКТА ПЕРЕД ЕГО СОЗДАНИЕМ ОБЯЗАТЕЛЬНА!!!
    {
     ObjectCreate(0,"InitDeinit",OBJ_ARROW_THUMB_UP,0,t,pr);
     Print(__FUNCTION__, " создан InitDeinit");
    }
    
   ObjectSetInteger(0,"InitDeinit",OBJPROP_WIDTH,50); 
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   if(UninitializeReason() != REASON_CHARTCHANGE)      // ПРОВЕРКА ПРИЧИНЫ ДЕИНИЦИАЛИЗАЦИИ РЕШАЕТ ТВОЮ ПРОБЛЕМУ НА РАЗ...
    {
     ObjectDelete(0,"InitDeinit");
     ChartRedraw(0); // ЭТО НУЖНО ТОЛЬКО НА ВЫХОДНЫЕ. В РАБОЧИЕ ДНИ НЕ БУДЕТ ЛИШНИМ, НО С ПРИХОДОМ НОВОГО ТИКА ОБЪЕКТ БУДЕТ НЕДОСТУПЕН.
     Print(__FUNCTION__, "  InitDeinit удалён");
    }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnCalculate (const int rates_total,      // размер массива price[]
                 const int prev_calculated,  // обработано баров на предыдущем вызове
                 const int begin,            // откуда начинаются значимые данные
                 const double& price[])      // массив для расчета
  {

   datetime t = TimeCurrent();
   double pr  = SymbolInfoDouble(Symbol(),SYMBOL_BID);
   ObjectMove(0,"InitDeinit",0,t,pr);
   return(rates_total);
  }
//+------------------------------------------------------------------+
Files:
 
Alexey Viktorov:

Better use an example of Virtually correct code.

It's the same. The object can be deleted via Deinit of the irrelevant indicator copy. And OnInit of the actual one is performed BEFORE this event.

But it is much better to show the transfer of any information from OnDeinit of the old copy to OnInit of the new one. Actually, this is the point.

 
fxsaber:
This is the same. The object can be deleted via Deinit of the irrelevant indicator copy. And OnInit of the actual one is performed BEFORE this event.
if(UninitializeReason() != REASON_CHARTCHANGE)
 {
  // Если причина деинициализации смена периода графика сюда не попадаем и объекты не удаляются.
 }

In all other cases, except for pulling the computer's power plug, the object will be deleted.

 
fxsaber:


But it's much better to show passing any information from OnDeinit of the old copy to OnInit of the new one. This is actually the point.

I noticed long ago, that simple solutions are not your strong suit. :-)))
 
Alexey Viktorov:

In all other cases, except for pulling the computer's power plug, the object will be deleted.

You have demonstrated a solution to a private discussion problem. The general one is formulated as follows
fxsaber:

Reliablypass any information from OnDeinit of the old copy to OnInit of the new copy.

Which, consequently, requires a clear OnDeinit/OnInit execution sequence. I've never needed this, but I've implemented the solution idea without any problems.

 
fxsaber:
You have demonstrated the solution to a private discussion problem. A general one may be formulated as follows

Which, consequently, requires a clear OnDeinit/OnInit execution sequence. I never needed this, but I implemented the solution idea without any problems.

Well, I am not against it. But scratching my right ear with my left little finger is not for me. Instead of such a simple test to write"it" ... I don't even know what to call it. Let it be for the amateur, because masochism has a right to exist. With all due respect to your professionalism in programming.

 
Alexey Viktorov:

Instead of such a simple check, write"it" ...

So you have missed the point of the problem being discussed in this thread.
 
fxsaber:
So you did not understand the essence of the problem discussed in this thread.

I understand, but you have transferred discussion of the problem to your abilities (not small).

And after all my answer was to a specific example of a code of a colt, and not in response to your abilities and an example of data transfer with regulation of sequence of execution On_Init and On_Deinit.

What is the purpose of continuing the dialogue? Are you trying to convince me that scratching the right ear with left little finger is very pleasant? Or another purpose?

 
Alexey Viktorov:

Or is it a different target?

The example you redid did not perfectly match the problem you were discussing. You could show another example that would not have a solution throughUninitializeReason.
Reason: