【问题标题】:Why am I getting an array out of range error in this code?为什么在此代码中出现数组超出范围错误?
【发布时间】:2019-02-08 19:40:13
【问题描述】:

最初的问题

我正在尝试绘制指标“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: array out of range in '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 提出的观点我相信

【问题讨论】:

  • 您只声明了一个缓冲区,但您试图在代码中使用两个缓冲区。
  • 大声笑 - 有两个缓冲区:``` double ExtMapBufferCustomIndicator[];双 ExtMapBufferRSICustomIndicator[]; ```
  • 错误消息直截了当:它发生在 RSIxxx.mq4(自定义指标)中......在 H1 时间范围内......我“猜测”连接到“自定义参数”(20, 40) ...
  • 否,因为当我用重新计算来替换对外部指标的调用时,错误仍在继续,...正如我在最后解释的那样
  • 没有。您只声明了一个缓冲区和两个动态数组。您必须将数组显式映射到缓冲区。你没有这样做。

标签: arrays mql4


【解决方案1】:

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

double intCalcxxx[1000];

..不过还是有问题(指标计算常数值),但至少没有运行时异常!

【讨论】:

  • 谢谢 - 当我停止将其设置为索引缓冲区时,我忽略了这一点。现在找出为什么它返回一个常量!
猜你喜欢
  • 1970-01-01
  • 2020-10-21
  • 2017-10-01
  • 1970-01-01
  • 2016-03-07
  • 2020-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多