Indicator · Pine Script v5 · On the chart

Pivot Trendlines

by g-moe· code credited to goodgains

Pivot Trendlines marks confirmed pivot highs and pivot lows on the price chart and tracks small pivot highs and lows from consecutive red or green candles.

View on GitHub

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

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

pivot-trendlines-v7.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, max_labels_count = 500)

// @keywords: pivot-trendlines, pivot-high, pivot-low, sph-spl, small-pivot-high, small-pivot-low, candle-count, green-red-bars, swing-tracker, background-signal, pivot-cross, trendline-labels, pivot-length, bar-summation, sph-label, spl-label, pivot-shape, trend-signal, consecutive-bars, pivot-markers

pivot_length = input.int(2, title = 'Length', minval=1)

sh = ta.pivothigh(high,pivot_length,pivot_length) 
sl = ta.pivotlow(low,pivot_length,pivot_length) 

var int sh_bn = 0
var int sl_bn = 0

sh_bn := nz(ta.valuewhen(not na(sh),bar_index[pivot_length],0), 0)
sl_bn := nz(ta.valuewhen(not na(sl),bar_index[pivot_length],0), 0)

greenbar = close >= open ? 1 : 0
redbar = close < open ? 1 : 0 


num_of_green = math.sum(greenbar, math.max(bar_index-sl_bn,1))
num_of_red = math.sum(redbar, math.max(bar_index-sh_bn,1))
plot(num_of_green, color = color.green)
plot(num_of_red, color = color.red)


// sph
use_sph = input.bool(true, title = 'SPH⠀⠀⠀', inline = '1', group = 'Settings')
sph_color = input.color(color.rgb(0, 0, 0), title = '', inline = '1', group = 'Settings')


// spl
use_spl = input.bool(true, title = 'SPL⠀⠀⠀', inline = '2', group = 'Settings')
spl_color = input.color(color.rgb(0, 0, 0), title = '', inline = '2', group = 'Settings')


// bgcolor(num_of_red == 2 ? color.new(color.red,70) : num_of_green ==2 ? color.new(color.green,70) : na)



plotshape(sh ? high[pivot_length] : na, title = 'xu', style = shape.xcross, location = location.absolute,
 color = color.new(color.red,0), offset = -pivot_length, textcolor = sph_color, size = size.tiny, editable = false)
plotshape(sl ? low[pivot_length] : na, title = 'xd', style = shape.xcross, location = location.absolute,
 color = color.new(color.green,0), offset = -pivot_length, textcolor = spl_color, size = size.tiny, editable = false)

// plotshape(sph, title = 'sph', style = shape.labeldown, location = location.abovebar, text = 'sph',
//  color = color.new(color.white,100), offset = -pivot_length, textcolor = sph_color, size = size.tiny, editable = false)
// plotshape(spl, title = 'spl', style = shape.labelup, location = location.belowbar, text = 'spl',
//  color = color.new(color.white,100), offset = -pivot_length, textcolor = spl_color, size = size.tiny, editable = false)




var float sh_tracker = na
var float sl_tracker = na
var int sh_trackerbn = na
var int sl_trackerbn = na

var bool sph = na 
var bool spl = na 
var int last_small_signal = 0


sph := num_of_red > 2 and sh_tracker[1] != 0 and last_small_signal[1] != -1
spl := num_of_green > 2 and sl_tracker[1] != 9999999 and last_small_signal[1] != 1
last_small_signal := barstate.isfirst ? 0 : sph ? -1 : spl ? 1 : last_small_signal[1]

sh_tracker   := barstate.isfirst ? 0 : spl ? 0 : not na(sh) ? math.max(sh, sh_tracker[1]) : sh_tracker[1]
sh_trackerbn := barstate.isfirst ? 0 : spl ? 0 : not na(sh) and sh > sh_tracker[1] ? bar_index : sh_trackerbn[1]

sl_tracker   := barstate.isfirst ? 9999999 : sph ? 9999999 : not na(sl) ? math.min(sl, sl_tracker[1]) : sl_tracker[1]
sl_trackerbn := barstate.isfirst ? 0 : sph ? 0 : not na(sl) and sl < sl_tracker[1] ? bar_index : sl_trackerbn[1]

bgcolor(last_small_signal == 1 ? color.new(color.green,80) : last_small_signal == - 1 ? color.new(color.red,80) : na)



plot(sh_tracker, color = color.red)
plot(sl_tracker, color = color.green)






f_drawsp(value, index, array_labels, type) =>

    if type == 1
        array.unshift(array_labels, label.new(x=index, y=value, text='sph', xloc=xloc.bar_index,
         yloc=yloc.abovebar, style=label.style_none, size = size.small, textalign=text.align_center,
         color = sph_color, textcolor = use_sph ? sph_color : color.new(sph_color,100)))
    else if type == -1 and use_spl
        array.unshift(array_labels, label.new(x=index, y=value, text='spl', xloc=xloc.bar_index,
         yloc=yloc.belowbar, style=label.style_none, size = size.small, textalign=text.align_center,
         color = sph_color, textcolor = use_spl ? spl_color : color.new(spl_color,100)))

var sphs = array.new_float(200, na)
var spls = array.new_float(200, na)

var sphs_labels = array.new_label(500,na)
var spls_labels = array.new_label(500,na)



if sph
    // if last_small_signal[1] == -1
    //     array.unshift(sphs,sh_tracker)
    //     f_drawsp(sh_tracker, sphs_labels, 1)
    // else if sh_tracker >= array.get(sphs,0)
    //     label.set_y(array.first(sphs_labels), sh_tracker)
    //     label.set_x(array.first(sphs_labels), bar_index-2)
    //     array.set(sphs, 0, sh_tracker)
    f_drawsp(sh_tracker, sh_trackerbn, sphs_labels, 1)

