Zigzag patterns

 

Im creating an indicator that has to detect chart structure . e.g Rising wedge

I do have code I took somewhere online that only places s/r at the peaks.

I want to convert the code to take in trendline, so can you please guide me;

//+------------------------------------------------------------------+
//|                                                        HR2.2.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_plots   1
#property indicator_buffers 2
//+------------------------------------------------------------------+
//| ZigZag Inputs                                                    |
//+------------------------------------------------------------------+
input int inpDepth = 12; // Depth
input int inpDeviation = 5; // Deviation
input int inpBackstep = 3; //Backstep

//+------------------------------------------------------------------+
//| Peak Analysis input                                              |
//+------------------------------------------------------------------+
input int InpGapPoints= 7; //Minimum Gap peaks in points
input int InpSensitivity = 2; //Peak Sensitivity
input int InpLookBack = 50; //LookBack

//+------------------------------------------------------------------+
//| Drawing Inputs                                                   |
//+------------------------------------------------------------------+
input string InpPrefix = "SRLEvel_"; // OBject Name Prefix
input color InpLineColour= Red; //Line Colour
input int InpLineWeight= 1;// Line Weight

//For Levels
double SRLevel[];

//ZigZag GET fron the indicator
double Buffer[];
int Handle;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Zigzag
   Handle = iCustom(_Symbol,_Period,"Examples\\ZigZag",inpDepth,inpDeviation,inpBackstep);

   if(Handle == INVALID_HANDLE)
     {
      Print("Could not create a handle to the zizag indicator");
      return(INIT_FAILED);
     }
   ArraySetAsSeries(Buffer, true);


//Clean up Any SRLEVELS on the chart if any exist
   ObjectsDeleteAll(0, InpPrefix, 0, OBJ_HLINE);
   ChartRedraw(0);
   ArrayResize(SRLevel,InpLookBack);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   setChartColorBackground();
   IndicatorRelease(Handle);
//Clean up Any SRLEVELS on the chart if any exist

   ObjectsDeleteAll(0, InpPrefix, 0, OBJ_HLINE);
   ChartRedraw(0);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---One Time convert points to a price gap
   static double levelGap = InpGapPoints;
   DrawLevels();
   if(rates_total == prev_calculated)
      return(rates_total);

//Get the most recent <lookback> peaks
   double zz = 0;
   double zzPeaks[];
   int zzCount = 0;

   ArrayResize(zzPeaks, InpLookBack);
   ArrayInitialize(zzPeaks,0.0);

   int count = CopyBuffer(Handle, 0, 0,rates_total, Buffer);

   if(count < 0)
     {
      int err = GetLastError();
      return(0);
     }

   for(int i = 1; i < rates_total && zzCount < InpLookBack; i++)

     {


      if(zz != 0&& zz != EMPTY_VALUE)
        {
         zzPeaks[zzCount] = zz;
         zz = Buffer[i];

         Print(zzPeaks[zzCount]);

         zzCount++;
        }
     }

   ArraySort(zzPeaks);

//Search for groupings and set levels
   int srCounter= 0;
   double price = 0;
   int priceCount = 0;
   ArrayInitialize(SRLevel, 0.0);

   for(int i = InpLookBack-1; i>=0; i--)
     {
      price+=zzPeaks[i];
      priceCount++;

      if(i==0 || (zzPeaks[i] - zzPeaks[i-1]) >levelGap)
        {
         if(priceCount >= InpSensitivity)
           {
            price =price/priceCount;
            SRLevel[srCounter] = price;
            srCounter++;
           }

         price = 0;
         priceCount = 0;
        }
     }


//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
void DrawLevels()
  {

   for(int i = 0; i<InpLookBack; i++)
     {
      string name = InpPrefix + IntegerToString(i);
      if(SRLevel[i] == 0)
        {
         ObjectDelete(0, name);
         continue;
        }
      if(ObjectFind(0, name)<0)
        {
         // ObjectCreate(0,name,OBJ_TREND,0,time1,price1,time2,price2);
         ObjectCreate(0,name,OBJ_HLINE, 0, 0, SRLevel[i]);
         ObjectSetInteger(0, name, OBJPROP_COLOR,InpLineColour);
         ObjectSetInteger(0, name, OBJPROP_WIDTH,InpLineWeight);

        }
      else
        {
         ObjectSetDouble(0,name,OBJPROP_PRICE,SRLevel[i]);
        }
     }
   ChartRedraw(0);


  }
//+------------------------------------------------------------------+
void              setChartColorBackground(color chart = White)
  {
   string Server = AccountInfoString(ACCOUNT_SERVER);
   string Name = AccountInfoString(ACCOUNT_NAME);
   string currency=AccountInfoString(ACCOUNT_CURRENCY);
   string charts = _Symbol;

   datetime compilation_time=__DATETIME__-__DATE__;

//+------------------------------------------------------------------+
//|      Chart Colours                                               |
//+------------------------------------------------------------------+
   ChartSetInteger(0, CHART_COLOR_BACKGROUND, chart);
   ChartSetInteger(0, CHART_COLOR_CANDLE_BULL, C'38,166,154');
   ChartSetInteger(0, CHART_COLOR_CANDLE_BEAR, C'239,83,80');
   ChartSetInteger(0, CHART_COLOR_CHART_UP, C'38,166,154');
   ChartSetInteger(0, CHART_COLOR_CHART_DOWN,  C'239,83,80');
   ChartSetInteger(0, CHART_COLOR_CHART_LINE, clrNavy);
   ChartSetInteger(0, CHART_COLOR_FOREGROUND, clrBlack);
   ChartSetInteger(0, CHART_COLOR_BID,  C'38,166,154');
   ChartSetInteger(0, CHART_COLOR_ASK,  C'239,83,80');
   ChartSetInteger(0, CHART_COLOR_LAST, clrMoccasin);
   ChartSetInteger(0, CHART_COLOR_GRID, clrNONE);
   ChartSetInteger(0, CHART_COLOR_STOP_LEVEL, clrLightSkyBlue);
   ChartSetInteger(0, CHART_SHOW_PERIOD_SEP, 0);

   ChartSetInteger(0,CHART_SHOW_VOLUMES, CHART_VOLUME_HIDE);
   ChartSetInteger(0,CHART_SHOW_TICKER,0);
   ChartSetInteger(0,CHART_SHOW_ONE_CLICK,0);
   ChartSetInteger(0,CHART_AUTOSCROLL,true);
  }
//+------------------------------------------------------------------+