Download MetaTrader 5

Please help for the "array out of range" error

To add comments, please log in or register
Make profit on MQL5 services. It's easy!
OzanK
70
OzanK 2015.03.26 20:16 

Hi ...


My code is the following ... and I'm getting an error saying that "array out of range in '1.mq5' (66,33)" 

It is the hand-numbered row in the code.

How can I correct this error?


Thanks a lot.


//+------------------------------------------------------------------+
//|                                                            1.mq5 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "Label1"
#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  clrSaddleBrown,clrCrimson,clrDeepPink,clrCornflowerBlue,clrSteelBlue,clrMediumBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

input int maPeriod = 10;
input int maShift = 0;
input ENUM_MA_METHOD maMethod = MODE_SMA;
input ENUM_APPLIED_PRICE maPrice = PRICE_CLOSE;

//--- indicator buffers
double         Label1Buffer[];
double         Label1Colors[];

double maBuffer[];
int maHandle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA);
   SetIndexBuffer(1,Label1Colors,INDICATOR_COLOR_INDEX);
   
   ArraySetAsSeries(Label1Buffer,true);
   ArraySetAsSeries(Label1Colors,true);
   
   ArraySetAsSeries(maBuffer, true);
   maHandle = iMA(_Symbol,0,maPeriod,maShift,maMethod,maPrice);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//---
   int bars = rates_total - 1;
   if(prev_calculated > 0)
     {
      bars = rates_total - prev_calculated;
     }
     
   CopyBuffer(maHandle,0,0,bars,maBuffer);
   
   for(int i = bars; i >= 0; i--)
     {
66    Label1Buffer[i] = maBuffer[i];
      if(((maBuffer[i] - maBuffer[i + 1]) / maBuffer[i + 1]) < ((maBuffer[i + 1] - maBuffer[i + 2]) / maBuffer[i + 2])) Label1Colors[i] = 0;
      if(maBuffer[i] < maBuffer[i + 1]) Label1Colors[i] = 1;
      if(price[i] < maBuffer[i]) Label1Colors[i] = 2;
      if(((maBuffer[i] - maBuffer[i + 1]) / maBuffer[i + 1]) > ((maBuffer[i + 1] - maBuffer[i + 2]) / maBuffer[i + 2])) Label1Colors[i] = 6;
      if(maBuffer[i] > maBuffer[i + 1]) Label1Colors[i] = 5;
      if(price[i] > maBuffer[i]) Label1Colors[i] = 4;      
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
InvestSoft
8592
Janusz Trojca 2015.03.26 20:35  
OzanK:

Hi ...


My code is the following ... and I'm getting an error saying that "array out of range in '1.mq5' (66,33)" 

It is the hand-numbered row in the code.

How can I correct this error?


Thanks a lot.


You must set in CopyBuffer as 4th param amount to copy not last index so it should be:

CopyBuffer(maHandle,0,0,bars+1,maBuffer);

 In loop, if you get value from previous index (maBuffer[i+1]) you can't start loop from last index. You must start from one before last:

for(int i = bars-1; i >= 0; i--)
OzanK
70
OzanK 2015.03.26 20:56  
forex_trader:

You must set in CopyBuffer as 4th param amount to copy not last index so it should be:

 In loop, if you get value from previous index (maBuffer[i+1]) you can't start loop from last index. You must start from one before last:

Thank you ... I solved the problem by:


for(int i = bars-3; i >= 0; i--)
Bahrom Juraev
15989
Bahrom Juraev 2015.03.27 02:15  
good luck
InvestSoft
8592
Janusz Trojca 2015.03.27 10:27  
OzanK:

Thank you ... I solved the problem by:


If the oldest bar is insignificant, you can omit it and start from bars-3.
OzanK
70
OzanK 2015.03.28 18:00  
forex_trader:
If the oldest bar is insignificant, you can omit it and start from bars-3.

Thank you forex_trader ... I got the point relating with CopyBuffer().


I would like to ask one more question ...

When populating a buffer of a custom indicator with the buffer of an another indicator, should we use PLOT_DRAW_BEGIN in our code?

Let's say ... we will use iMA with the period of 10 for populating our custom indicator's buffer.

In this case, should we use PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,9) for a more robust indicator?


My reason for asking ... if we do not use PLOT_DRAW_BEGIN, the drawing of our custom indicator is also starting at the 10th bar. So, it seems that there is no need to use PLOT_DRAW_BEGIN at all.

InvestSoft
8592
Janusz Trojca 2015.03.30 11:20  
OzanK:

Thank you forex_trader ... I got the point relating with CopyBuffer().


I would like to ask one more question ...

When populating a buffer of a custom indicator with the buffer of an another indicator, should we use PLOT_DRAW_BEGIN in our code?

Let's say ... we will use iMA with the period of 10 for populating our custom indicator's buffer.

In this case, should we use PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,9) for a more robust indicator?


My reason for asking ... if we do not use PLOT_DRAW_BEGIN, the drawing of our custom indicator is also starting at the 10th bar. So, it seems that there is no need to use PLOT_DRAW_BEGIN at all.

PLOT_DRAW_BEGIN you can use to avoid plotting indicator on the first N bars of the history. If Indicator's values for this N bars is NULL value, you must not use PLOT_DRAW_BEGIN (the same effect).
To add comments, please log in or register