Indicator · Pine Script v5 · On the chart

Pivot Trendlines v2

by g-moe· code credited to goodgains

This indicator plots horizontal support and resistance levels alongside sloping diagonal trendlines based on pivot high and pivot low points directly on the price chart.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code198
Settings28inputs
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

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

pivot-trendlines-v2.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_v2", overlay = true)

// @keywords: pivot-trendlines-v2, pivot-high-low, horizontal-support-resistance, diagonal-trendlines, three-trendline-groups, extend-trendline-options, pivot-length-config, support-resistance-lines, valuewhen-pivots, trendline-styles, multi-pivot-length, horizontal-resistance, horizontal-support, sloping-resistance, sloping-support, line-extend-modes, pivot-based-lines, configurable-line-style, three-pivot-groups, overlay-trendlines

trendline_options = input.string("Extend", title = 'Trendline Extending', options = ['Extend', 'Extend Horizontal', 'No Extend'])

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

    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

    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=bar_index, y2=csh, xloc=xloc.bar_index,
         color=_h_resistance, style = _h_resistancestyle, extend= trendline_options == 'Extend' ? extend.right : extend.none)
        hl_resistance2 := line.new(x1=psh_time, y1=psh, x2=bar_index, y2=psh, xloc=xloc.bar_index,
         color=_h_resistance, style = _h_resistancestyle, extend= trendline_options == 'Extend' ? extend.right : extend.none)
        tl_resistance := line.new(x1=psh_time, y1=psh, x2=bar_index, y2=resistance_tl, xloc=xloc.bar_index,
         color=_t_resistance, style = _t_resistancestyle, extend= trendline_options == 'Extend' ? extend.right : extend.none)

    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=bar_index, y2=csl, xloc=xloc.bar_index,
         color=_h_support, style = _h_supportstyle, extend= trendline_options == 'Extend' ? extend.right : extend.none)
        hl_support2 := line.new(x1=psl_time, y1=psl, x2=bar_index, y2=psl, xloc=xloc.bar_index,
         color=_h_support, style = _h_supportstyle, extend= trendline_options == 'Extend' ? extend.right : extend.none)
        tl_support := line.new(x1=psl_time, y1=psl, x2=bar_index, y2=support_tl, xloc=xloc.bar_index,
         color=_t_support, style = _t_supportstyle, extend= trendline_options == 'Extend' ? extend.right : extend.none)


    var line hl_resistance_extend = na
    var line hl_resistance2_extend  = na
    var line hl_support_extend  = na
    var line hl_support2_extend  = na
    var line tl_resistance_extend  = na
    var line tl_support_extend  = na
    if trendline_options == 'Extend Horizontal' and barstate.islast
        line.delete(hl_resistance_extend[1])
        line.delete(hl_resistance2_extend[1])
        line.delete(tl_resistance_extend[1])

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

        line.delete(hl_support_extend[1])
        line.delete(hl_support2_extend[1])
        line.delete(tl_support_extend[1])

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

    [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)

[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)

[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(r1_1, color = hr_color1, title = 'Group 1 R1', show_last = 1)
plot(r2_1, color = hr_color2, title = 'Group 2 R1', show_last = 1)
plot(r3_1, color = hr_color3, title = 'Group 3 R1', show_last = 1)
plot(s1_1, color = hs_color1, title = 'Group 1 S1', show_last = 1)
plot(s2_1, color = hs_color2, title = 'Group 2 S1', show_last = 1)
plot(s3_1, color = hs_color3, title = 'Group 3 S1', show_last = 1)

plot(r1_2, color = hr_color1, title = 'Group 1 R1', show_last = 1)
plot(r2_2, color = hr_color2, title = 'Group 2 R2', show_last = 1)
plot(r3_2, color = hr_color3, title = 'Group 3 R2', show_last = 1)
plot(s1_2, color = hs_color1, title = 'Group 1 R2', show_last = 1)
plot(s2_2, color = hs_color2, title = 'Group 2 R2', show_last = 1)
plot(s3_2, color = hs_color3, title = 'Group 3 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)
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)
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)


// f_extend_to_current_bar() =>


// 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

Pivot Trendlines v2 provides three independent groups of pivot length settings, allowing traders to monitor short-term and long-term key levels simultaneously. Users can customize line extension modes, colors, and line styles for each level type. Pivot points rely on historical confirmation bars, introducing a slight delay before new trendlines appear.

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
Trendline ExtendingstringExtendtrendline_options
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
Show all 28 settings
SettingTypeDefaultVariable
Horizontal Resistance  colorcolor.grayhr_color2
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

575 stars

This indicator detects equal highs and lows by comparing prices within user-defined left and right bars using a tolerance percentage. It plots horizontal lines on the price chart for equal highs in one color and lows in another. A screener allows input of mult

Indicator v5 Support & resistanceTrend
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Equal Highs and Lows with Screener

Function - Linear Regression

by ricardosantos79
261 stars

This indicator fits a linear regression through stored pivot highs and lows and draws the resulting channel on the price chart. A function returns predicted values, maximum and minimum deviations, and the average absolute deviation. Confirmed two-bar pivots up

Indicator v5 TrendSupport & resistance
Updated 4 years ago · None stated GitHub repository of Function - Linear Regression

AI Trend

by dearvn
257 stars

AI Trend is an indicator that plots pivot points, trend lines, and support and resistance levels directly on the price chart. It marks high and low pivots based on user-defined length, draws trend lines that check for violations at specified points, and identi

Indicator v5 TrendSupport & resistance
Updated 7 weeks ago · None stated GitHub repository of AI Trend

Dynamic Linear Regression

by TradersPost
228 stars

Dynamic Linear Regression calculates and plots linear regression trendlines and standard deviation channels directly on the price chart over a user-selected time period. Traders can configure static channels locked at the end time, dynamic channels that contin

Indicator v6 TrendSupport & resistance
Updated 3 months ago · None stated GitHub repository of Dynamic Linear Regression

Ichimoku Cloud

by harryguiacorn
220 stars

This indicator plots Ichimoku Cloud elements on the price chart, including Tenkan-sen, Kijun-sen, Senkou Span A and B lines with optional Chikou Span. It signals trend changes via Tenkan-sen and Kijun-sen crosses and places arrows at Kijun-sen direction shifts

Indicator v5 TrendMoving averages
Updated 1 year ago · None stated GitHub repository of Ichimoku Cloud
219 stars

This indicator identifies local swing highs and swing lows to construct a Zig Zag pattern on the main price chart. It evaluates pivot points based on a customizable number of surrounding left and right bars. Using persistent variables, the script tracks previo

Indicator v4 Support & resistanceTrend Repaints
Updated 5 years ago · None stated GitHub repository of Teaching: Finding Highs and Lows 6

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