为什么我在此代码中收到数组超出范围错误?
Why am I getting an array out of range error in this code?
初始问题
我正在尝试绘制指标“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);
注意原来的指标工作正常
即使通过简化代码,错误仍然存在!
即使删除对外部代码的引用并将其替换为重新计算原始指标,也会出现同样的问题
感谢收到所有建议!
编辑 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 我相信
提出的观点
当然你必须初始化你的数组,因为它的大小是 0...这导致 "array out of range"(已经在 i == 0
)。解决方案:
double intCalcxxx[1000];
..但之后还是有问题(指标计算常数值),但至少没有运行时异常!
初始问题
我正在尝试绘制指标“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);
注意原来的指标工作正常
即使通过简化代码,错误仍然存在!
即使删除对外部代码的引用并将其替换为重新计算原始指标,也会出现同样的问题
感谢收到所有建议!
编辑 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 我相信
提出的观点当然你必须初始化你的数组,因为它的大小是 0...这导致 "array out of range"(已经在 i == 0
)。解决方案:
double intCalcxxx[1000];
..但之后还是有问题(指标计算常数值),但至少没有运行时异常!