为什么iCustom()返回指标的默认空值(2147483647)?

时间:2019-08-23 18:51:34

标签: mql4 mt4

我建立了自己的自定义指标,该指标使用之前的X条数量计算图表的近似斜率和加速度。

当我将指标附加到图表时,它可以按预期工作,但是当我尝试使用iCustom()在EA中检索最新值时,它仅返回2147483647。

我已经尝试为iCustom() shift参数使用不同的值,但没有成功。

double         SlopeBuffer[];
double         AccelerationBuffer[];

extern int delta;

int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,SlopeBuffer);
   SetIndexBuffer(1,AccelerationBuffer);

//---
   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[])
  {
//---
   int i, counted_bars;
   counted_bars = IndicatorCounted();
   i = Bars - counted_bars - delta; // Here I offset the initial bar by the delta(period)
                                    // so that the first bar has delta previous bars on the chart.

   while(i>=0){                     
      double Ex = 0; //intermediate calculation variables...
      double Ey = 0;
      double Exy = 0;
      double Exx = 0;
      for(int n=0;n<delta;n++){     // This for loop iterates over the previous delta bars
         Ex += n;                   // to calculate various sigma variables used to find the
         Ey += Close[i+delta-n-1];  // slope.
         Exy += n*Close[i+delta-n-1];
         Exx += n*n;
         }
      double slope = 100*(delta*Exy - Ex*Ey)/(delta*Exx - Ex*Ex); // final slope calculation.
      SlopeBuffer[i] = slope;                                     // add to the buffer.
      AccelerationBuffer[i] = (slope - SlopeBuffer[i+1]);         // calculate acceleration
   i--;                                                           // and adding to buffer.
   }

//--- return value of prev_calculated for next call
   return(rates_total);
  }

1 个答案:

答案 0 :(得分:0)

我发现了问题。

这是罪魁祸首。

i = Bars - counted_bars - delta;

一旦指标“赶上”了图表上所有已完成的柱,将使i等于1 - delta,这将是负数。结果是while循环永远不会运行。