Problem with DRAW_COLOR_CANDLES

 

Hi all. I 'm going to try DRAW_COLOR_CANDLES in my indicator. I've written following which works correctly in coloring Engulf candles:


#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots   1
//--- plot Candle
#property indicator_label1  "Candle"
#property indicator_type1   DRAW_COLOR_CANDLES
#property indicator_color1  clrNONE, clrWheat, clrMagenta
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- indicator buffers
double         CandleBuffer1[];
double         CandleBuffer2[];
double         CandleBuffer3[];
double         CandleBuffer4[];
double         CandleColor[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,CandleBuffer1,INDICATOR_DATA);
   SetIndexBuffer(1,CandleBuffer2,INDICATOR_DATA);
   SetIndexBuffer(2,CandleBuffer3,INDICATOR_DATA);
   SetIndexBuffer(3,CandleBuffer4,INDICATOR_DATA);
   SetIndexBuffer(4,CandleColor,INDICATOR_COLOR_INDEX);
//---
   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 start = prev_calculated;
   if(start>=rates_total)
      start = rates_total-1;

   for(int i = start; i<rates_total; i++){
      CandleBuffer1[i]=open[i];
      CandleBuffer2[i]=high[i];
      CandleBuffer3[i]=low[i];
      CandleBuffer4[i]=close[i];
      
      CandleColor[i]=0;
     
      bool StrongEngulf_Up =  close[i]>open[i] && close[i-1]<=open[i-1];
      bool StrongEngulf_Down =  close[i]<open[i] && close[i-1]>=open[i-1];
      
      if (StrongEngulf_Up) CandleColor[i]=1;
      if (StrongEngulf_Down) CandleColor[i]=2;
    }

//--- return value of prev_calculated for next call
   return(rates_total);
  }


Now, if i change small piece of code -as follows- i get strange behavior form indicator. It plots some vertical lines in my chart filling form top  to down of monitor.


double open_pre = open[i-1];   
      
bool StrongEngulf_Up =  close[i]>open[i] && close[i-1]<=open_pre;
bool StrongEngulf_Down =  close[i]<open[i] && close[i-1]>=open_pre;

In fact, thr code is more sophisticated. I've simplified it to show the exact problem.

Q1: Why this occurs?
Q1: Why should i use index "i-1" to refer to previous candle?


Thanks in  advance for your help