Help in simplifying my Peak High Low Code

 
//+------------------------------------------------------------------+
//| 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

I need help in making the function for PikBar more efficient

 
  1. Bwalya Tambule: I need help in making the function for PikBar more efficient

    Don't worry about performance, until you have a problem. Then, and only then, use the profiler, find the problem, fix the problem, and remeasure.

    I assume you mean more generalized.

    int   PikBar(ENUM_TIMEFRAMES timeframe,int mode, int shoulder, int startBar,int peakNo) 
       {
       for(int iBar=1; iBar <= peakNo; ++iBar)
          startBar = FindPeak(timeframe,mode,shoulder,startBar);
       return startBar;
  2. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

    On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
              Error 4806 while using CopyBuffer() - Expert Advisors and Automated Trading - MQL5 programming forum #10 (2020)
              Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum (2019)
              Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum #2 (2018)
              SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum (2019)

 
William Roeder #:
  1. Don't worry about performance, until you have a problem. Then, and only then, use the profiler, find the problem, fix the problem, and remeasure.

    I assume you mean more generalized.

  2. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

    On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
              Error 4806 while using CopyBuffer() - Expert Advisors and Automated Trading - MQL5 programming forum #10 (2020)
              Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum (2019)
              Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum #2 (2018)
              SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum (2019)

Wow, thanks so much.

Reason: