Please require help with "array out of range error". Simple bullish/bearish indicator setup.

 

I have been trying to rectify an issue I've had for a while now and could really do with some help please. I am trying to create an indicator that shows the Bearish/Bullish Engulfing patterns (trying to build up my skills). I first created a similar FindHammerPattern() function which worked really well but my 'FindHiLoEngulfingPattern()' function keeps getting the "array out of range" error. I have tried to investigate the problem (See Print() statements) but am really confused as to what it could be. I think the issue has to do with the parameters used by the OnCalculate() function were the same number as my GetHammerPattern() function. However, in the FindHiLoEngulfingPattern() function because I am trying to retrieve TWO candles worth of data it is causing problems. I would just like to know how I could retrieve two (or more) candles worth of data. Ideally correcting my code or pointing me in the right direction would be greatly appreciated. Many thanks in advance.

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- input parameters
input   bool    inp_IsHiLoEngulfing       = false; // Find Hi & LO ENGULFING Patterns

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int   OnInit()
{

  return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| 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[])
{
  for(int i = prev_calculated-1; i < rates_total-1; i++)   
  {
    if(i < 0 || i > rates_total-1) continue; 

    if(inp_IsHiLoEngulfing) 
    {
      FindHiLoEngulfingPattern(time[i], high[i], low[i], open[i], close[i], high[i-1], low[i-1], open[i-1], close[i-1]);
      Print("i = ", i);
      Print("i-1 = ", i-1);
      Print("i: ", i, " time1: ", time[i], " highBar1: ", high[i], " lowBar1: ", low[i], " openBar1: ", open[i], " closeBar1: ", close[i]); 
      Print("i: ", i, " time1: ", time[i], " highBar2: ", high[i-1], " lowBar2: ", low[i-1], " openBar2: ", open[i-1], " closeBar2: ", close[i-1]);
    } 

    Print(i);
  } 
  return(rates_total);
}

//+------------------------------------------------------------------+
//| Custom indicator De-initialization function                      |
//+------------------------------------------------------------------+
void  OnDeinit(const int reason)
{
  ObjectsDeleteAll(0, "PATTERN");             
  ChartRedraw(0);                             
}

//+------------------------------------------------------------------+
//| Custom functions                                                 |
//+------------------------------------------------------------------+

int   FindHiLoEngulfingPattern(datetime time, 
                                   double highBar1, 
                                   double lowBar1, 
                                   double openBar1, 
                                   double closeBar1, 
                                   double highBar2, 
                                   double lowBar2, 
                                   double openBar2, 
                                   double closeBar2) 
{           
  // FIND BULLISH ENGULFING PATTERN - Using the HIGH/LOW
  if(openBar1 < closeBar1)                                    
  {
    if(openBar2 > closeBar2)                                  
    {
      if(highBar1 > highBar2 && lowBar1 < lowBar2)  
      {
        CreateArrowObj(time, lowBar1, 233, 1, clrGreen, " H/L BULL ENGULFING");
        return 1;
      }
    }
  }
  // FIND BEARISH ENGULFING PATTERN - USING THE HIGH/LOW
  if(openBar1 > closeBar1)                                    
  {
    if(openBar2 < closeBar2)                                  
    {
      if(highBar1 > highBar2 && lowBar1 < lowBar2)  
      {
        CreateArrowObj(time, highBar1, 234, -1, clrRed, "  H/L BEAR ENGULFING");  
        return -1;
      }
    }
  }
return 0;
}

void  CreateArrowObj(datetime time, double price, int arrowCode, int direction, color clr, string txt)  
{
  string objName = "";                                                                
  StringConcatenate(objName, "PATTERN", time, " at ", DoubleToString(price, _Digits), " (", arrowCode, ")");
  if(ObjectCreate(0, objName, OBJ_ARROW, 0, time, price))                             
  {
    ObjectSetInteger(0, objName, OBJPROP_ARROWCODE, arrowCode);                       
    ObjectSetInteger(0, objName, OBJPROP_COLOR, clr);                                 
    if(direction > 0) ObjectSetInteger(0, objName, OBJPROP_ANCHOR, ANCHOR_TOP);     
    if(direction < 0) ObjectSetInteger(0, objName, OBJPROP_ANCHOR, ANCHOR_BOTTOM);     
  }
  string objNameDesc = objName+txt;                                                   
  if(ObjectCreate(0, objNameDesc, OBJ_TEXT, 0, time, price))                          
  {
    ObjectSetString(0, objNameDesc, OBJPROP_TEXT, " "+txt);                           
    ObjectSetInteger(0, objNameDesc, OBJPROP_COLOR, clr);                             
    if(direction > 0) ObjectSetInteger(0, objNameDesc, OBJPROP_ANCHOR, ANCHOR_LEFT_UPPER); 
    if(direction < 0) ObjectSetInteger(0, objNameDesc, OBJPROP_ANCHOR, ANCHOR_LEFT_LOWER); 
  }
}
 

Think about it — when "i = 0", what do you think "i - 1" will give?

It will give the result "-1". Is there such an index in your array? Does "array[-1]" make sense when the lowest it can go is "array[0]"?

No, so it will give an "array out of range" error.

 
Fernando Carreiro #:

Think about it — when "i = 0", what do you think "i - 1" will give?

It will give the result "-1". Is there such an index in your array? Does "array[-1]" make sense when the lowest it can go is "array[0]"?

No, so it will give an "array out of range" error.

OMG !!!! I figured it out. Thank you so much Fernando, just shows what a fresh set of eyes can do. All I simply did was amend my if statement to make it so 'i' < 1 (see below). Been looking at this for days and so 'chuffed' it is now working. Thank you again. (I'm making the assumption it is correct because it has started showing me my engulfing patterns)    

  for(int i = prev_calculated-1; i < rates_total-1; i++)   
  {
    if(i < 1 || i > rates_total-1) continue; 

    if(inp_IsHiLoEngulfing) 
    {
Reason: