Extract previous three HH, HL, LL and LH values from ZigZag Indicator Code

 

Hi Friend

Can someone help me how to extract previous three HH, HL, LL and LH from the code of ZigZag indicator? I have removed the indicator functionality portion from the code, so it can be suitably modified to convert into a function.

I would like to convert it into a function.

Thanks in advance for your support and guidance.

//+------------------------------------------------------------------------------------------------------------------------------+
//| Script to create Function get_ZigZagValues.mq4
//+------------------------------------------------------------------------------------------------------------------------------+
#property strict
input int InpDepth     = 9;      // Depth
input int InpDeviation = 5;      // Deviation
input int InpBackstep  = 3;      // Backstep
// Define buffers
double ExtZigzagBuffer[];
double ExtHighBuffer[];
double ExtLowBuffer[];
// Define Globals
int    ExtLevel = 3;             // recounting's depth of extremums
//+-----------------------------------------------------------------------------------------------------------------------------+
//| Custom initialization function
//+-----------------------------------------------------------------------------------------------------------------------------+
int OnInit()
{
   ArrayInitialize(ExtZigzagBuffer,0);
   ArrayInitialize(ExtHighBuffer,0);
   ArrayInitialize(ExtLowBuffer,0);
//--- first counting position
   return(Bars - InpDepth);
}
//+-----------------------------------------------------------------------------------------------------------------------------+
//| Extract HH, HL, LL and LH prices from the ZigZag indicator code
//+-----------------------------------------------------------------------------------------------------------------------------+
void OnStart()
{
  int    i,limit,counterZ,whatlookfor=0;
  int    backStep,pos,lasthighpos=0,lastlowpos=0;
  double extremum;
  double current_Low  = 0;
  double current_High = 0;
  double last_High    = 0;
  double last_Low     = 0;
  // Check for history and inputs
  if(Bars > InpDepth || InpBackstep <= InpDepth)
    // First calculations
    if(IndicatorCounted() == 0)
      limit = (Bars - InpDepth);
    else
    {
      i = counterZ = 0;
      // Find first extremum in the depth ExtLevel or 100 last bars
      while(counterZ < ExtLevel && i < 100)
      {
        if(ExtZigzagBuffer[i] != 0)
          counterZ++;
          i++;
      }
      // No extremum found, recounting all from begin
      if(counterZ == 0)
        limit = (Bars - InpDepth);
      // Set start position to found extremum position
      else
      {
        limit = i-1;
        // What kind of extremum?
        if(ExtLowBuffer[i] != 0)
        {
          current_Low = ExtLowBuffer[i];    // Low extremum
          whatlookfor = 1;                  // Will look for the next high extremum
        }
        else
        {
          current_High = ExtHighBuffer[i];  // High extremum
          whatlookfor  = -1;                // Will look for the next low extremum
        }
      // Clear the rest data
      for(i = limit-1; i >= 0; i--)  
      {
        ExtZigzagBuffer[i] = 0;  
        ExtLowBuffer[i]    = 0;
        ExtHighBuffer[i]   = 0;
      }
    }
  }
  // Main loop
  for(i = limit; i >= 0; i--)
  {
    // Find lowest Close in depth of bars
    extremum = Close[iLowest(NULL,0,MODE_CLOSE,InpDepth,i)];
    // This lowest has been found previously
    if(extremum == last_Low)
      extremum = 0;
    else
    {
      last_Low = extremum;                                      // New last low
      if(Close[i] - extremum > InpDeviation * Point)            // Discard extremum if current Close is too high
        extremum = 0;
      else
      {
        for(backStep = 1; backStep <= InpBackstep; backStep++)  // Clear previous extremums in backstep bars
        {
          pos = i + backStep;
          if(ExtLowBuffer[pos] != 0 && ExtLowBuffer[pos] > extremum)
            ExtLowBuffer[pos] = 0;
        }
      }
    }
    // Found extremum is current Close
    if(Close[i] == extremum)
      ExtLowBuffer[i] = extremum;
    else
      ExtLowBuffer[i] = 0;
    // Find highest Close in depth of bars
    extremum = Close[iHighest(NULL,0,MODE_CLOSE,InpDepth,i)];
    // This highest has been found previously
    if(extremum == last_High)
      extremum = 0;
    else
    {
      last_High = extremum;                                   // New last high
        if(extremum - Close[i] > InpDeviation * Point)        // Discard extremum if current Close is too low
          extremum = 0;
        else
        {
          for(backStep = 1; backStep <= InpBackstep; backStep++) // Clear previous extremums in backstep bars
          {
            pos = i + backStep;
            if(ExtHighBuffer[pos] != 0 && ExtHighBuffer[pos] < extremum)
              ExtHighBuffer[pos] = 0;
          }
        }
    }
    // Found extremum is current Close
    if(Close[i] == extremum)
      ExtHighBuffer[i] = extremum;
    else
      ExtHighBuffer[i] = 0;
  }
  // Final cutting
  if(whatlookfor == 0)
  {
    last_Low  = 0;
    last_High = 0;  
  }
  else
  {
    last_Low  = current_Low;
    last_High = current_High;
  }
  for(i = limit; i >= 0; i--)
  {
    switch(whatlookfor)
    {
      case 0: // look for peak or lawn
        if(last_Low == 0 && last_High == 0)
        {
          if(ExtHighBuffer[i] != 0)
          {
            last_High          = Close[i];
            lasthighpos        = i;
            whatlookfor        = -1;
            ExtZigzagBuffer[i] = last_High;
          }
          if(ExtLowBuffer[i] != 0)
          {
            last_Low           = Close[i];
            lastlowpos         = i;
            whatlookfor        = 1;
            ExtZigzagBuffer[i] = last_Low;
          }
        }
        break;  
      case 1: // look for peak
        if(ExtLowBuffer[i] != 0 && ExtLowBuffer[i] < last_Low && ExtHighBuffer[i] == 0)
        {
          ExtZigzagBuffer[lastlowpos] = 0;
          lastlowpos                  = i;
          last_Low                    = ExtLowBuffer[i];
          ExtZigzagBuffer[i]          = last_Low;
        }
        if(ExtHighBuffer[i] != 0 && ExtLowBuffer[i] == 0)
        {
          last_High          = ExtHighBuffer[i];
          lasthighpos        = i;
          ExtZigzagBuffer[i] = last_High;
          whatlookfor        = -1;
        }  
        break;              
      case -1: // look for lawn
        if(ExtHighBuffer[i] != 0 && ExtHighBuffer[i] > last_High && ExtLowBuffer[i] == 0)
        {
          ExtZigzagBuffer[lasthighpos] = 0;
          lasthighpos                  = i;
          last_High                    = ExtHighBuffer[i];
          ExtZigzagBuffer[i]           = last_High;
        }
        if(ExtLowBuffer[i]!=0 && ExtHighBuffer[i]==0)
        {
          last_Low           = ExtLowBuffer[i];
          lastlowpos         = i;
          ExtZigzagBuffer[i] = last_Low;
          whatlookfor        = 1;
        }  
        break;              
      }
     }
// Completed processing
}
//+------------------------------------------------------------------------------------------------------------------------------+