Automated Trendlines for Indicators

I recently posted an article titled "How to Draw Trendlines & Avoid Severe Losses."  In that article, I provide a set of objective, unambiguous rules for drawing trendlines. In response to a request from a reader, I followed up with an article titled "How to Generate Automated Trendlines," which explained how to use the rules to draw trendlines on price charts automatically.

The following article takes this concept one step further and describes how to use the same process to generate automated trendlines and trendline break signals on the chart of any indicator. The AmiBroker code segments used to create automated support and resistance lines for indicators are provided at the end of this article.

Trendline Construction and Limitations

I do not plan to repeat the trendline rules here; if you would like a more comprehensive explanation of the rules used for trendline construction, including detailed examples, please review the two previous trendline articles noted above.

However, a disclaimer is required before we continue.  There are still two main issues outstanding with the code.  First, using the date of the lowest or highest point as the origin of the trendline is logical, but that date does not necessarily signify the beginning of the trend.  As a result, in the following examples I entered specific look-back periods to force the algorithm to use my choice of trendline origination dates.

The second drawback is that the current code only generates trendlines (and trendline break signals) looking backward from today.  It does not generate all trendlines and signals that would have existed historically, which currently precludes backtesting, but that drawback does not affect the following examples.

Indicator Trendlines

Trendlines are one of the oldest and most popular technical tools; however, most traders only use trendlines on price charts.  As a result, indicator trendlines can offer us unique insights and a distinct trading advantage.

For this example, I will use the momentum or Rate of Change (ROC) indicator. The ROC indicator calculates the percentage change in the closing price of a security over a specified look-back period.  The ROC tends to peak well before price because momentum typically begins to fade or decline before prices begin to reverse direction. As a result, ROC trendline breaks should give us advance notice of potential price reversals.

However, prices often continue to climb after ROC peaks.  Consequently, ROC trendline breaks should not be used as primary directional signals.  Instead, ROC trendline breaks should be considered an early warning sign to take partial profits or to tighten stops.

Crude Oil Example

Figure 1 below is a weekly chart of the continuous crude oil futures contract through Friday September 28, 2012.  The top panel includes a candlestick chart and automated support (green) and resistance (red) lines with a look-back period of 22 weeks. The top panel also includes a simple six-week moving average line (violet).

In addition to the trendlines, the top panel also includes trendline break symbols.  There are many ways to define trendline breaks.  In these examples, a trendline break would be a weekly close or crossover below a support line (bearish signal - downward red triangle) or a weekly close or crossover above a resistance line (bullish signal - upward green triangle).

The blue line in the second panel is the 26-week ROC indicator.  The AmiBroker indicator trendline code was used to generate automated support (green) and resistance (red) lines based on 26-week look-back periods.  The green and red ROC trendline break triangles were also generated automatically.

Finally, the bottom panel contains one of my favorite tools, my custom COT indicator, which identifies bullish and bearish extremes based on the commitment of traders data for commercials, large speculators, and small traders.  High values (above the green line) and low values (below the red line) indicate extreme COT positions that typically lead to reversals.  However, this indicator is usually early and should always be confirmed by a reversal in the price trend.

I manually added vertical green and red lines to identify the dates of the ROC trendline breaks.  I also manually added circles to mark the price and ROC trendline breaks.  The trendlines and triangles were drawn automatically by the AmiBroker code.

The bullish ROC trendline break occurred on June 29, 2012, one week before price broke its automatic resistance line.  Price also crossed above its 6-week moving average the same week it broke the resistance line.  Finally, the COT indicator was extremely bullish, confirming the bullish trend reversal, which set up a high probability bullish trade in crude oil.  As you can see from the chart, the bullish move continued for another 11 weeks.

The red vertical line drawn on August 24, 2012 marked the break of the ROC support line, which signified an impending bearish price reversal. Note that the ROC trendline break preceded the price trendline break by 4-weeks, which is not unusual.   This would have been an excellent time to take some profits in a long crude oil position or to tighten stops, but a short position would have been premature.

Surprisingly, the COT indicator did not reach an extreme bearish level until last Friday's close on September 28, 2012.  The COT statistic is a reliable leading indicator, but it is not perfect.  However, when the ROC and COT indicators are used together, they are very effective at identifying future market turning points.

Figure 1: Crude Oil Auto Indicator Trendline 9-28-12

While the above chart suggests a current bearish trend for crude oil, shorting crude oil always makes me nervous, especially in light of the escalating tensions between Israel and Iran.  Vertical debit spreads with fixed maximum losses might be safer vehicles for bearish positions in crude oil, heading oil, and gasoline than short futures positions.  In addition, several weeks have passed since the initial bearish signal, which adversely affects the risk/return profile of new bearish positions.

AMIBroker Code

The following two AMIBroker code samples were used to generate the automated indicator support and resistance lines in the previous example.  Both are prototypes that require further work. The code samples are provided for educational purposes only.  Please read the Disclaimer and Terms for more information.  The code samples for the automated price trendlines were provided in the article "How to Generate Automated Trendlines."

The images below are screen shots from AmiBroker.  As a result, you will need to type the code into your AmiBroker software manually; typos will cause compile or run-time errors.  The code below compiles and runs without errors on my machine.

After entering the code, drag the indicator support and resistance formulas from the chart panel to the indicator pane that you would like to use for the trendline calculations.  Enter your look-back period and the resulting trendline should appear on the chart automatically.  You can then scroll from symbol to symbol and the resulting trendlines will be generated in the indicator pane.

Figure 2: AmiBroker Auto Indicator Support Line

Figure 3: AmiBroker Auto Indicator Resistance Line


The above example used a weekly chart to identify intermediate and long-term trend changes.  If you were interested in short-term trend changes, you could use the same indicator and trendline code in daily charts.  You could also change the periods used in the ROC calculation and the look-back periods used by the trendline algorithm. In addition, the indicator trendline code should work on all custom or standard indicators in AmiBroker.  However, the code still needs to be improved to better identify the trend origination date.

Indicator trendlines go a step beyond price trendlines and offer us unique insights into the strength of the prevailing price trend and can even be used as leading indicators of impending price reversals.  Using automated trendlines reduces the time required to generate trendlines and ensures that we are always using a consistent trendline methodology.


Your comments, feedback, and questions are always welcome and appreciated.  Please use the comment section at the bottom of this page or send me an email.

Do you have any questions about the material?  What topics would you like to see in the future?


If you found the information on www.TraderEdge.Net helpful, please pass along the link to your friends and colleagues or share the link with your social or professional networks.

The "Share / Save" button below contains links to all major social and professional networks.  If you do not see your network listed, use the down-arrow to access the entire list of networking sites.

Thank you for your support.

Brian Johnson

Copyright 2012 - Trading Insights, LLC - All Rights Reserved.


About Brian Johnson

I have been an investment professional for over 30 years. I worked as a fixed income portfolio manager, personally managing over $13 billion in assets for institutional clients. I was also the President of a financial consulting and software development firm, developing artificial intelligence based forecasting and risk management systems for institutional investment managers. I am now a full-time proprietary trader in options, futures, stocks, and ETFs using both algorithmic and discretionary trading strategies. In addition to my professional investment experience, I designed and taught courses in financial derivatives for both MBA and undergraduate business programs on a part-time basis for a number of years. I have also written four books on options and derivative strategies.
This entry was posted in AmiBroker Code, Futures, In-Depth Article, Market Timing, Technical Analysis, Trade Analysis and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *