Coding trendline based on peaks/troughs?
Author: kbellare
Creation Date: 11/9/2013 7:57 PM
profile picture


Is there an indicator that touches "relevant" peaks and troughs to draw trendlines, just like traders would visually draw? If not, any suggestions on coding this? Trying to build a "trendline break" trend reversal strategy.

I tried TrendlinePeaks and TrendlineTroughs from the TASC library, but it draws them quite far from the price curve and shows sudden turns/corners - the wiki doesn't explain the logic used, so appreciate if someone can explain and advise if it's suitable for what i'm trying to do.

profile picture


it draws them quite far from the price curve and shows sudden turns/corners

It is drawing the trendline extensions from the two most recent peaks (or troughs) at the time that the second peak (trough) is identified. The sharp turn means the next peak (trough) has been identified and is now creating the projection for it.

Trendline projections are the lines that you are concerned about crossing. You could create a study that drew the trendlines touching the peaks and troughs too, but this would just create a terribly verbose chart of lines.

If you need to visualize the trendline through the peaks (troughs), just use a straightedge to "project the projection" backwards. (And keep in mind that the chart needs to be linear or log depending on the indicator's UseLogScale parameter.
profile picture


I like the Tasc TrendLinePeaks/TrendLineTroughs indicators. I'd like to get as much information as possible for them (e.g. a detailed description of the algorithm or preferably the code) Any more info beyond the description in your post above would be appreciated.
profile picture


To scrutinize the indicators, you can download latest source code from this page:

Home - TASCIndicators
profile picture


The logic is pretty simple. First, peaks (troughs) are identified using the standard Peak (Trough) indicator. It uses the equation of a line (y = mx + b) to find the y value of the line that extends from the two most recent peaks (toughs) at the current x (i.e., bar). The abrupt changes in the indicator values occur at the time a new peak (trough) is detected. That's it!

Finally, note that the equation of the line changes depending on the linear or log scale option, which should correspond to the option you've selected to view the chart.
profile picture


Thanks very much for the fast response.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).