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

 
fxsaber:
The solution to the problem

I.e. the whole problem is adding TWO of the same lines to any indicator.


Library code


Is there anything simpler, clearer and more vivid?

I also need a global variable name for each instance of the indicator, in case there are two indicators with different parameters in the chart).

 
Dmitry Fedoseev:


Can't it be simpler, clearer and more obvious?

No.

Also each indicator instance needs its own global variable name, in case there are two indicators with different parameters in the chart).

You shouldn't do it, because two instances of the indicator with different parameters on one chart contradicts the usage, which is wanted in this branch.

Therefore, if you start the second instance, it simply won't work until the first one is unloaded.

 
fxsaber:

No way.

...



#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Label1
#property  indicator_label1  "Label1"
#property  indicator_type1   DRAW_LINE
#property  indicator_color1  clrRed
#property  indicator_style1  STYLE_SOLID
#property  indicator_width1  1

string gvName;

double Label1Buffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(){
   gvName=MQLInfoString(MQL_PROGRAM_NAME);
   
   if(GlobalVariableCheck(gvName)){
      EventSetMillisecondTimer(1);      
   }
   else{
      GlobalVariableSet(gvName,1);
      NewInit();
   }
   return(INIT_SUCCEEDED);
}

void NewInit(){
   // все что было в ините должно быть здесь
   SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA);
}

void OnDeinit(const int r){
   GlobalVariableDel(gvName);
}

void OnTimer(){
   if(!GlobalVariableCheck(gvName)){
      EventKillTimer();      
      GlobalVariableSet(gvName,1);
      NewInit();
   }

}  
//+------------------------------------------------------------------+
//| 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[])
  {

   for(int i=rates_total-10;i<rates_total;i++)Label1Buffer[i]=close[i];

   return(rates_total);
  }
//+------------------------------------------------------------------+
Is it?
 
Dmitry Fedoseev:
Is it?

No, of course not. Handle-chart must necessarily be written in the name of a global variable.

And my solution is achieved by adding just two lines. It's somewhat simpler.

#include <Init_Sync.mqh> // Делает синхронизированными Init/Deinit индикаторов

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Label1
#property  indicator_label1  "Label1"
#property  indicator_type1   DRAW_LINE
#property  indicator_color1  clrRed
#property  indicator_style1  STYLE_SOLID
#property  indicator_width1  1

double Label1Buffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(){
   SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA);
   return(INIT_SUCCEEDED);
}

void OnTimer(){
  CHECK_INIT_SYNC;
   }

//+------------------------------------------------------------------+
//| 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[])
  {

   for(int i=rates_total-10;i<rates_total;i++)Label1Buffer[i]=close[i];

   return(rates_total);
  }
//+------------------------------------------------------------------+
 
fxsaber:

No, of course not. Handle-chart must necessarily be written in the name of a global variable.

And my solution is achieved by adding just two lines. This is somewhat simpler.


And the logic (algorithm) itself is the same?
 
fxsaber:

....

And my solution is achieved by adding just two lines. This is somewhat simpler.

And calling functions like SetIndexBuffer with a delay and not in the standard init doesn't bother you? Are you absolutely sure it's OK?

 
Dmitry Fedoseev:

Is the logic (algorithm) the same?
Yes.
 
Dmitry Fedoseev:

Count how many lines I have and how many you have... two lines it adds, a hell of a lot of nonsense to where - two lines.

You haven't counted - that's a lot.

Exactly two is enough to add for any indicator to have the coveted property of this discussion.

 
fxsaber:

You haven't counted - that's a lot.

Exactly two is enough to add to make any indicator a desirable feature of this discussion.


This is the tenth question. It's a matter of taste. But still my code is 5 times smaller and readable :/

More interesting is this:

Calling the SetIndexBuffer function with a delay and not in the standard inite is not confusing? Are you absolutely sure this is normal?

 
Dmitry Fedoseev:

Is calling the SetIndexBuffer function with a delay and not in the standard init? Are you absolutely sure that this is normal?

Absolutely. Calling OnCalculate will probably cause problems, but this is solved by easy library edits. It will be possible to clarify it on Monday.

In principle, one line may be enough.