True Market structure

 
Im working on this indicator, it draws swing points..I just need a more efficient way to perfom this task..my code appears messy to me...some times it misses a point. current faulty
//+------------------------------------------------------------------+
//|                                                SwingInternal.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots   4
//--- plot High
#property indicator_label1  "High"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrCrimson
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Low
#property indicator_label2  "Low"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrMediumSeaGreen
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

//--- plot Internal
#property indicator_label3  "Internal"
#property indicator_type3   DRAW_ZIGZAG
#property indicator_color3  clrSteelBlue
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1

#property indicator_label4  "Liquidity"
#property indicator_type4   DRAW_ARROW
#property indicator_color4  clrMediumPurple
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1

//--- input parameters
input int InpDepth    = 12; // Depth
input int InpDeviation = 5; // Deviation
input int InpBackstep = 3;  // Back Step
input bool drawZag = true; //Show ZigZag

//--- indicator buffers
double         HighBuffer[];
double         LowBuffer[];
double         InternalBuffer1[];
double         InternalBuffer2[];
double         LiqHi[];

int handle;
double hHigh[], lLow[];
int code = 158;
int li1Code = 83;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, HighBuffer, INDICATOR_DATA);
   SetIndexBuffer(1, LowBuffer, INDICATOR_DATA);
   SetIndexBuffer(2, InternalBuffer1, INDICATOR_DATA);
   SetIndexBuffer(3, InternalBuffer2, INDICATOR_DATA);
   SetIndexBuffer(4, LiqHi, INDICATOR_DATA);

   PlotIndexSetInteger(0, PLOT_ARROW, code);
   PlotIndexSetInteger(1, PLOT_ARROW, code);
   PlotIndexSetInteger(4, PLOT_ARROW, li1Code);


   handle = iCustom(_Symbol, _Period, "Examples\\ZigZagColor", InpDepth, InpDeviation, InpBackstep);
   if(handle == INVALID_HANDLE)
     {
      Print("Error creating Bollinger Bands indicator: ", GetLastError());
      return(-1);
     }
   PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, 0.0);
   PlotIndexSetDouble(3, PLOT_EMPTY_VALUE, 0.0);

//---
   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 MAX_COUNT = rates_total - 1;

   if(CopyBuffer(handle, 0, 0, MAX_COUNT, hHigh) <= 0)
     {
      Print("Error copying upper band buffer: ", GetLastError());
      return(0);
     }

   if(CopyBuffer(handle, 1, 0, MAX_COUNT, lLow) <= 0)
     {
      Print("Error copying lower band buffer: ", GetLastError());
      return(0);
     }

   // Initialization of hi and lo to reasonable extreme values
   double hi = -1;  // Start with a very low value
   double lo = 1e+10; // Start with a very high value

   int prevLowIndex = -1;
   int currentLowIndex = prevLowIndex;

   int prevHIndex = -1;
   int currentHIndex = prevHIndex;

   int hiStartIndex = -1;
   int loStartIndex = -1;

   for(int i = 0; i < MAX_COUNT; i++)
     {
      // Update high and low based on the conditions
      if(hHigh[i] > hi && close[i] > hi && hHigh[i] != 0)
        {
         hi = hHigh[i];
         currentHIndex = i;
         hiStartIndex = i;
         HighBuffer[i] = hi;
        }

      if(lLow[i] < lo && close[i] < lo && lLow[i] != 0)
        {
         lo = lLow[i];
         currentLowIndex = i;
         loStartIndex = i;
         LowBuffer[i] = lo;
        }

      // Update hi when a new low index is found
      if(currentLowIndex != prevLowIndex)
        {
         if(prevLowIndex != -1)
           {
            double tempHi = -1;
            for(int j = prevLowIndex + 1; j <= currentLowIndex; j++)
              {
               if(hHigh[j] > tempHi && close[j] > tempHi && hHigh[j] != 0)
                 {
                  tempHi = hHigh[j];
                  hiStartIndex = j;
                 }
              }
            hi = tempHi;
           }
         prevLowIndex = currentLowIndex;
        }

      // Update lo when a new high index is found
      if(currentHIndex != prevHIndex)
        {
         if(prevHIndex != -1)
           {
            double tempLo = 1e+10;
            for(int j = prevHIndex + 1; j <= currentHIndex; j++)
              {
               if(lLow[j] < tempLo && close[j] < tempLo && lLow[j] != 0)
                 {
                  tempLo = lLow[j];
                  loStartIndex = j;
                 }
              }
            lo = tempLo;
           }
         prevHIndex = currentHIndex;
        }

      // Set buffers from the highest index to the current index
      if(hiStartIndex != -1 && close[i] <= hi)
        {
         for(int j = hiStartIndex; j <= i; j++)
           {
            HighBuffer[j] = hi;
           }
        }
      
      if(loStartIndex != -1 && close[i] >= lo)
        {
         for(int j = loStartIndex; j <= i; j++)
           {
            LowBuffer[j] = lo;
           }
        }

      // Draw ZigZag points if required
      if(drawZag)
        {
         InternalBuffer1[i] = hHigh[i];
         InternalBuffer2[i] = lLow[i];
        }
      else
        {
         InternalBuffer1[i] = EMPTY_VALUE;
         InternalBuffer2[i] = EMPTY_VALUE;
        }
     }

   return(rates_total);
  }
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(handle);

  }
//+------------------------------------------------------------------+