Indicator · Pine Script v5 · On the chart

Pivot Trendlines

by g-moe· code credited to goodgains

Pivot Trendlines is an indicator that plots horizontal support and resistance lines along with trendline support and resistance based on swing pivots of customizable lengths.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code166
Settings27inputs
LicenceNone statedfile: MPL-2.0
Last update5 months ago27 May 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

166 lines of Pine Script v5 from Tradingview/pivot-trendlines.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

pivot-trendlines.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © goodgains


//@version=5
indicator("Pivot Trendlines", overlay = true)

// @keywords: pivot-trendlines, horizontal-support, horizontal-resistance, diagonal-trendlines, three-groups, pivot-length, valuewhen-pivots, support-resistance, trendline-draw, extend-lines, multi-pivot, swing-lines, chart-levels, resistance-support, pivot-groups, line-styles, price-structure, breakout-levels, hl-lines, trend-channels

n  = input.int(1, title = 'Pivot Length', inline = '1', group = 'Trendline Group 1', minval = 1)
hs_color1 = input.color(color.gray, title = "Horizontal Support    ", inline = '2', group = 'Trendline Group 1')
hr_color1 = input.color(color.gray, title = "Horizontal Resistance  ", inline = '3', group = 'Trendline Group 1')
ts_color1 = input.color(color.gray, title = "Trendline Support     ", inline = '4', group = 'Trendline Group 1')
tr_color1 = input.color(color.gray, title = "Trendline Resistance  ", inline = '5', group = 'Trendline Group 1')

_style_hs1  = input.string('Solid', title = '', options = ['Solid', 'Dotted', 'Dashed'], group = 'Trendline Group 1', inline = '2')
_style_hr1   = input.string('Solid', title = '', options = ['Solid', 'Dotted', 'Dashed'], group = 'Trendline Group 1', inline = '3')
_style_ts1   = input.string('Solid', title = '', options = ['Solid', 'Dotted', 'Dashed'], group = 'Trendline Group 1', inline = '4')
_style_tr1   = input.string('Solid', title = '', options = ['Solid', 'Dotted', 'Dashed'], group = 'Trendline Group 1', inline = '5')

style_hs1 = _style_hs1 == "Solid" ? line.style_solid : _style_hs1 == "Dotted" ? line.style_dotted : line.style_dashed
style_hr1 = _style_hr1 == "Solid" ? line.style_solid : _style_hr1 == "Dotted" ? line.style_dotted : line.style_dashed
style_ts1 = _style_ts1 == "Solid" ? line.style_solid : _style_ts1 == "Dotted" ? line.style_dotted : line.style_dashed
style_tr1 = _style_tr1 == "Solid" ? line.style_solid : _style_tr1 == "Dotted" ? line.style_dotted : line.style_dashed


n2 = input.int(2, title = 'Pivot Length', inline = '1', group = 'Trendline Group 2', minval = 1)
hs_color2 = input.color(color.gray, title = "Horizontal Support    ", inline = '2', group = 'Trendline Group 2')
hr_color2 = input.color(color.gray, title = "Horizontal Resistance  ", inline = '3', group = 'Trendline Group 2')
ts_color2 = input.color(color.gray, title = "Trendline Support     ", inline = '4', group = 'Trendline Group 2')
tr_color2 = input.color(color.gray, title = "Trendline Resistance  ", inline = '5', group = 'Trendline Group 2')

_style_hs2  = input.string('Solid', title = '', options = ['Solid', 'Dotted', 'Dashed'], group = 'Trendline Group 2', inline = '2')
_style_hr2   = input.string('Solid', title = '', options = ['Solid', 'Dotted', 'Dashed'], group = 'Trendline Group 2', inline = '3')
_style_ts2   = input.string('Solid', title = '', options = ['Solid', 'Dotted', 'Dashed'], group = 'Trendline Group 2', inline = '4')
_style_tr2   = input.string('Solid', title = '', options = ['Solid', 'Dotted', 'Dashed'], group = 'Trendline Group 2', inline = '5')

