为什么我得到一个数组超出范围错误的代码?

问题描述 投票:1回答:1

Initial Problem

我试图绘制一个指标,“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 [仪器],H1:阵列超出范围‘RSIxxx.mq4’(55,26)

引用是针对这一行:

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

NB原来的指标工作正常

Persistance of error even by simplifying code!

甚至通过除去参考外部代码,并用同样的问题出现在原始指示器的重新计算替换它

所有建议感激地接受!

Edit 2019-02-09

为了澄清,并在回答为初始2个反应,在该代码发生相同错误:

//+------------------------------------------------------------------+
//|                                  Copyright © 2019, Andy Thompson |
//|                                   mailto:[email protected] |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Andy Thompson"
#property link      "mailto:[email protected]"
#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我相信作出警告或错误,这也将解决点严格模式

arrays mql4
1个回答
0
投票

当然,你必须初始化数组,自成,它具有尺寸0 ...这将导致“数组越界”(已在i == 0)。解:

double intCalcxxx[1000];

..但随后仍然存在问题(指标计算的固定值),但至少没有运行时异常!

© www.soinside.com 2019 - 2024. All rights reserved.