Array Out of Range problem

 

Hello.

I am trying to learn coding for 3 months. I have read many many articles and sample codes. I tried to write a multicurrency indicator. When i try to use it, i get the "array out of range" error on H4 timeframe. I have read a lot of "array out of range" explanation on this forum but i can not solve the problem (because i am a beginner). Because of this error, i can not use this indicator in EA either. 

Error occurs with the line that begins " topsgrxBuffer[i] = sqrxBuffer[i]+...... ". 

I kindly ask you to help me. Best regards.

//+------------------------------------------------------------------+
//|                                                          au1.mq5 |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "www.mql5.com"
#property link      "http:www.mql5.com"
#property indicator_separate_window
#property indicator_buffers 9
#property indicator_plots   1
#property indicator_applied_price PRICE_CLOSE

#property indicator_label1  "au1";
#property indicator_type1   DRAW_LINE
#property indicator_color1  Red
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
#include <MovingAverages.mqh>
//--- input parameters

input string   S1_Symbol            =  "GBPJPY";   
input string   S2_Symbol            =  "GBPUSD";
double   Buffer[];   
double valx[1];
double valy[1];
double xyBuffer[];
double sqrxBuffer[];
double ortxBuffer[];
double ortyBuffer[];
double topxyBuffer[];
double topsqrxBuffer[];
datetime tm1[1];
datetime tm2[1];