style_hs2 = _style_hs2 == "Solid" ? line.style_solid : _style_hs2 == "Dotted" ? line.style_dotted : line.style_dashed
style_hr2 = _style_hr2 == "Solid" ? line.style_solid : _style_hr2 == "Dotted" ? line.style_dotted : line.style_dashed
style_ts2 = _style_ts2 == "Solid" ? line.style_solid : _style_ts2 == "Dotted" ? line.style_dotted : line.style_dashed
style_tr2 = _style_tr2 == "Solid" ? line.style_solid : _style_tr2 == "Dotted" ? line.style_dotted : line.style_dashed


n3 = input.int(3, title = 'Pivot Length', inline = '1', group = 'Trendline Group 3', minval = 1)
hs_color3 = input.color(color.gray, title = "Horizontal Support    ", inline = '2', group = 'Trendline Group 3')
hr_color3 = input.color(color.gray, title = "Horizontal Resistance  ", inline = '3', group = 'Trendline Group 3')
ts_color3 = input.color(color.gray, title = "Trendline Support     ", inline = '4', group = 'Trendline Group 3')
tr_color3 = input.color(color.gray, title = "Trendline Resistance  ", inline = '5', group = 'Trendline Group 3')

_style_hs3  = input.string('Solid', title = '', options = ['Solid', 'Dotted', 'Dashed'], group = 'Trendline Group 3', inline = '2')
_style_hr3   = input.string('Solid', title = '', options = ['Solid', 'Dotted', 'Dashed'], group = 'Trendline Group 3', inline = '3')
_style_ts3   = input.string('Solid', title = '', options = ['Solid', 'Dotted', 'Dashed'], group = 'Trendline Group 3', inline = '4')
_style_tr3   = input.string('Solid', title = '', options = ['Solid', 'Dotted', 'Dashed'], group = 'Trendline Group 3', inline = '5')

style_hs3 = _style_hs3 == "Solid" ? line.style_solid : _style_hs3 == "Dotted" ? line.style_dotted : line.style_dashed
style_hr3 = _style_hr3 == "Solid" ? line.style_solid : _style_hr3 == "Dotted" ? line.style_dotted : line.style_dashed
style_ts3 = _style_ts3 == "Solid" ? line.style_solid : _style_ts3 == "Dotted" ? line.style_dotted : line.style_dashed
style_tr3 = _style_tr3 == "Solid" ? line.style_solid : _style_tr3 == "Dotted" ? line.style_dotted : line.style_dashed



f_calculate_and_draw_lines(p_length, 
 _h_resistance, _h_resistancestyle, _h_support, _h_supportstyle, 
 _t_resistance, _t_resistancestyle, _t_support, _t_supportstyle) =>

    ph = ta.pivothigh(high, 0, p_length) and high[p_length] == ta.highest(high[p_length], p_length) 
    pl = ta.pivotlow(low, 0, p_length)   and low[p_length]   == ta.lowest(low[p_length], p_length) 

    csh = ta.valuewhen(ph, high[p_length], 0)
    csl = ta.valuewhen(pl, low[p_length], 0)
    psh = ta.valuewhen(ph, high[p_length], 1)
    psl = ta.valuewhen(pl, low[p_length], 1)

    csh_time = ta.valuewhen(ph, bar_index[p_length], 0)
    csl_time  = ta.valuewhen(pl, bar_index[p_length], 0)
    psh_time  = ta.valuewhen(ph, bar_index[p_length], 1)
    psl_time  = ta.valuewhen(pl, bar_index[p_length], 1)

    var line hl_resistance = na
    var line hl_resistance2 = na
    var line hl_support = na
    var line hl_support2 = na
    var line tl_resistance = na
    var line tl_support = na

    if not na(ph)
        line.delete(hl_resistance[1])
        line.delete(hl_resistance2[1])
        line.delete(tl_resistance[1])

        hl_resistance := line.new(x1=csh_time, y1=csh, x2=csh_time+1, y2=csh, xloc=xloc.bar_index,
         color=_h_resistance, style = _h_resistancestyle, extend=extend.right)
        hl_resistance2 := line.new(x1=psh_time, y1=psh, x2=psh_time+1, y2=psh, xloc=xloc.bar_index,
         color=_h_resistance, style = _h_resistancestyle, extend=extend.right)
        tl_resistance := line.new(x1=psh_time, y1=psh, x2=csh_time, y2=csh, xloc=xloc.bar_index,
         color=_t_resistance, style = _t_resistancestyle, extend=extend.right)

    if not na(pl)
        line.delete(hl_support[1])
        line.delete(hl_support2[1])
        line.delete(tl_support[1])

        hl_support := line.new(x1=csl_time, y1=csl, x2=csl_time+1, y2=csl, xloc=xloc.bar_index,
         color=_h_support, style = _h_supportstyle, extend=extend.right)
        hl_support2 := line.new(x1=psl_time, y1=psl, x2=psl_time+1, y2=psl, xloc=xloc.bar_index,
         color=_h_support, style = _h_supportstyle, extend=extend.right)
        tl_support := line.new(x1=psl_time, y1=psl, x2=csl_time, y2=csl, xloc=xloc.bar_index,
         color=_t_support, style = _t_supportstyle, extend=extend.right)


    resistance_1 = csh
    resistance_2 = psh
    support_1 = csl
    support_2 = psl

    resistance_slope = (csh - psh) / (csh_time - psh_time)
    support_slope = (csl - psl) / (csl_time - psl_time)

    resistance_tl = resistance_slope*(bar_index-csh_time) + csh
    support_tl = support_slope*(bar_index-csl_time) + csl

    [resistance_1, resistance_2, resistance_tl, support_1, support_2, support_tl]




