Array out of range error message

 

Hi guys, i loathe to ask this, as i'm sure it's going to be something very simple, but I've exhausted all the avenues i (knowingly) can explore. I''m receiving an out of range error message, located at (according to the info provided in the terminal experts tab) Ln 222, Col 25. This is the line where i state the condition:(if) HA_Close_Buffer[i-4]>HA_Open_Buffer[i-4])​. So, referencing "i' is fine, and as is  "i-1'', ''í-2''  and ''i-3'', however, when we get to ''í-4'' the error occurs. Whats going on? Why is it complaining about trying to receive the value from that array index position?

I've ran ArraySize, pre and post statement, and it's displaying the correct number of index elements and as the arrays i'm using are buffers i don't believe i need to do any additional ''sizing''. I've totally ran out of ideas as to why this is occurring now; other articles/forums state that this is usually due to the coder defined values which stipulate the for loops operation, but, I can't see what i can change there to rectify the issue either as the index positions info i'm trying to retrieve, as far as i can see, should be there and available. If anyone's able to explain whats taking place i'll be forever in their debt; I've been staring at this for 2 days now and i think my brain is on the precipice of exploding! Thanks all.

//+------------------------------------------------------------------+
//|                                         Lesson - Indicator 3.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Indicator basic properties                                       |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2016 Carl Carver Software. All Rights Reserved."
#property link      "carl_carver@hotmail.com"
#property version   "1.00"
#property description "Heiken Ashi Trend Detector"
#property strict
#property indicator_chart_window
#property indicator_buffers 10

//+------------------------------------------------------------------+
//| Indicator format properties                                      |
//+------------------------------------------------------------------+
//HA Properties
#property indicator_color1 clrBlack
#property indicator_color2 clrRed
#property indicator_color3 clrLimeGreen
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 3
#property indicator_width4 3

//Swing Properties
#property indicator_color5 clrBlack
#property indicator_color6 clrBlack
#property indicator_color7 clrBlack
#property indicator_color8 clrBlack
#property indicator_color9 clrBlack
#property indicator_color10 clrBlack
#property indicator_width5 2
#property indicator_width6 2
#property indicator_width7 2
#property indicator_width8 2
#property indicator_width9 2
#property indicator_width10 2 

//+------------------------------------------------------------------+
//| User inputs                                                      |
//+------------------------------------------------------------------+
input color Shadow=clrBlack;
input color Bear_Body = clrRed;
input color Bull_Body = clrLimeGreen;

//+------------------------------------------------------------------+
//| Data array buffers                                               |
//+------------------------------------------------------------------+
double HA_High_Buffer[];
double HA_Low_Buffer[];
double HA_Open_Buffer[];
double HA_Close_Buffer[];

double Swing_1_Buffer[];    //Wingding symbol code: 140 - On-screen symbol: 1
double Swing_2_Buffer[];    //Wingding symbol code: 141 - On-screen symbol: 2
double Swing_LL_Buffer[];   //Wingding symbol code: 142 - On-screen symbol: 3
double Swing_LH_Buffer[];   //Wingding symbol code: 143 - On-screen symbol: 4
double Swing_HL_Buffer[];   //Wingding symbol code: 144 - On-screen symbol: 5
double Swing_HH_Buffer[];   //Wingding symbol code: 145 - On-screen symbol: 6
//+------------------------------------------------------------------+
//| Custom indicator initialization function/buffer mapping          |
//|------------------------------------------------------------------|
void OnInit(void)
  {
   IndicatorShortName("Heiken Ashi");
   IndicatorDigits(Digits());

//HA Buffers
   SetIndexBuffer(0,HA_High_Buffer);
   SetIndexStyle(0,DRAW_HISTOGRAM,0,1,Shadow);
   SetIndexDrawBegin(0,10);
   SetIndexLabel(0,"HA High");

   SetIndexBuffer(1,HA_Low_Buffer);
   SetIndexStyle(1,DRAW_HISTOGRAM,0,1,Shadow);
   SetIndexDrawBegin(1,10);
   SetIndexLabel(1,"HA Low");

   SetIndexBuffer(2,HA_Open_Buffer);
   SetIndexStyle(2,DRAW_HISTOGRAM,0,3,Bear_Body);
   SetIndexDrawBegin(2,10);
   SetIndexLabel(2,"HA Open");

   SetIndexBuffer(3,HA_Close_Buffer);
   SetIndexStyle(3,DRAW_HISTOGRAM,0,3,Bull_Body);
   SetIndexDrawBegin(3,10);
   SetIndexLabel(3,"HA Close");

//Swing Buffers   
   SetIndexBuffer(4,Swing_1_Buffer);
   SetIndexStyle(4,DRAW_ARROW);
   SetIndexArrow(4,140);
   SetIndexLabel(4,"Sw. Low");

   SetIndexBuffer(5,Swing_2_Buffer);
   SetIndexStyle(5,DRAW_ARROW);
   SetIndexArrow(5,141);
   SetIndexLabel(5,"Sw. High");

   SetIndexBuffer(6,Swing_LL_Buffer);
   SetIndexStyle(6,DRAW_ARROW);
   SetIndexArrow(6,142);
   SetIndexLabel(6,"Sw. Lower Low");

   SetIndexBuffer(7,Swing_LH_Buffer);
   SetIndexStyle(7,DRAW_ARROW);
   SetIndexArrow(7,143);
   SetIndexLabel(7,"Sw. Lower High");

   SetIndexBuffer(8,Swing_HL_Buffer);
   SetIndexStyle(8,DRAW_ARROW);
   SetIndexArrow(8,144);
   SetIndexLabel(8,"Sw. Higher Low");

   SetIndexBuffer(9,Swing_HH_Buffer);
   SetIndexStyle(9,DRAW_ARROW);
   SetIndexArrow(9,145);
   SetIndexLabel(9,"Sw. Higher High");
  }
