Finding Peaks and Troughs

Discussion in 'Strategy Development' started by Corey, Aug 6, 2008.

  1. Corey


    I've been struggling for quite a while to find an appropriate formula for finding peaks and troughs.

    I find 'swing highs' and 'swing lows' based on some arbitrary look-back distance to be a bit too crude. It misses the heart of the subject, and adds an extra layer of constraint I don't like.

    I did find a paper online, titled "A Peak-Trough Detection Algorithm Based on Momentum," which was interesting, but ultimately not what I wanted.

    Most of the peak-trough detection algorithms I found dealt with continuous functions -- which made working with stock prices impossible.

    Empirically, I know the algorithm had to have some volatility component -- and I wanted as little look back as possible. I found an algorithm online (can't find the source) that I modified that worked fairly well.

    Basically, using a 'delta' function, if your closing price was higher than your last min/max by delta, you locked that min/max as a trough/peak. I chose my delta value to be a function of the ATR. I also allowed peaks and troughs that were 'close' to be merged into a singular peak/trough entity by only keeping the more prominent peak/trough.

    But even this missed the subtleties. It helped recognize that in times of less volatility, the peaks and troughs were not as pronounced as those in more volatile periods -- but it still missed quite a few.

    I could sum the issue up nicely: without some sort of continuous representation, it was difficult to find peaks and troughs.

    Then it hit me like a ton of bricks: a moving average is a continuous representation of price. Durh. Furthermore, price tends to oscillate around fast moving averages.

    I quickly whipped up an algorithm. Basically, when price crosses above the 10DMA, I start recording highs. When it crosses back below, I take the max high and use that as my peak. Same for troughs -- when price crosses below, I start recording and stop when price crosses above again.

    The nice thing about this algorithm is that it solves the volatility issue on its own! The 10DMA is adaptive to the price volatility environment!

    Anyway, just thought I would share the simple, yet effective method for identifying peaks and troughs for any of you out there struggling to find a good algorithm.
  2. RhinoGG

    RhinoGG Guest

    giddy up! jump in the saddle, hold on to the bridle!
  3. Corey

    Works for me, why did you chose 10 periods?

  4. I don't know why, but I have always believed that moving averages were dynamic representations of support and resistance levels.

    My belief might be wrong though.
  5. i use a 3 or 5 period T3 ema for that purpose, every time the ema slope changes (from + to - and vice versa), i then identify the corresponding high and low, very handy to test stuff like fib ext targets and the like. 3 periods will yield a lot more highs and lows, and 5 periods smooths it a bit, giving you only the ''majors'' highs and lows.

    if you cannot use a T3, use a hull, its almost the same in the end although the formula is totally different.
  6. my initial testing indicates that is true for some ma's. surprisingly, the more popular ones (20 and 50 sma) arent as efficient as more esoteric emas, like the 34 ema (wich is the one i found to work best)
    340 WMA is also pretty efficient.

    i believe that the more popular ones like the 20,50,100 sma's will be used as newbie traps by the smart money. This could explain why they are the ma's the most talked about.
  7. This holds true for which markets?

    In my particular case, I have been observing them on weekly charts of broad-based indices. Also, the higher the time frame, the more efficient they become in determining s/r levels.
  8. Corey


    I hate back testing and optimizing, but I felt that looking at different charts, it found the highs and lows I wanted it to find. I found that a 5 day simple moving average was too fast, and that I would get 1 bar blips above the average that weren't long-term peaks, but short term randomness. 10 worked great across all the examples.

    As silvermotion said, you could also use a shorter period and use inflection points to mark the peaks and troughs -- I just felt the results I was getting with this method were once again a bit too noisy for my purposes.

    I will check out the t3 moving average though. Thanks for the heads up silvermotion.
  9. finding peak and through is for placing "STO" or "BTO" very near to it, and its mathematically simple.

    you coud have data very closely spaced that you can treat them as continous.
  10. Volume and Bid/Ask Data bias helps.

    I would consider creating a data set that is similar to Market Profile.
    #10     Aug 12, 2008