[r1_1, r1_2, tl_resistance1, s1_1, s1_2, tl_support1] = f_calculate_and_draw_lines(n, 
 hr_color1, style_hr1, hs_color1, style_hs1, 
 tr_color1, style_tr1, ts_color1, style_ts1)
plot(r1_1, color = hr_color1, title = 'Group 1 R1', show_last = 1)
plot(s1_1, color = hs_color1, title = 'Group 1 S1', show_last = 1)
plot(r1_2, color = hr_color1, title = 'Group 1 R1', show_last = 1)
plot(s1_2, color = hs_color1, title = 'Group 1 R2', show_last = 1)
plot(tl_resistance1,    color = tr_color1, title = 'Group 1 TL R1', show_last = 1)
plot(tl_support1,       color = ts_color1, title = 'Group 1 TL S1', show_last = 1)



[r2_1, r2_2, tl_resistance2, s2_1, s2_2, tl_support2] = f_calculate_and_draw_lines(n2, 
 hr_color2, style_hr2, hs_color2, style_hs2, 
 tr_color2, style_tr2, ts_color2, style_ts2)
plot(r2_1, color = hr_color2, title = 'Group 2 R1', show_last = 1)
plot(s2_1, color = hs_color2, title = 'Group 2 S1', show_last = 1)
plot(r2_2, color = hr_color2, title = 'Group 2 R2', show_last = 1)
plot(s2_2, color = hs_color2, title = 'Group 2 R2', show_last = 1)
plot(tl_resistance2,    color = tr_color2, title = 'Group 2 TL R2', show_last = 1)
plot(tl_support2,       color = ts_color2, title = 'Group 2 TL S2', show_last = 1)


[r3_1, r3_2, tl_resistance3, s3_1, s3_2, tl_support3] = f_calculate_and_draw_lines(n3, 
 hr_color3, style_hr3, hs_color3, style_hs3, 
 tr_color3, style_tr3, ts_color3, style_ts3)
plot(r3_1, color = hr_color3, title = 'Group 3 R1', show_last = 1)
plot(s3_1, color = hs_color3, title = 'Group 3 S1', show_last = 1)
plot(r3_2, color = hr_color3, title = 'Group 3 R2', show_last = 1)
plot(s3_2, color = hs_color3, title = 'Group 3 R2', show_last = 1)
plot(tl_resistance3,    color = tr_color3, title = 'Group 3 TL R3', show_last = 1)
plot(tl_support3,       color = ts_color3, title = 'Group 3 TL S3', show_last = 1)





// ph = ta.pivothigh(high, 0, n) and high[n] == ta.highest(high[n], n) 
// pl = ta.pivotlow(low, 0, n)   and low[n]   == ta.lowest(low[n], n) 
// barcolor(ph?color.red : pl?color.green : na, offset = -n)

