Init()和DeInit()执行顺序 - 页 22

 
fxsaber:
问题的解决方案

也就是说,整个问题是在任何指标上增加两行相同的内容。


图书馆代码


还有什么更简单、更清晰、更生动的吗?

我还需要为指标的每个实例提供一个全局变量名,以防图表中存在两个参数不同的指标)。

 
Dmitry Fedoseev:


难道不能更简单、更清晰、更明显吗?

没有。

另外,每个指标实例都需要自己的全局变量名称,以防图表中存在两个参数不同的指标)。

你不应该这样做,因为一个图表上有两个参数不同的指标实例,这与这个分支想要的用法相矛盾。

因此,如果你启动第二个实例,在第一个实例被卸载之前,它根本无法工作。

 
fxsaber:

不可能。

...



#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);
  }
//+------------------------------------------------------------------+
是吗?
 
Dmitry Fedoseev:
是吗?

不,当然不是。Handle-chart一定要写在全局变量 的名称中。

而我的解决方案只需添加两行即可实现。这多少有点简单。

#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:

不,当然不是。Handle-chart一定要写在全局变量 的名称中。

而我的解决方案只需添加两行就可以实现。这在某种程度上更简单。


而逻辑(算法)本身是相同的?
 
fxsaber:

....

而我的解决方案只需添加两行就可以实现。这在某种程度上更简单。

调用 SetIndexBuffer这样的函数 时有延迟,而且不是在标准的init中,你不觉得麻烦吗?你绝对确定它是好的吗?

 
Dmitry Fedoseev:

逻辑(算法)是否相同?
是的。
 
Dmitry Fedoseev:

数一数我有多少条线,你有多少条线......。它增加了两行,在那里有很多废话 - 两行。

你没有数过--那是一个很大的数字。

对于任何指标来说,恰好有两个足够的添加量来拥有这个讨论中令人垂涎的属性。

 
fxsaber:

你没有数过--那是一个很大的数字。

恰恰是两个足以使任何指标成为本次讨论的理想特征的补充。


这是第10个问题。这是一个品味的问题。但我的代码仍然小了5倍,而且可读性也很好 :/

更有趣的是这一点。

调用有延迟的SetIndexBuffer函数,而不是在标准的inite中调用,这不令人困惑吗?你绝对确定这是正常的吗?

 
Dmitry Fedoseev:

调用SetIndexBuffer函数 时有延迟,而且不在标准init中?你绝对确定这是正常的吗?

绝对的。调用OnCalculate可能会引起问题,但这可以通过简单的库编辑来解决。周一将有可能澄清这一点。

原则上,一条线可能就够了。