Array Out Of Range error on my custon indicator at line "BU[0]=BU[1]-BU[2]/3.414+(1/3.414)*Close[0]; "
this is the code that I've translated in an indicator for mql4, but I get the error from the array range
regards
Giuseppe
I do not see your local arrays (BU[], PB[];) getting a size before being used.
this is the code that I've translated in an indicator for mql4, but I get the error from the array range
regards
Giuseppe
I do not see your local arrays (BU[], PB[];) getting a size before being used.
now I've changed the code as follow, but no output if I set the arrays to value 31, the output is a fixed flat line that doesn't change.
What about the use of the method resize? I've also doubt for the ArrayMinimum ArrayMaximum methods use
//+------------------------------------------------------------------+ //| Momentum.mq4 | //| Copyright 2005-2014, MetaQuotes Software Corp. | //| https://www.mql4.com | //+------------------------------------------------------------------+ #property copyright "2005-2014, MetaQuotes Software Corp." #property link "https://www.mql4.com" #property description "DorseyCorinnah" #property strict #property indicator_separate_window #property indicator_buffers 1 #property indicator_color1 DodgerBlue //--- input parameter //input int InpMomPeriod=31; // Momentum Period input int d=31; input int e=31; //--- buffers double ExtMomBuffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit(void) { string short_name; //--- indicator line SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,ExtMomBuffer); //--- name for DataWindow and indicator subwindow label short_name="Mom("+IntegerToString(d)+")"; IndicatorShortName(short_name); SetIndexLabel(0,short_name); //--- check for input parameter if(d<=0) { Print("Wrong input parameter Momentum Period=",d); return(INIT_FAILED); } //--- SetIndexDrawBegin(0,d); //--- initialization done return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Momentum | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { int i,limit,min,max; double R,M; double BU[31]; double PB[31]; //--- check for bars count and input parameter if(rates_total<=d || d<=0) return(0); //--- counting from 0 to rates_total ArraySetAsSeries(ExtMomBuffer,false); ArraySetAsSeries(close,false); //--- initial zero if(prev_calculated<=0) { for(i=0; i<d; i++) ExtMomBuffer[i]=0.0; limit=d; } else limit=prev_calculated-1; //--- the main loop of calculations for(i=limit; i<rates_total; i++){ // ExtMomBuffer[i]=close[i]*100/close[i-InpMomPeriod]; if (Bars<2) { BU[0]=Close[0]; } else { BU[0]=BU[1]-BU[2]/3.414+(1/3.414)*Close[0]; } //M=Average[d](close) M=iMA(NULL,0,d,0,MODE_SMA,PRICE_TYPICAL,0); PB[0]=MathAbs(BU[0]-M); min=ArrayMinimum(PB,e,0); max=ArrayMaximum(PB,e,0); R=100*(PB[0]-PB[min])/(PB[max]-PB[min]); ExtMomBuffer[i]=R; } //--- done return(rates_total); } //+------------------------------------------------------------------+
now I've changed the code as follow, but no output if I set the arrays to value 31, the output is a fixed flat line that doesn't change.
What about the use of the method resize? I've also doubt for the ArrayMinimum ArrayMaximum methods use
IndicatorBuffers() is ? ?
You are creating an Indicator so you use buffers not arrays, Buffers are series automatically so you don't need ArraySetAsSeries() for the buffer . . .
double BU[31]; double PB[31]; : BU[0]=BU[1]-BU[2]/3.414+(1/3.414)*Close[0]; : PB[0]=MathAbs(BU[0]-M);You only set BU[0] never any other element. If it was a buffer then the values would shift on a new bar. They're arrays so they don't.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Dear All,
after a deep reading of the MQL reference I've started to program and translate an existing indicator from another system (realtimepro)
This indicato get an ok from the Compile, but fail when attached to a chart.
the fail message is Array Out Of Range, and from the debugger I see is at the line of this assignment:
BU[0]=BU[1]-BU[2]/3.414+(1/3.414)*Close[0];
http://s16.postimg.org/tecvtc6gl/Array_Out_Of_Range.png
this is the full code and I'm trying to find the error, can you indicate me what to search ?
Regards,
Giuseppe