finding latest swing high and swing low

 

Hi everyone, I want to find the latest swing high and swing low. I used this code to find it:

double swingHigh  = iHigh(Symbol(),PERIOD_CURRENT,iHighest(Symbol(),PERIOD_CURRENT,MODE_HIGH,WHOLE_ARRAY,5));
double swingLow   = iLow(Symbol(),PERIOD_CURRENT,iLowest(Symbol(),PERIOD_CURRENT,MODE_LOW,WHOLE_ARRAY,5));

Using this I can find the swing high and swing low but it will search for the highest and lowest price on the whole array since I used the the WHOLE_ARRAY in the count parameter. What I want is to find only the latest swing high and changing the count parameter to number doesn't seems to work like what I want. Is there's a way that the EA would stop when It finds the latest swing high and swing low?

 
Go up hill until you find it.
#define INDEX uint
   #define  INV_INDEX      UINT_MAX          ///< Unspecified: Past last item.
#define COUNT uint
/** Finds the INDEX of the maximum local extream in an _array_, i.e.\. moving
 * up hill until you reach the top.                                        */
template<typename T>
INDEX      max_extreama(const T&   inp[],      ///<[in]Array to search
                        COUNT      size,       ///<[in]Size of a local
                        INDEX   iEnd=INV_INDEX,///<[in]One past last.
                        INDEX      iBeg=0)     ///<[in]Starting index.
   if(iEnd == INV_INDEX)   iEnd  = ArraySize(inp);
   if(iEnd == iBeg)        return iEnd;
   INDEX    iExtreme = iBeg++;   T     extreme  = inp[iExtreme];
   INDEX    iLimit   = MathMin(iEnd, iExtreme + size);
   for(; iBeg < iLimit; ++iBeg){
      T     value = inp[iBeg];
      if(extreme <= value){
         iExtreme = iBeg;  extreme = value;
         iLimit   = MathMin(iEnd, iExtreme + size);
   }  }
   return iExtreme;
}
 
//+------------------------------------------------------------------+
//| Get H Line                                            |
//+------------------------------------------------------------------+
int   PikBar(ENUM_TIMEFRAMES timeframe,int mode, int shoulder, int startBar,int peakNo) 
   {
   int bar=-1;
   int bar1 = FindPeak(timeframe,mode,shoulder,startBar);
   int bar2 = FindPeak(timeframe,mode,shoulder,bar1+1);
   int bar3 = FindPeak(timeframe,mode,shoulder,bar2+1);
   int bar4 = FindPeak(timeframe,mode,shoulder,bar3+1);
   int bar5 = FindPeak(timeframe,mode,shoulder,bar4+1);
   int bar6 = FindPeak(timeframe,mode,shoulder,bar5+1);
   int bar7 = FindPeak(timeframe,mode,shoulder,bar6+1);
   int bar8 = FindPeak(timeframe,mode,shoulder,bar7+1);
   int bar9 = FindPeak(timeframe,mode,shoulder,bar8+1);
   int bar10 = FindPeak(timeframe,mode,shoulder,bar9+1);
   int bar11 = FindPeak(timeframe,mode,shoulder,bar10+1);
   int bar12 = FindPeak(timeframe,mode,shoulder,bar11+1);
   int bar13 = FindPeak(timeframe,mode,shoulder,bar12+1);
   int bar14 = FindPeak(timeframe,mode,shoulder,bar13+1);
   int bar15 = FindPeak(timeframe,mode,shoulder,bar14+1);
  if(peakNo == 1) bar = bar1 ;
   if(peakNo == 2) bar = bar2 ;
   if(peakNo == 3)  bar = bar3 ;
   if(peakNo == 4)  bar = bar4 ;
   if(peakNo == 5)  bar = bar5 ;
   if(peakNo == 6) bar = bar6 ;
   if(peakNo == 7)  bar = bar7 ;
   if(peakNo == 8)  bar = bar8 ;
   if(peakNo == 9)  bar = bar9 ;
   if(peakNo == 10)  bar = bar10 ;
   if(peakNo == 11)  bar = bar11 ;
   if(peakNo == 12)  bar = bar12 ;
   if(peakNo == 13)  bar = bar13 ;
   if(peakNo == 14)  bar = bar14 ;
   if(peakNo == 15)  bar = bar15 ;
   return bar ;
   }
//+------------------------------------------------------------------+
//|                                              |
//+------------------------------------------------------------------+
 int FindPeak(ENUM_TIMEFRAMES timeframe,int mode, int shoulder, int startBar)
 {//a1
 if(mode!= MODE_HIGH && mode!= MODE_LOW) return(-1);
 int currentBar = startBar;
 int foundBar = FindNextPeak(timeframe,mode,shoulder*2+1, currentBar - shoulder);
 while (foundBar!= currentBar){//while1
 currentBar = FindNextPeak(timeframe,mode, shoulder, currentBar+1);
 foundBar   = FindNextPeak(timeframe,mode, shoulder*2+1,currentBar-shoulder);
 }//while1
 return(currentBar);
}//a1 
//+------------------------------------------------------------------+
 int FindNextPeak(ENUM_TIMEFRAMES timeframe, int mode, int shoulder, int startBar)
 {//a2
 if(startBar < 0) {//a3
 shoulder += startBar;
 startBar = 0 ;
 }//a3
 return( (mode == MODE_HIGH) ?
        iHighest(Symbol() , timeframe, (ENUM_SERIESMODE)mode, shoulder, startBar) : 
        iLowest(Symbol() , timeframe, (ENUM_SERIESMODE)MODE_LOW, shoulder, startBar)  
            );
 }//a2

That is the code I use to find Peak High and Low. Please help on the Peak Bar function. I am using the long way to find the different peak bars. What is the short way as the long takes up a lot of code. In the code above the first peak high is bar1 the next peak high after that is bar2 . My code is too long winded. Hope the long way is self evident and some kind soul can simplify. thanks 

 
Please help with making the above an efficient code
Reason: