Indicator Bug - Last X Bars' High Break

 

Hello,


I am trying to build an indicator that shows a line next to the price that indicates last X Bars High.

The only caviat is, that i want to show the maximum of the current and the last. In another words, only show the current Last X Bars Max if is higher compared to the Last X Bars Max. Else just print the last one.


For example in the screenshot ( https://imgur.com/a/rRWPUwp ), the line should remain equal because the high is never broken.

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot highs
#property indicator_label1  "highs"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrMediumOrchid
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

//--- indicator buffers
double         highBuffer[];

//----Indicator Variables
int            minimumBars =  50;


int OnInit(){

   SetIndexBuffer(0,highBuffer,INDICATOR_DATA);
   
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,minimumBars);

   return(INIT_SUCCEEDED);
}
  
  
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[]){

   if(rates_total<minimumBars-1+begin)
      return(0);

   int first;
   double lastMax = 0.0;
   
   if(prev_calculated==0) 
      first=minimumBars-1+begin; 
   else first=prev_calculated-1;
   
   
   for(int bar=first;bar<rates_total;bar++){
   
      double currentMax=0.0;
       // Finding High of last X bars
       for(int iii=0;iii<minimumBars;iii++){
         double tmpPrice = price[bar-iii];
         
         if(tmpPrice > currentMax){
            currentMax = tmpPrice;
         }
       }      
       
      //double maximumMax = MathMax(lastMax,currentMax);
      
      //if(maximumMax==currentMax){
      //   lastMax = currentMax;
      //}
      
      highBuffer[bar]=currentMax ; //highBuffer[bar]=maximumMax;
      
   }

   return(rates_total);
}


If i add the commented lines, the indicator just prints a straight line(Screenshot - https://imgur.com/a/J73p33N). For my understanding the logic is correct.  Can someone help me?