Indicator · Pine Script v5 · On the chart

Pivot Trendlines

by g-moe· code credited to goodgains

The Pivot Trendlines indicator detects swing pivot highs and lows on the price chart using a user-defined pivot length.

View on GitHub

Repaints

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

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

pivot-trendlines-v4.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, swing-pivot-high, swing-pivot-low, large-pivot-high, large-pivot-low, trendline-draw, sph-label, spl-label, lph-label, lpl-label, pivot-length, swing-structure, trendline-connect, pivot-labels, structure-markers, high-low-pivots, overlay-trendlines, pivot-detection, market-structure, small-large-pivots

// sph
pivot_length = input.int(2, title = 'Length', minval=1)
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')

var int sph_redcount = na

sph = ta.pivothigh(high,pivot_length,pivot_length)
// sph_red


// bgcolor(not na(sph) ? color.black : na)
// plot(sph_redcount)


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


f_drawsp(value, array_labels, type) =>

    if type == 1
        array.unshift(array_labels, label.new(x=bar_index-2, 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=bar_index-2, 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)

var int last_small_signal = na 
last_small_signal := not na(sph) ? 1 : not na(spl) ? -1 : last_small_signal[1]

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

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


// 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(true, title = 'LPH⠀⠀⠀', inline = '3', group = 'Settings')
lph_color = input.color(color.blue, title = '', inline = '3', group = 'Settings')

use_lpl = input.bool(true, 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 identifies small pivots when they occur and large pivots when price breaks through the recent opposite small pivot. Labels for small pivot highs (SPH), small pivot lows (SPL), large pivot highs (LPH), and large pivot lows (LPL) are plotted directly on the price chart. Settings allow toggling each pivot type and setting custom colors. The tool visualizes market structure and swing points for trend analysis. It draws no exit signals or stop loss as it is not a strategy. The indicator works on any timeframe and may repaint as new bars are added.

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

  • Signals can change on the live bar or after it closes (repainting)

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
LPH⠀⠀⠀booltrueuse_lph
lph_colorcolorcolor.bluelph_color
LPL⠀⠀⠀booltrueuse_lpl
lpl_colorcolorcolor.bluelpl_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

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

This Pine Script indicator plots a ZigZag on the price chart. It identifies reversal points when price moves more than a set percentage from the last pivot. The script calculates forecasts using zigzag data and labels detected geometric patterns including harm

Indicator v4 TrendSupport & resistance
228 stars

This indicator plots market structure on the price chart by identifying swing highs and lows. It connects these points with lines to visualize trend direction and detect Break of Structure (BOS) when price breaks a prior swing in the current trend. Change of C

Indicator v6 TrendSupport & resistance
Updated 3 months ago · None stated GitHub repository of Market Structure - BOS & CHoCH

Open Source Fractal

by cantolab
183 stars

This indicator implements the Fractal Model concept to plot market structure shifts, liquidity sweeps, and Change in State of Delivery (CISD) levels. It overlays custom higher-timeframe candle previews, open prices, and time markers directly on the price chart

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated 2 months ago · MIT GitHub repository of Open Source Fractal
56 stars

BigBeluga Smart Money Concepts identifies and overlays market structure and volumetric order blocks directly on the price chart. It analyzes market swings, trend sweeps, and mapping structure using customizable pivot lengths and calculation modes. The indicato

Indicator v5 SMC / ICT / price actionSupport & resistance
Updated 2 months ago · None stated GitHub repository of BigBeluga - Smart Money Concepts
53 stars

Apex Trend & Liquidity Master calculates a trend cloud using customizable moving averages expanded by a volatility multiplier directly on the main price chart. It identifies dynamic support and resistance liquidity zones from swing high and low pivots, extendi

Indicator v6 TrendSupport & resistance
Updated 2 months ago · MPL-2.0 GitHub repository of Apex Trend & Liquidity Master

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