int MAHand;
ENUM_TIMEFRAMES TF;
int TimeX=0;
int _handle1,_handle2;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,Buffer,INDICATOR_DATA);
   SetIndexBuffer(1,valx,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,valy,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,xyBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(4,sqrxBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(5,ortxBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(6,ortyBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(7,topxyBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(8,topsqrxBuffer,INDICATOR_CALCULATIONS);
   _handle1 = iMA(S1_Symbol,0,200,0,MODE_SMA,PRICE_CLOSE); if(_handle1==INVALID_HANDLE) { IndicatorRelease(_handle1); return(INIT_FAILED); }
   _handle2 = iMA(S2_Symbol,0,200,0,MODE_SMA,PRICE_CLOSE); if(_handle2==INVALID_HANDLE) { IndicatorRelease(_handle2); return(INIT_FAILED); }
   IndicatorSetInteger(INDICATOR_DIGITS,5);   
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   if(_handle1!=INVALID_HANDLE) IndicatorRelease(_handle1);
   if(_handle2!=INVALID_HANDLE) IndicatorRelease(_handle2);
   
   Comment("comment1");
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 limit;
   static bool err=false;

   if(prev_calculated<=0 || err)
     {
      if(!SymbolSelect(S1_Symbol,true))
                  {
                     Alert("No symbol "+S1_Symbol);
                     Buffer[rates_total-1]=EMPTY_VALUE;
                     err=true;
                     return(0);
                  }
      if(!SymbolSelect(S2_Symbol,true))
                  {
                     Alert("No symbol "+S2_Symbol);
                     Buffer[rates_total-1]=EMPTY_VALUE;
                     err=true;
                     return(0);
                  }
        
      limit=1;
      
         int b1=Bars(S1_Symbol,PERIOD_CURRENT);
         int b2=Bars(S2_Symbol,PERIOD_CURRENT);
         if(b1==0 || b2==0)
                    {
                     Comment(MQL5InfoString(MQL5_PROGRAM_NAME)+": Please wait...");
                     Buffer[rates_total-1]=EMPTY_VALUE;
                     err=true;
                     return(0);
                   }
         int cr1=CopyTime(S1_Symbol,PERIOD_CURRENT,b1-1,1,tm1);
         int cr2=CopyTime(S2_Symbol,PERIOD_CURRENT,b2-1,1,tm2);
         if(cr1==-1 || cr2==-1)
                  {
                     Buffer[rates_total-1]=EMPTY_VALUE;
                     err=true;
//                   return(0);
                  }
         for(int i=0;i<rates_total;i++)
         {
            if(time[i]>=tm1[0] && time[i]>=tm2[0])
                     {
                        limit=i+1;
                        break;
                     }
         }
      if(err)
                     {
                     Comment("comment2");
                     err=false;
                     }
      PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,200);
     }
   else
     {
      limit=prev_calculated-1;
     }
     
     double   _ORTVALX[1];
     double   _ORTVALY[1];
     
   for(int i=limit;i<rates_total;i++)
    {

      CopyClose(S1_Symbol,PERIOD_CURRENT,time[i],1,valx);
      CopyClose(S2_Symbol,PERIOD_CURRENT,time[i],1,valy);
      int _copied1=CopyBuffer(_handle1,0,time[i],1,_ORTVALX);  if(_copied1!=1) continue;
      int _copied2=CopyBuffer(_handle2,0,time[i],1,_ORTVALY);  if(_copied2!=1) continue;

      if(valx[0]==0 || valy[0]==0)
        {
         Buffer[i]=Buffer[i-1];
         continue;
        } 
       
        {        
            ortxBuffer[i]=_ORTVALX[0];
            ortyBuffer[i]=_ORTVALY[0];
            xyBuffer[i]=valx[0]*valy[0];
            sqrxBuffer[i]=valx[0]*valx[0];
            topsqrxBuffer[i]=sqrxBuffer[i]+sqrxBuffer[i-1]+sqrxBuffer[i-2]+sqrxBuffer[i-3]+sqrxBuffer[i-4]+sqrxBuffer[i-5]+sqrxBuffer[i-6]+sqrxBuffer[i-7]+sqrxBuffer[i-8]+sqrxBuffer[i-9]+sqrxBuffer[i-10];
            topxyBuffer[i]=xyBuffer[i]+xyBuffer[i-1]+xyBuffer[i-2]+xyBuffer[i-3]+xyBuffer[i-4]+xyBuffer[i-5]+xyBuffer[i-6]+xyBuffer[i-7]+xyBuffer[i-8]+xyBuffer[i-9]+xyBuffer[i-10];  
            Buffer[i]=topsqrxBuffer[i]+topxyBuffer[i]+ortxBuffer[i]+ortyBuffer[i]+xyBuffer[i]-sqrxBuffer[i]-valy[0];  
                                              
        }     
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

Learn to use the debugger and how to deal with errors:

https://www.mql5.com/en/articles/654
https://www.mql5.com/en/articles/2041

This way you can find your errors yourself.

Debugging MQL5 Programs
Debugging MQL5 Programs
  • www.mql5.com
This article is intended primarily for the programmers who have already learned the language but have not fully mastered the program development yet. It reveals some debugging techniques and presents a combined experience of the author and many other programmers.
 
aunal7673:

Hello.

I am trying to learn coding for 3 months. I have read many many articles and sample codes. I tried to write a multicurrency indicator. When i try to use it, i get the "array out of range" error on H4 timeframe. I have read a lot of "array out of range" explanation on this forum but i can not solve the problem (because i am a beginner). Because of this error, i can not use this indicator in EA either. I kindly ask you to help me. Best regards.

Check the line and column it is displaying, then Print the index, that way you would see what is going above/below the range
 
Carl Schreiber #:

Learn to use the debugger and how to deal with errors:

https://www.mql5.com/en/articles/654
https://www.mql5.com/en/articles/2041

This way you can find your errors yourself.

Thank you for your reply. There is no error and warning when i compile the script. Problem occurs while using on chart.  Error line is the one that starts 'topsqrxbuffer[i]=.....'
I tried the 'arrayresize' but i think i made some new mistakes.
 
aunal7673 #: Thank you for your reply. There is no error and warning when i compile the script. Problem occurs while using on chart.  Error line is the one that starts 'topsqrxbuffer[i]=.....'
I tried the 'arrayresize' but i think i made some new mistakes.

You have not specified at which line the array out of range error is occurring, so we can only guess, but I do see several lines where there are problems, namely:

Buffer[rates_total-1]

The above would fail if "rates_total" were zero, and this can happen at the beginning when the indicator does not yet have any data ready. Always check for that condition first:

// OnCalculate event handler
   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[] )
   {
      // Make sure we have rates data
         if( rates_total < 1 ) return 0;
      
      // Other logic ...   

      // Return value of prev_calculated for next call
         return rates_total;
   };
 
Array out of range mostly happen when calculating Indicator buffer. Suppose your array A[] can hold 100 values. So your index should be from 0 to 99. If in some case you call your array A[-1] or A[100] then it'll be array out of range because your range is from 0 to 99. Check the Expert log in which line this problem happened. 
 
Carl Schreiber #:

Learn to use the debugger and how to deal with errors:

https://www.mql5.com/en/articles/654
https://www.mql5.com/en/articles/2041

This way you can find your errors yourself.

Hello again.
I have read all the comments on this page and tried to solve the problem but did not succeed. Should i write this post any other page on this forum?
 
aunal7673 #:
Hello again.
I have read all the comments on this page and tried to solve the problem but did not succeed. Should i write this post any other page on this forum?

Do not start another topic.

Show what you have tried to solve the problem.

 
aunal7673 #:
Hello again.
I have read all the comments on this page and tried to solve the problem but did not succeed.
...


Hello,

The first step to find the source of the problem,
 - I suggest that you first check it by 'Print()' the contents of the variable that I highlighted in yellow.
 - Once you know their contents, I'm sure you can continue solving the problem.

    Print("limit: ",limit," | rates_total: ",rates_total," | prev_calculated: ",prev_calculated);

    for(int i=limit;i<rates_total;i++)
    {

Because I suspect this code is asking for an unavailable sequence of array numbers.

topsqrxBuffer[i]=sqrxBuffer[i]+sqrxBuffer[i-1]+sqrxBuffer[i-2]+sqrxBuffer[i-3]+sqrxBuffer[i-4]+sqrxBuffer[i-5]+sqrxBuffer[i-6]+sqrxBuffer[i-7]+sqrxBuffer[i-8]+sqrxBuffer[i-9]+sqrxBuffer[i-10];
topxyBuffer[i]=xyBuffer[i]+xyBuffer[i-1]+xyBuffer[i-2]+xyBuffer[i-3]+xyBuffer[i-4]+xyBuffer[i-5]+xyBuffer[i-6]+xyBuffer[i-7]+xyBuffer[i-8]+xyBuffer[i-9]+xyBuffer[i-10];  


Good luck.

 

Had the same issue writing an indicator.

Solution was :

#property indicator_buffers >> total number of ANY ARRAY<< 

You are using 11 arrays in your indicator, so the total number of indicator buffers has to be 11 and

you need to set the index for each :

 SetIndexBuffer(number,name,data...calc...etc);

.

You do not need to increment the indicator plots, if you do not need more than 1.

Increment the indicator buffers count, if you need to add a new array and -of course- set a new index for each new array.

For your indicator : 

#property indicator_buffers 11

// ...
// and add :

SetIndexBuffer(9,tm1,INDICATOR_CALCULATIONS);
SetIndexBuffer(10,tm2,INDICATOR_CALCULATIONS);

//That´s it. :o)

This should solve it.

Good luck !

oli

 
double valx[1];
double valy[1];
⋮
   SetIndexBuffer(1,valx,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,valy,INDICATOR_CALCULATIONS);

Note

After binding, the dynamic array buffer[]

Those are not dynamic arrays.