if spl
    // if last_small_signal[1] == 1
    //     array.unshift(spls, sl_tracker)
    //     f_drawsp(sl_tracker, spls_labels, -1)
    // else if sl_tracker <= array.get(spls,0)
    //     label.set_y(array.first(spls_labels), sl_tracker)
    //     label.set_x(array.first(spls_labels), bar_index-2)
    //     array.set(spls, 0, sl_tracker)
    f_drawsp(sl_tracker, sl_trackerbn, spls_labels, -1)









// // to mark LPH we need to break most recent spl
// var bool taken_out_spl = false
// var bool taken_out_sph = false

// var bool LPL = na
// var bool LPH = na

// var int last_large_signal = 0

// taken_out_spl := not na(spl) ? false : low <= array.first(spls) ? true : taken_out_spl[1]
// taken_out_sph := not na(sph) ? false : high >= array.first(sphs) ? true : taken_out_sph[1]

// LPL := (taken_out_sph and not taken_out_sph[1]) and last_large_signal[1] != -1
// LPH := (taken_out_spl and not taken_out_spl[1]) and last_large_signal[1] != 1
// last_large_signal := nz(LPL,false) ? -1 : nz(LPH,false) ? 1 : nz(last_large_signal[1],0)
// // barcolor(LPL ? color.green : LPH ? color.red : na)
// // barcolor(last_large_signal == 1 ? color.green : last_large_signal == -1 ? color.red : na)


// var float track_temp_LPL = na
// var int   track_temp_LPL_bn = na

// var float track_temp_LPH = na
// var int   track_temp_LPH_bn = na

// use_lph = input.bool(false, title = 'LPH⠀⠀⠀', inline = '3', group = 'Settings')
// lph_color = input.color(color.blue, title = '', inline = '3', group = 'Settings')

// use_lpl = input.bool(false, title = 'LPL⠀⠀⠀', inline = '4', group = 'Settings')
// lpl_color = input.color(color.blue, title = '', inline = '4', group = 'Settings')

// if LPH
//     track_temp_LPL := low
//     track_temp_LPL_bn := bar_index
//     label.new(x=track_temp_LPH_bn[1], y=track_temp_LPH[1], text = 'LPH', style = label.style_label_center,
//      yloc=yloc.abovebar, color = use_lph ? lph_color : color.new(lph_color,100), textcolor = use_lph ? sph_color : color.new(sph_color,100),
//      size = size.small)


// if LPL
//     track_temp_LPH := high
//     track_temp_LPH_bn := bar_index
//     label.new(x=track_temp_LPL_bn[1], y=track_temp_LPL[1], text = 'LPL', style = label.style_label_center,
//      yloc=yloc.belowbar, color = use_lpl ? lpl_color : color.new(lpl_color,100), textcolor = use_lpl ? spl_color : color.new(spl_color,100),
//      size = size.small)



// if low <= track_temp_LPL[1]
//     track_temp_LPL := low
//     track_temp_LPL_bn := bar_index

// if high >= track_temp_LPH[1]
//     track_temp_LPH := high
//     track_temp_LPH_bn := bar_index

Overview

Pivot Trendlines plots the count of green bars since the last pivot low and red bars since the last pivot high, and it colors the background according to the latest small-pivot signal. The pivot length sets how many bars confirm each pivot, while toggles and colors control the SPH and SPL markers. The script does not place trades or define exits.

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
Lengthint2pivot_length
SPH⠀⠀⠀booltrueuse_sph
sph_colorcolorcolor.rgb(0, 0, 0)sph_color
SPL⠀⠀⠀booltrueuse_spl
spl_colorcolorcolor.rgb(0, 0, 0)spl_color
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

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

Pro Pattern Al-Sat

by MeridianAlgo
9 stars

This overlay indicator marks bullish and bearish chart patterns on the price chart and can hide either side. Optional filters require agreement from fast and slow EMAs, Supertrend, and ADX, plus volume, RSI, nearby pivot support or resistance, and a user-defin

Indicator v6 Candlestick patternsTrend
Updated 4 months ago · MIT GitHub repository of Pro Pattern Al-Sat

This overlay indicator detects tweezer tops and bottoms and kangaroo-tail pin bars, then draws supply and demand zones from those patterns. It scores each zone, decays freshness, merges overlapping zones, and can invalidate a zone after a close-through or afte

Indicator v6 Candlestick patternsSupport & resistance
Updated 3 days ago · MIT GitHub repository of Tweezer & Kangaroo Zones [WavesUnchained]
7 stars

This indicator plots Smart Money Concepts and price action components directly on the price chart. It highlights bullish and bearish order blocks, multi-timeframe pivot points, market structure swings with key Fibonacci retracement levels (0.618 and 0.786), zo

Indicator v5 SMC / ICT / price actionSupport & resistance Repaints
Updated 3 months ago · None stated GitHub repository of Cryptokazancev Strategy Pack

TheStrat Suite

by 345-strategies
5 stars

This indicator implements Rob Smith's TheStrat price action methodology across up to six configurable timeframes. It classifies candles into inside, directional, and outside bars, identifies patterns like hammers, shooters, and failed 2s, and calculates magnit

Indicator v6 Candlestick patternsSupport & resistance Repaints
Updated 4 days ago · MPL-2.0 GitHub repository of TheStrat Suite

JINADA Up

by cathxrsys
3 stars

JINADA Up is an all-in-one chart overlay indicator that combines support and resistance zones, multiple moving averages, candlestick body and wick ratio analysis, and dynamic stop levels. It identifies support and resistance levels across user-defined lookback

Indicator v6 TrendMoving averages
Updated 9 months ago · GPL-3.0 GitHub repository of JINADA Up

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