My fractal indicator doesnt work. Plz help me.(MQL5)

 

I've coded Fractal Indicator with a adjustable input variable.
It looks working at first glance, but sometimes it doesn't display fractal sign when it should be there. ( Picture attached below)

Im new to this language and community.
I have no idea what causes this problem. If you teach me what is the problem and could be a solution, I will be very thankful to you.


#property copyright     "Copyright 2018, MetaQuotes Software Corp."
#property link          "https://mql5.com"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot UpFactal
#property indicator_label1 "Factal Up"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  4
//--- plot DownFractal
#property indicator_label2  "Fractal Down"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  4
//--- input parameters
input int       Frames = 2; // Frames
//--- indicator buffers
double         BufferUpFractal[];
double         BufferDownFractal[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
        //--- settings parameters
   if (Frames < 1)
        return (0);
        //--- indicator buffers mapping
   SetIndexBuffer(0,BufferUpFractal,INDICATOR_DATA);
   SetIndexBuffer(1,BufferDownFractal,INDICATOR_DATA);
        //--- setting a buffers parameters
   PlotIndexSetInteger(0,PLOT_ARROW,119);
   PlotIndexSetInteger(1,PLOT_ARROW,119);
        //--- strings parameters
   IndicatorSetString(INDICATOR_SHORTNAME, "Advanced fractals(" + (string)Frames + ")");
        //---
   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 limit;
        //--- Checking for minimum number of bars
   if (rates_total < Frames * 2 + 1) 
        return (0);
        //---
   
   
   if (prev_calculated < Frames*2+3)
      {
       limit = Frames;
       ArrayInitialize(BufferUpFractal,EMPTY_VALUE);
       ArrayInitialize(BufferDownFractal,EMPTY_VALUE);
      }
   else limit = rates_total - 1 - Frames*2;

        //--- Calculate indicator
   for (int i = limit; i < rates_total - 1 - Frames && !IsStopped(); i++)
   {
     
      if(i==ArrayMaximum(high,i-Frames,i+Frames))
          BufferUpFractal[i] = high[i];
      else BufferUpFractal[i] = EMPTY_VALUE;
      
      if(i==ArrayMinimum(low,i-Frames,i+Frames))
          BufferDownFractal[i] = low[i];
      else BufferDownFractal[i] = EMPTY_VALUE;
      
   }
        //--- return value of prev_calculated for next call
   return(rates_total);
}
 
      if(i==ArrayMaximum(high,i-Frames,i+Frames))

It's a fractal when there are n on the left and n on the right lower than the middle. You check if the highest [middle … n on the right] equals the middle.

Perhaps you should read the manual. https://www.mql5.com/en/docs/array/arraymaximum
   How To Ask Questions The Smart Way. 2004
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

Reason: