How to fill empty cell in buffer with prior value

 

I found a Pivot indicator, that identifies the highest candle among a set or before and after candles. It only marks the position of the pivot candle. This is the same idea of the fractal indicator, with the difference that the pivot indicator is flexible on how many candles on the right and left to search for.

I wonder if an expert among you could help me with the missing line of code, so that each empty cell  carries the latest pivot...

I am adding this line, but it does not work:

if (phBuffer[0]==EMPTY_VALUE) phBuffer[0]= phBuffer[1];

Here is the full code...

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot ph
#property indicator_label1  "UpperPivot"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrBlue
#property indicator_width1  6
//--- plot pl
#property indicator_label2  "LowerPivot"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrRed
#property indicator_width2  6
//--- input parameters
input int _right=10;
input int _left=10;
//--- indicator buffers
double         phBuffer[];
double         plBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,phBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,plBuffer,INDICATOR_DATA);

   ArraySetAsSeries(phBuffer,true);
   ArraySetAsSeries(plBuffer,true);
//---
   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[])
  {
   ArraySetAsSeries(high, true);
   ArraySetAsSeries(low, true);
   int BARS=MathMax(Bars(_Symbol, PERIOD_CURRENT)-(_right+_left)-prev_calculated,1);
   pivothigh(high, phBuffer, _left, _right, 0, BARS);
   pivotlow(low, plBuffer, _left, _right, 0, BARS);
   
   //these next two lines shoukld be the fillers
   if (phBuffer[0]==EMPTY_VALUE) phBuffer[0]= phBuffer[1];
   if (plBuffer[0]==EMPTY_VALUE) plBuffer[0]= plBuffer[1];
   
   return(rates_total);
  }
//+------------------------------------------------------------------+


void pivothigh(const double &high[], double &output[], int left, int right, int start_pos, int count)
{
   for(int index=start_pos;index<start_pos+count;index++)
   {
      bool next_index=false;
      int i=index+right;
      for(int j=i+1;j<i+1+left;j++)
      {
         if(j>Bars(_Symbol, PERIOD_CURRENT)-1) break;
         if(high[j]>high[i]) 
         {
            output[index] = EMPTY_VALUE;
            next_index=true;
            break;
         }
      }
      if(next_index==true) continue;
      for(int j=i-1;j>i-1-right;j--)
      {
         if(j<0) break;
         if(high[j]>high[i]) 
         {
            output[index] = EMPTY_VALUE;
            next_index=true;
            break;
         }
      }
      if(next_index==true) continue;
      output[index] = high[i];
   }
}

void pivotlow(const double &low[], double &output[] , int left, int right, int start_pos, int count)
{
   for(int index=start_pos;index<start_pos+count;index++)
   {
      bool next_index=false;
      int i=index+right;
      for(int j=i+1;j<i+1+left;j++)
      {
         if(j>Bars(_Symbol, PERIOD_CURRENT)-1) break;
         if(low[j]<low[i]) 
         {
            output[index] = EMPTY_VALUE;
            next_index=true;
            break;
         }
      }
      if(next_index==true) continue;
      for(int j=i-1;j>i-1-right;j--)
      {
         if(j<0) break;
         if(low[j]<low[i])
         {
            output[index] = EMPTY_VALUE;
            next_index=true;
            break;
         }
      }
      if(next_index==true) continue;
      output[index] = low[i];
   }
}
 
void pivothigh(const double &high[], double &output[], int left, int right, int start_pos, int count)
{
   for(int index=start_pos+count-1;index>=start_pos;index--)
   {  
      if(output[index+1]==EMPTY_VALUE) output[index+1]=output[index+2];
      bool next_index=false;
      int i=index+right;
      for(int j=i+1;j<i+1+left;j++)
      {
...
}

void pivotlow(const double &low[], double &output[] , int left, int right, int start_pos, int count)
{
   for(int index=start_pos+count-1;index>=start_pos;index--)
   {
      if(output[index+1]==EMPTY_VALUE) output[index+1]=output[index+2];
      bool next_index=false;
      int i=index+right;
      for(int j=i+1;j<i+1+left;j++)
      {
         if(j>Bars(_Symbol, PERIOD_CURRENT)-1) break;
...
}