为什么在此代码中出现数组超出范围错误?

时间:2019-02-08 19:40:14

标签: arrays mql4

最初的问题

我正在尝试绘制指标“ xxx.mq4”的RSI,如下所示:

#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_width1 2

//---- buffers
double ExtMapBufferCustomIndicator[];
double ExtMapBufferRSICustomIndicator[];
int i;
string s="xxx";

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBufferRSICustomIndicator);
   SetIndexLabel(0,"RSICustomIndicator");

   IndicatorShortName("RSI of xxx: RSICustomIndicator");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int counted_bars=IndicatorCounted();
   if(counted_bars < 0)  return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   if(counted_bars==0) limit-=15;
//   printf(limit);
//---- main loop
   for(i=0; i<limit; i++)
     {
      ExtMapBufferCustomIndicator[i]= iCustom(NULL,0,s,20,40,0,0);
     }
   for(i=0; i<limit; i++)
     {
      ExtMapBufferRSICustomIndicator[i]=iRSIOnArray(ExtMapBufferCustomIndicator,0,14,0);
     }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+

但是在运行时出现以下错误:“ RSIxxx [instrument],H1:'RSIxxx.mq4'中的数组超出范围(55,26)

此行参考:

ExtMapBufferCustomIndicator[i]= iCustom(NULL,0,s,20,40,0,0);

NB原始指示器工作正常

即使通过简化代码也能保持错误!

即使通过删除对外部代码的引用并用重新计算原始指标来代替它,也会出现相同的问题

感谢所有建议!

编辑2019-02-09

为了澄清问题,并回答最初的两个响应者,此代码出现相同的错误:

//+------------------------------------------------------------------+
//|                                  Copyright © 2019, Andy Thompson |
//|                                   mailto:andydoc1@googlemail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Andy Thompson"
#property link      "mailto:andydoc1@googlemail.com"
#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_width1 2

//---- buffers
double intCalcxxx[];
double ExtMapBufferRSIxxx[];
int i;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBufferRSIxxx);
   SetIndexLabel(0,"RSIxxx");
   ArraySetAsSeries(intCalcxxx,true);
   IndicatorShortName("RSI of xxx: RSIxxx");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int counted_bars=IndicatorCounted();
   if(counted_bars < 0)  return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   if(counted_bars==0) limit-=15;
//   printf(limit);
//---- main loop
   for(i=0; i<1000; i++)
     {
     Print(i,", ",limit);
      intCalcxxx[i]=(34.38805726*MathPow(iClose("EURUSD",0,i),0.3155)*MathPow(iClose("EURJPY",0,i),0.1891)*MathPow(iClose("EURGBP",0,i),0.3056)*MathPow(iClose("EURSEK",0,i),0.0785)*MathPow(iClose("EURCHF",0,i),0.1113))/(50.14348112*MathPow(iClose("EURUSD",0,i),-0.576)*MathPow(iClose("USDJPY",0,i),0.136)*MathPow(iClose("GBPUSD",0,i),-0.119)*MathPow(iClose("USDCAD",0,i),0.091)*MathPow(iClose("USDSEK",0,i),0.042)*MathPow(iClose("USDCHF",0,i),0.036));
     }
   for(i=0; i<1000; i++)
     {
      ExtMapBufferRSIxxx[i]=iRSIOnArray(intCalcxxx,0,14,0);
     }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+

并且代码在MetaEditor中以严格模式进行编译,没有警告或错误,这也可以解决我相信nicholishen提出的观点

1 个答案:

答案 0 :(得分:0)

当然,您必须初始化数组,因为它的大小为0 ...,这会导致“数组超出范围”(已经在i == 0中)。解决方案:

double intCalcxxx[1000];

..但是仍然存在问题(指标计算常数值),但至少没有运行时异常!