//+------------------------------------------------------------------+
//| Heiken Ashi program                                              |
//+------------------------------------------------------------------+
int OnCalculate
(
 const int rates_total,
 const int prev_calculated,

 //+------------------------------------------------------------------+
 //| Candle information data arrays                                   |
 //+------------------------------------------------------------------+
 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[]
 )
  {

//+------------------------------------------------------------------+
//| Counting from 0 to rates total                                   |
//+------------------------------------------------------------------+
//All arrays are set as standard indexing (NOT series/reversed indexing)
//Price arrays
   ArraySetAsSeries(open,false);
   ArraySetAsSeries(high,false);
   ArraySetAsSeries(low,false);
   ArraySetAsSeries(close,false);

//Heiken Ashi arrays
   ArraySetAsSeries(HA_High_Buffer,false);
   ArraySetAsSeries(HA_Low_Buffer,false);
   ArraySetAsSeries(HA_Open_Buffer,false);
   ArraySetAsSeries(HA_Close_Buffer,false);

//Swing Arrays
   ArraySetAsSeries(Swing_1_Buffer,false);
   ArraySetAsSeries(Swing_2_Buffer,false);
   ArraySetAsSeries(Swing_LL_Buffer,false);
   ArraySetAsSeries(Swing_LH_Buffer,false);
   ArraySetAsSeries(Swing_HL_Buffer,false);
   ArraySetAsSeries(Swing_HH_Buffer,false);

//+------------------------------------------------------------------+
//| Heikin Ashi candle calculation variables                         |
//+------------------------------------------------------------------+
   int i=0, Completed_Bars=IndicatorCounted();
   double HA_Open, HA_High, HA_Low, HA_Close;

//+------------------------------------------------------------------+
//| Error check - sufficient bars on chart to complete calculations? |
//|                                                                  |
//+------------------------------------------------------------------+
   if(Bars<1) return(-1);

//Setting the first candle
   if(open[0]<close[0] || open[0]>close[0])
     {
      HA_Low_Buffer[0]=low[0];
      HA_High_Buffer[0]=high[0];
      HA_Open_Buffer[0]=open[0];
      HA_Close_Buffer[0]=close[0];
     }

//Main HA candle calculation/loop
   for(i=1; i<Bars; i++)
     {
      HA_Open=(HA_Open_Buffer[i-1]+HA_Close_Buffer[i-1])/2;
      HA_Close=(open[i]+high[i]+low[i]+close[i])/4;
      HA_High=MathMax(high[i],MathMax(HA_Open,HA_Close));
      HA_Low=MathMin(low[i],MathMin(HA_Open,HA_Close));

      //Candle printing instruction
      if(HA_Open<HA_Close || HA_Open>HA_Close)
        {
         HA_Low_Buffer[i]=HA_Low;
         HA_High_Buffer[i]=HA_High;
         HA_Open_Buffer[i]=HA_Open;
         HA_Close_Buffer[i]=HA_Close;
        }
           
//+------------------------------------------------------------------+
//| Swing Detection Variables                                        |
//+------------------------------------------------------------------+   
   //int Swing_Rule=0;
   //double Swing_1, Swing_2, Swing_LL, Swing_LH, Swing_HL, Swing_HH;
   double Lowest_Bar_Price=Low[iLowest(NULL,0,MODE_LOW,8,2)];
   double Highest_Bar_Price=High[iHighest(NULL,0,MODE_HIGH,8,2)];
   int Lowest_Bar=Bars-1-iLowest(NULL,0,MODE_LOW,8,2);
   int Highest_Bar=Bars-1-iHighest(NULL,0,MODE_HIGH,8,2);
   
      if(HA_Close_Buffer[i-1]<HA_Open_Buffer[i-1] && 
         HA_Close_Buffer[i-2]<HA_Open_Buffer[i-2] && 
         HA_Close_Buffer[i-3]>HA_Open_Buffer[i-3] && 
         HA_Close_Buffer[i-4]>HA_Open_Buffer[i-4])
        {
         Swing_1_Buffer[i]=HA_Low_Buffer[i];
        }
        
//Keep comments section in the "for" loop for the time being - keeps data on chart window accurate        
   Comment
   ("i/0: ",i,
    "\nRates Total: ",rates_total,
    "\nPrev Calc: ",prev_calculated,
    "\nBars: ",Bars,
    "\nCompleted Bars: ",Completed_Bars,
    "\nSpecified Array Size: ", ArraySize (HA_Close_Buffer),
    "\nLowest Bar# ",iLowest(NULL,0,MODE_LOW,8,2),
    "\nBar# ",iLowest(NULL,0,MODE_LOW,8,2)," is equal to candle ",Lowest_Bar,
    "\nBar# ",iLowest(NULL,0,MODE_LOW,8,2)," Low Price: ",Lowest_Bar_Price,
    "\nBar# ",iLowest(NULL,0,MODE_LOW,8,2)," High Price: ",HA_High_Buffer[Lowest_Bar],
    "\nCurrent HA High: ",HA_High_Buffer[i],
    "\nCurrent HA Low: ",HA_Low_Buffer[i],
    "\nSwing_1_Buffer Vale: ",Swing_1_Buffer[i]);
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+

 

 

 

 


 

Your cycle:

for(i=1; i<Bars; i++)


So,

HA_Close_Buffer[i-4]>HA_Open_Buffer[i-4]

 equals with

HA_Close_Buffer[-3]>HA_Open_Buffer[-3] 


-3 is out of range

 
   for(i=1; i<Bars; i++)

When i==1

i-2 will be -1

The current bar is bar[0], bar[-1] does not exist so the out of range error

 
Gentlemen, thank you, totally obvious! This does however prompt me to ask a further question - somewhat related: Using the same code above - mitigating the if statement so that only the Heiken Ashi code is running (please see revised code below) - when you scroll as far to the left of the chart as is possible there are 10 candles that are not printed on the chart, however, the values exist in the data window when you scroll over the *missing* bars. Do you have any idea why the bars are not printed on the chart? I can't help but wonder if this is going to impact any revisions i make to the for loop. Thanks again. 
//+------------------------------------------------------------------+
//|                                         Lesson - Indicator 3.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Indicator basic properties                                       |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2016 Carl Carver Software. All Rights Reserved."
#property link      "carl_carver@hotmail.com"
#property version   "1.00"
#property description "Heiken Ashi Trend Detector"
#property strict
#property indicator_chart_window
#property indicator_buffers 10

//+------------------------------------------------------------------+
//| Indicator format properties                                      |
//+------------------------------------------------------------------+
//HA Properties
#property indicator_color1 clrBlack
#property indicator_color2 clrRed
#property indicator_color3 clrLimeGreen
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 3
#property indicator_width4 3

//Swing Properties
#property indicator_color5 clrBlack
#property indicator_color6 clrBlack
#property indicator_color7 clrBlack
#property indicator_color8 clrBlack
#property indicator_color9 clrBlack
#property indicator_color10 clrBlack
#property indicator_width5 2
#property indicator_width6 2
#property indicator_width7 2
#property indicator_width8 2
#property indicator_width9 2
#property indicator_width10 2 

//+------------------------------------------------------------------+
//| User inputs                                                      |
//+------------------------------------------------------------------+
input color Shadow=clrBlack;
input color Bear_Body = clrRed;
input color Bull_Body = clrLimeGreen;

//+------------------------------------------------------------------+
//| Data array buffers                                               |
//+------------------------------------------------------------------+
double HA_High_Buffer[];
double HA_Low_Buffer[];
double HA_Open_Buffer[];
double HA_Close_Buffer[];

double Swing_1_Buffer[];    //Wingding symbol code: 140 - On-screen symbol: 1
double Swing_2_Buffer[];    //Wingding symbol code: 141 - On-screen symbol: 2
double Swing_LL_Buffer[];   //Wingding symbol code: 142 - On-screen symbol: 3
double Swing_LH_Buffer[];   //Wingding symbol code: 143 - On-screen symbol: 4
double Swing_HL_Buffer[];   //Wingding symbol code: 144 - On-screen symbol: 5
double Swing_HH_Buffer[];   //Wingding symbol code: 145 - On-screen symbol: 6
//+------------------------------------------------------------------+
//| Custom indicator initialization function/buffer mapping          |
//|------------------------------------------------------------------|
void OnInit(void)
  {
   IndicatorShortName("Heiken Ashi");
   IndicatorDigits(Digits());

//HA Buffers
   SetIndexBuffer(0,HA_High_Buffer);
   SetIndexStyle(0,DRAW_HISTOGRAM,0,1,Shadow);
   SetIndexDrawBegin(0,10);
   SetIndexLabel(0,"HA High");

   SetIndexBuffer(1,HA_Low_Buffer);
   SetIndexStyle(1,DRAW_HISTOGRAM,0,1,Shadow);
   SetIndexDrawBegin(1,10);
   SetIndexLabel(1,"HA Low");

   SetIndexBuffer(2,HA_Open_Buffer);
   SetIndexStyle(2,DRAW_HISTOGRAM,0,3,Bear_Body);
   SetIndexDrawBegin(2,10);
   SetIndexLabel(2,"HA Open");

   SetIndexBuffer(3,HA_Close_Buffer);
   SetIndexStyle(3,DRAW_HISTOGRAM,0,3,Bull_Body);
   SetIndexDrawBegin(3,10);
   SetIndexLabel(3,"HA Close");

//Swing Buffers   
   SetIndexBuffer(4,Swing_1_Buffer);
   SetIndexStyle(4,DRAW_ARROW);
   SetIndexArrow(4,140);
   SetIndexLabel(4,"Sw. Low");

   SetIndexBuffer(5,Swing_2_Buffer);
   SetIndexStyle(5,DRAW_ARROW);
   SetIndexArrow(5,141);
   SetIndexLabel(5,"Sw. High");

   SetIndexBuffer(6,Swing_LL_Buffer);
   SetIndexStyle(6,DRAW_ARROW);
   SetIndexArrow(6,142);
   SetIndexLabel(6,"Sw. Lower Low");

   SetIndexBuffer(7,Swing_LH_Buffer);
   SetIndexStyle(7,DRAW_ARROW);
   SetIndexArrow(7,143);
   SetIndexLabel(7,"Sw. Lower High");

   SetIndexBuffer(8,Swing_HL_Buffer);
   SetIndexStyle(8,DRAW_ARROW);
   SetIndexArrow(8,144);
   SetIndexLabel(8,"Sw. Higher Low");

   SetIndexBuffer(9,Swing_HH_Buffer);
   SetIndexStyle(9,DRAW_ARROW);
   SetIndexArrow(9,145);
   SetIndexLabel(9,"Sw. Higher High");
  }
//+------------------------------------------------------------------+
//| Heiken Ashi program                                              |
//+------------------------------------------------------------------+
int OnCalculate
(
 const int rates_total,
 const int prev_calculated,

 //+------------------------------------------------------------------+
 //| Candle information data arrays                                   |
 //+------------------------------------------------------------------+
 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[]
 )
  {

//+------------------------------------------------------------------+
//| Counting from 0 to rates total                                   |
//+------------------------------------------------------------------+
//All arrays are set as standard indexing (NOT series/reversed indexing)
//Price arrays
   ArraySetAsSeries(open,false);
   ArraySetAsSeries(high,false);
   ArraySetAsSeries(low,false);
   ArraySetAsSeries(close,false);

//Heiken Ashi arrays
   ArraySetAsSeries(HA_High_Buffer,false);
   ArraySetAsSeries(HA_Low_Buffer,false);
   ArraySetAsSeries(HA_Open_Buffer,false);
   ArraySetAsSeries(HA_Close_Buffer,false);

//Swing Arrays
   ArraySetAsSeries(Swing_1_Buffer,false);
   ArraySetAsSeries(Swing_2_Buffer,false);
   ArraySetAsSeries(Swing_LL_Buffer,false);
   ArraySetAsSeries(Swing_LH_Buffer,false);
   ArraySetAsSeries(Swing_HL_Buffer,false);
   ArraySetAsSeries(Swing_HH_Buffer,false);

//+------------------------------------------------------------------+
//| Heikin Ashi candle calculation variables                         |
//+------------------------------------------------------------------+
   int i=0, Completed_Bars=IndicatorCounted();
   double HA_Open, HA_High, HA_Low, HA_Close;

//+------------------------------------------------------------------+
//| Error check - sufficient bars on chart to complete calculations? |
//|                                                                  |
//+------------------------------------------------------------------+
   if(Bars<1) return(-1);

//Setting the first candle
   if(open[0]<close[0] || open[0]>close[0])
     {
      HA_Low_Buffer[0]=low[0];
      HA_High_Buffer[0]=high[0];
      HA_Open_Buffer[0]=open[0];
      HA_Close_Buffer[0]=close[0];
     }

//Main HA candle calculation/loop
   for(i=1; i<Bars; i++)
     {
      HA_Open=(HA_Open_Buffer[i-1]+HA_Close_Buffer[i-1])/2;
      HA_Close=(open[i]+high[i]+low[i]+close[i])/4;
      HA_High=MathMax(high[i],MathMax(HA_Open,HA_Close));
      HA_Low=MathMin(low[i],MathMin(HA_Open,HA_Close));

      //Candle printing instruction
      if(HA_Open<HA_Close || HA_Open>HA_Close)
        {
         HA_Low_Buffer[i]=HA_Low;
         HA_High_Buffer[i]=HA_High;
         HA_Open_Buffer[i]=HA_Open;
         HA_Close_Buffer[i]=HA_Close;
        }
           
//Keep comments section in the "for" loop for the time being - keeps data on chart window accurate        
   Comment
   ("i/0: ",i,
    "\nRates Total: ",rates_total,
    "\nPrev Calc: ",prev_calculated,
    "\nBars: ",Bars,
    "\nCompleted Bars: ",Completed_Bars,
    "\nSpecified Array Size: ", ArraySize (HA_Close_Buffer),
    "\nLowest Bar# ",iLowest(NULL,0,MODE_LOW,8,2),
    "\nCurrent HA High: ",HA_High_Buffer[i],
    "\nCurrent HA Low: ",HA_Low_Buffer[i],
    "\nSwing_1_Buffer Vale: ",Swing_1_Buffer[i]);
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
SetIndexDrawBegin(0,10);
 
Thank you ggekko. Stupid thing to miss. 
Reason: