How to find the value of previous candle in an indicator?

 

I'm new to indicators. I'd need to find the open/close of the previous candle on my indicator.

It's basically an indicator that shows buy/sell arrows on the chart.

I want to check 2 previous candles and if there is an engulfing candle I highlight with an arrow:

I'm using this code but it errors that "array out of range":

bool previousIsBearish = Open[i + 1] > Close[i + 1];


Here is my indicator:

//+------------------------------------------------------------------+
//|                                                      Candles.mq4 |
//|                               Copyright 2020, Professoft Limited |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property strict
#property indicator_chart_window
#property indicator_buffers 2

input bool ShowEngulfingCandle = true;

#property indicator_type1 DRAW_ARROW
#property indicator_width1 1
#property indicator_color1 0xFFAA00
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 1
#property indicator_color2 0x0000FF
#property indicator_label2 "Sell"

//--- indicator buffers
double         SBuffer[];
double         BBuffer[];

#define BuyArrows 0
#define SellArrows 1


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
  IndicatorBuffers(2);
  SetIndexBuffer(BuyArrows,BBuffer);
   SetIndexEmptyValue(BuyArrows, EMPTY_VALUE);
   SetIndexArrow(BuyArrows, 233);
   SetIndexLabel(BuyArrows, "Buy");   
   PlotIndexSetInteger(BuyArrows,PLOT_ARROW,159);
   

   SetIndexBuffer(SellArrows,SBuffer);
   SetIndexEmptyValue(SellArrows, EMPTY_VALUE);
   SetIndexArrow(SellArrows, 234);
   SetIndexLabel(SellArrows, "Sell");
   PlotIndexSetInteger(SellArrows,PLOT_ARROW,159);
   
   
  
   
//---
   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[])
  {
//---
    int barIndex,                           // Bar index
       Counted_bars;                // Number of counted bars
//--------------------------------------------------------------------
   
   int limit = rates_total - prev_calculated;
   if (prev_calculated > 0) limit++;
   
   for (int i = limit-1; i>=0; i--) {
       bool isBearish = Open[i] > Close[i];
       bool previousIsBearish = Open[i + 1] > Close[i + 1]; // PROBLEM IS HERE
                   
      if (isBearish && !previousIsBearish) {
          // sell buffer
          SBuffer[i]=High[i] + (10 * Point); 
      } else if (!isBearish && previousIsBearish) {
         // buy buffer
         BBuffer[i]=Low[i] - (10 * Point);  
      }
   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
   int limit = rates_total - prev_calculated;
   if (prev_calculated > 0) limit++;

       bool previousIsBearish = Open[i + 1] > Close[i + 1]; // PROBLEM IS HERE
Your lookback is one.
          How to do your lookbacks correctly #9#14 & #19
 
William Roeder #:
Your lookback is one.
          How to do your lookbacks correctly #9#14 & #19

Great, thank you sorted.