Overview

Three separate groups allow adjustment of pivot length to draw multiple horizontal lines at key levels and connecting trendlines on the price chart. Users configure colors and line styles for each group to highlight potential breakout zones and trend channels. The tool provides visual reference levels derived from pivot points without generating buy or sell signals.

Details

Published by

g-moe on GitHub

Code credit

goodgains, as written in the script's header

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Pivot Lengthint1n
Horizontal Support    colorcolor.grayhs_color1
Horizontal Resistance  colorcolor.grayhr_color1
Trendline Support     colorcolor.grayts_color1
Trendline Resistance  colorcolor.graytr_color1
_style_hs1stringSolid_style_hs1
_style_hr1stringSolid_style_hr1
_style_ts1stringSolid_style_ts1
_style_tr1stringSolid_style_tr1
Pivot Lengthint2n2
Horizontal Support    colorcolor.grayhs_color2
Horizontal Resistance  colorcolor.grayhr_color2
Show all 27 settings
SettingTypeDefaultVariable
Trendline Support     colorcolor.grayts_color2
Trendline Resistance  colorcolor.graytr_color2
_style_hs2stringSolid_style_hs2
_style_hr2stringSolid_style_hr2
_style_ts2stringSolid_style_ts2
_style_tr2stringSolid_style_tr2
Pivot Lengthint3n3
Horizontal Support    colorcolor.grayhs_color3
Horizontal Resistance  colorcolor.grayhr_color3
Trendline Support     colorcolor.grayts_color3
Trendline Resistance  colorcolor.graytr_color3
_style_hs3stringSolid_style_hs3
_style_hr3stringSolid_style_hr3
_style_ts3stringSolid_style_ts3
_style_tr3stringSolid_style_tr3
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Sideway Zones Live

by dearvn
257 stars

Sideway Zones Live is an overlay indicator that estimates inner and outer sideways bands from recent highs and lows with a rational quadratic kernel. It tracks swing pivots over a loopback period and counts bars that remain between the outer high and low. Afte

Indicator v5 TrendSupport & resistance
Updated 7 weeks ago · None stated GitHub repository of Sideway Zones Live
56 stars

This indicator calculates dynamic upper and lower trendlines from pivot highs and pivot lows across a specified bar length. Trendline slopes are derived using ATR, standard deviation, or linear regression methods. It plots trendlines and breakout signal labels

Indicator v5 TrendSupport & resistance Repaints
Updated 2 months ago · None stated GitHub repository of Trendlines With Breaks [LUX]

Reversal Breakout System

by TamaraKGreen
46 stars

This indicator detects inside candles inside larger parent candles on the price chart. It plots colored boxes around inside and parent candles, fills them with user-adjustable transparency, draws Fibonacci retracement lines and mother candle range lines, and d

Indicator v6 Candlestick patternsBreakout
Updated 8 weeks ago · None stated GitHub repository of Reversal Breakout System
24 stars

This indicator maps Smart Money Concepts (SMC) market structure directly onto the price chart. It detects internal and swing structure breaks (BOS and CHoCH), tracks bullish and bearish order blocks, identifies fair value gaps (FVG), highlights equal highs and

Indicator v5 SMC / ICT / price actionSupport & resistance Repaints
Updated 12 days ago · Custom licence GitHub repository of Smart Money Concepts [LuxAlgo] Test B

The Market Tradability Engine calculates multi-factor tradability, balance, compression, and direction scores to evaluate market regime quality. It classifies market conditions into eight distinct states, including Balance, Compression, Ready, Breakout, Trend,

Indicator v6 TrendVolatility & bands
Updated 3 days ago · MIT GitHub repository of Market Tradability Engine [WavesUnchained]

This indicator tracks swing highs and swing lows to generate trend-following trailing stop levels on the price chart. When the closing price breaks above the previous swing high, the system establishes a bullish state using the recent swing low as a trailing s

Indicator v4 TrendBreakout
Updated 4 years ago · None stated GitHub repository of Accurate Swing Trading System

More Support & resistance scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers