Indicator does not work in all time periods

 
Hello!

I'm still a beginner so I don't really understand why this script doesn't work in all time periods?
It only does what it needs to in 1H and 4H.
(It is a simple program for marking peaks.)

Thanks in advance.


int OnInit()
{
   //--- set global variables
   min_hgt = int(InpMinHeight < 1 ? 1 : InpMinHeight);
   min_height = min_hgt * Point();
   min_bars = int(InpMinBars < 1 ? 1 : InpMinBars);
   max_dist = int(InpMaxDist < 1 ? 1 : InpMaxDist);
   //--- indicator buffers mapping
   SetIndexBuffer(0, BufferTopDBL, INDICATOR_DATA);
   SetIndexBuffer(1, BufferTop, INDICATOR_DATA);
   SetIndexBuffer(2, BufferBottomDBL, INDICATOR_DATA);
   SetIndexBuffer(3, BufferBottom, INDICATOR_DATA);
   //--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(0, PLOT_ARROW, 88);
   PlotIndexSetInteger(1, PLOT_ARROW, 158);
   PlotIndexSetInteger(2, PLOT_ARROW, 88);
   PlotIndexSetInteger(3, PLOT_ARROW, 158);
   //--- setting indicator parameters
   IndicatorSetString(INDICATOR_SHORTNAME, "Double top (Min height: " + (string)min_hgt + ", max distance: " + (string)max_dist + ", min bars: " + (string)min_bars + ")");
   IndicatorSetInteger(INDICATOR_DIGITS, Digits());
   //--- setting buffer arrays as timeseries
   ArraySetAsSeries(BufferTop, true);
   ArraySetAsSeries(BufferTopDBL, true);
   ArraySetAsSeries(BufferBottom, true);
   ArraySetAsSeries(BufferBottomDBL, 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[])
{
   //--- Puffertömbök beállítása idősorként
   ArraySetAsSeries(high, true);
   ArraySetAsSeries(low, true);
   ArraySetAsSeries(close, true);
   //--- A kiszámítandó gyertyák számának ellenőrzése, számítása
   if (rates_total < fmax(min_bars, 9))
      return 0;

   int limit = rates_total - prev_calculated;
   if (limit > 1)
   {
      limit = rates_total - min_bars - 1;
      ArrayInitialize(BufferTop, EMPTY_VALUE);
      ArrayInitialize(BufferTopDBL, EMPTY_VALUE);
      ArrayInitialize(BufferBottom, EMPTY_VALUE);
      ArrayInitialize(BufferBottomDBL, EMPTY_VALUE);
   }
   //--- Mutató számítás
   for (int i = limit; i >= 0 && !IsStopped(); i--)
   {
      BufferBottom[i] = BufferBottomDBL[i] = BufferTop[i] = BufferTopDBL[i] = EMPTY_VALUE;
      BufferTop[i + min_bars] = (IsTop(rates_total, i + min_bars, high, low) ? high[i + min_bars] : EMPTY_VALUE);
      BufferBottom[i + min_bars] = (IsBottom(rates_total, i + min_bars, high, low) ? low[i + min_bars] : EMPTY_VALUE);

      if (BufferTop[i + min_bars] == high[i + min_bars])
      {
         if (FindPrevTop(rates_total, i + min_bars, high) && AreAdjacentBarsSmaller(rates_total, i + min_bars, high))
            BufferTopDBL[i + min_bars] = high[i + min_bars];
         else
            BufferTopDBL[i + min_bars] = EMPTY_VALUE;
      }

      if (BufferBottom[i + min_bars] == low[i + min_bars])
      {
         if (FindPrevBottom(rates_total, i + min_bars, low) && AreAdjacentBarsSmaller(rates_total, i + min_bars, low))
            BufferBottomDBL[i + min_bars] = low[i + min_bars];
         else
            BufferBottomDBL[i + min_bars] = EMPTY_VALUE;
      }
   }

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

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool IsTop(const int rates_total, const int index, const double &high[], const double &low[])
{
   bool fl = true;
   for (int i = 1; i <= min_bars; i++)
      if (high[index - i] >= high[index])
         fl = false;

   if (fl && high[index] > high[index - 1] && high[index] > high[index + 1] && high[index] > high[index - 2] && high[index] > high[index + 2] && high[index] > high[index - 3] && high[index] > high[index + 3] && high[index] > high[index - 4] && high[index] > high[index + 4])
   {
      int i = index + 1;
      while (i < rates_total)
      {
         if (high[i] >= high[index])
            return false;
         if (high[index] - low[i] >= min_height)
            return true;
         i++;
      }
   }
   return false;
}


 
      BufferTop[i + min_bars] = 

Your lookback is min_bars.
          How to do your lookbacks correctly #9#14 & #19 (2016)

 
Thanks,
I will try it.