# any good functions for identifying local minima/maxima?

117

pretty new to programming.  i imagine this is a pretty common thing, so just wondering if anyone can save me the work. thx.
Moderator
10614

Give an example of what you mean to do.

MathMax and MathMin find the max or min of 2 numbers

117

i'm looking to identify the highs and lows as the price bounces over and under the mean average.  to the eye, it's easy to see where these highs and lows are, but it might be a bit tough to program such a thing.  retracements (which must be ignored) and double tops (of which i'd simply want the higher of the two) are a couple of examples of problems with identifying these minima and maxima easily.  after that, i'd like to use this function to identify funnels.
Moderator
10614

I know that some people use the zig zag indicator to help to identify highs and lows.

price bounces over and under the mean average

Is a mean average nastier than a normal average? Or is it just an average average?   :)

16103

```/** Finds the index of the maximum local extream
* i.e.\. moving up hill until you reach the top. */
int   max_extreama(int  window,           ///<[in] Size of a local.
int  iEnd,             ///<[in] One past last.
int  iBeg=0,           ///<[in] Starting index.
int  dir=MODE_ASCEND)  /**<[in] Direction of the search. */{
if(iEnd == iBeg)  return iEnd;
if(MODE_ASCEND == dir){
int      iExtreme = iBeg++;  double   extreme  = High[iExtreme], value;
int      iLimit   = MathMin(iEnd, iExtreme + window);
for(; iBeg < iLimit; ++iBeg){
value = High[iBeg];
if(extreme < value){
iExtreme = iBeg;  extreme = value;
iLimit   = MathMin(iEnd, iExtreme + window);
}  }
return iExtreme;
}
int      iExtreme = --iEnd;   double extreme = High[iExtreme], value;
--window;   // iEnd decremented first so window is one smaller.
int      iLast    = MathMax(iBeg, iExtreme - window);
while(iEnd > iLast){
value = High[--iEnd];
if(extreme < value){
iExtreme = iEnd;  extreme = value;
iLast    = MathMax(iBeg, iExtreme - window);
}  }
return iExtreme;
}```
117

thanks for the help guys