Why am I getting an array out of range error in this code?

无人久伴 提交于 2019-12-24 16:43:41

问题


Initial Problem

I am trying to plot the RSI of an indicator, "xxx.mq4", as follows:

#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);
  }
//+------------------------------------------------------------------+

but I get the following error on running: "RSIxxx [instrument],H1: array out of range in 'RSIxxx.mq4' (55,26)

The reference is to this line:

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

NB The original indicator works fine

Persistance of error even by simplifying code!

Even by removing the reference to the external code and replacing it with a recalculation of the original indicator the same problem occurs

All suggestions gratefully received!

Edit 2019-02-09

In order to clarify, and in answer to the initial two responders, the same error occurs with this code:

//+------------------------------------------------------------------+
//|                                  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);
  }
//+------------------------------------------------------------------+

and the code compiles in MetaEditor in strict mode with no warnings or errors which would also address the point made by nicholishen I believe


回答1:


Of course you have to initialize your array, since as is, it has the size 0... which leads to "array out of range" (already at i == 0). Solution:

double intCalcxxx[1000];

..but then there are still problems (indicator calculates constant value), but at least no runtime exceptions!



来源:https://stackoverflow.com/questions/54599160/why-am-i-getting-an-array-out-of-range-error-in-this-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!