Automatic Trendlines

For a full free tutorial explaining this code in more detail, visit the backtest-rookies (.com) website.

This indicator will plot two trend lines at any given time. A resistance trend line and a support trend line . The resistance trend is shown with red circles and is created by joining swing highs together. The second is a support trend which is created by joining swing lows.

Since we need swings to make the trend, the trend line code contains code for the swing detection. You can play around with the swing detection to alter how frequently new trend lines are detected. Relying on swings also means that there will be some delay in trend detection depending on how you configure the swing detection. The higher you set rightbars, the more lag you will have before a trend is detected. However, at the same time the quality of the pivots found will increase. So it is a trade-off you need to come to terms with and decide what the best settings are for you.


A single trend line is made up of several components.

  • Pivot Points: Marked as blue or orange circles. There will be two pivots per trend.
  • Orange/Purple Lines: Connecting all pivot points . You will see these lines change direction slightly each time a new pivot is detected (new circles appear).
  • Green/Red Circle lines: Showing the trend line from the earliest moment a new trend is detected.
  • Blue Dashed lines: Joining the purple and green/red circle lines so the full trend line can be seen.

Note: The blue dashed lines use pine-scripts drawing functions. As such, there is a limit to how many of these can be placed on a chart. When the limit is reached, the oldest line will be removed so the newest can be drawn. This means that if you detect enough trends and scroll back in time, the blue dashed lines will disappear at some point!
Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Want to use this script on a chart?


500 coins
Good. Can you please add a switch to only plot resistance trendlines (connecting a pivot high with a lower pivot high) and support trendlines (connecting a pivot low with a higher pivot low)?
The script connects only two consecutive pivots, why not connect say the first of the ten last that meet the criteria of being a resistance trendline for pivot highs or support trendline for pivot lows?
+1 Reply
sal157011 sal157011
@sal157011, Something like this:
SandeepKarthi sal157011
@sal157011, Let me give a shot on this and share, Should be a simple one
thanks for sharing this. I am also a reader of BacktestRookies website for 2 years.
Sir, how do you get the green segment and the red segment to the current period?
Thanks for sharing the Knowledge. God Bless.
Appreciated your efforts and generosity <3
@ICEKI, you are most welcome. Hope you find it useful!