Indicator · Pine Script v5 · On the chart

Pivot Based Trailing Maxima & Minima [LuxAlgo]

by theonetrade· code credited to LuxAlgo

This indicator calculates trailing maximum and minimum price bounds reset by pivot high and pivot low points over a specified lookback period.

View on GitHub

Repaints

GitHub stars34on the repository
Pine versionv5runs as is
Lines of code116
Settings7inputs
LicenceMIT
Last update2 weeks ago8 Nov 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

116 lines of Pine Script v5 from assets/indicator/bit-exact/pivot_based_trailing_maxima_minima_luxalgo_BEUgu3o1.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

pivot_based_trailing_maxima_minima_luxalgo_BEUgu3o1.pine
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © LuxAlgo

//@version=5
indicator("Pivot Based Trailing Maxima & Minima [LuxAlgo]", "LuxAlgo - Pivot Based Trailing Maxima & Minima", overlay = true, max_bars_back = 500, max_lines_count = 500)
//------------------------------------------------------------------------------
//Settings
//-----------------------------------------------------------------------------{
length = input.int(24, minval = 2, maxval = 500)
backpaint = input(false, tooltip = 'Backpainting offset displayed elements in the past. Disable backpainting to see real time information returned by the indicator.')

//Style
maxCss = input.color(color.teal, 'Trailing Maximum Color', group = 'Style')
minCss = input.color(color.red , 'Trailing Minimum Color', group = 'Style')
avgCss = input.color(#ff5d00   , 'Trailing Maximum Color', group = 'Style')

bullFill = input.color(color.new(color.teal,80), 'Uptrend Area'  , group = 'Style')
bearFill = input.color(color.new(color.red,80) , 'Downtrend Area', group = 'Style')

//-----------------------------------------------------------------------------}
//Calculation
//-----------------------------------------------------------------------------{
var float max = na
var float min = na
var offset = backpaint ? length : 0

ph = ta.pivothigh(length, length)
pl = ta.pivotlow(length, length)

if ph or pl
    max := high[length]
    min := low[length]

max := math.max(high[offset], max)
min := math.min(low[offset], min)

avg = math.avg(max,min)

//-----------------------------------------------------------------------------}
//Non offset trailing max/min
//-----------------------------------------------------------------------------{
n = bar_index

max_prev = max
min_prev = min
avg_prev = avg
max2 = max
min2 = min

fill_css = fixnan(ph ? bearFill : pl ? bullFill : na)

if barstate.islast and backpaint
    //Delete previously set lines
    for line_object in line.all
        line.delete(line_object)

    //Loop trough calculation window
    for i = 0 to length-1
        max2 := math.max(high[length-1-i], max_prev)
        min2 := math.min(low[length-1-i], min_prev)
        avg2 = math.avg(max2, min2)
        
        //Set lines
        line1 = line.new(n-(length-i), max_prev, n-(length-1-i), max2, color= maxCss)
        line2 = line.new(n-(length-i), min_prev, n-(length-1-i), min2, color= minCss)
        linefill.new(line1,line2,color.new(fill_css, 80))

        line.new(n-(length-i), avg_prev, n-(length-1-i), avg2, color= avgCss)
        
        max_prev := max2
        min_prev := min2
        avg_prev := avg2

//-----------------------------------------------------------------------------}
//Plots
//-----------------------------------------------------------------------------{
plot1 = plot(max, 'Trailing Maximum'
  , ph or pl ? na : maxCss
  , 1
  , plot.style_linebr
  , offset = -offset)

plot2 = plot(min, 'Trailing Minimum'
  , ph or pl ? na : minCss
  , 1
  , plot.style_linebr
  , offset = -offset)

plot(avg, 'Average'
  , ph or pl ? na : avgCss
  , 1
  , plot.style_linebr
  , offset = -offset)

fill(plot1, plot2, ph or pl ? na : fill_css)

//Labels
plotshape(pl ? pl : na, "Pivot High"
  , shape.labelup
  , location.absolute
  , maxCss
  , -offset
  , text = "▲"
  , textcolor = color.white
  , size = size.tiny)

plotshape(ph ? ph : na, "Pivot Low"
  , shape.labeldown
  , location.absolute
  , minCss
  , -offset
  , text = "▼"
  , textcolor = color.white
  , size = size.tiny)

//-----------------------------------------------------------------------------}

Overview

Pivot Based Trailing Maxima & Minima [LuxAlgo] plots the trailing maximum, trailing minimum, and their average value directly on the price chart, filling the space between the bounds to highlight trend direction. A toggleable backpainting setting allows historical visual alignment with pivot points, which repaints lines on past bars when activated.

Details

Published by

theonetrade on GitHub

Code credit

LuxAlgo, 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)

Topics

algorithmic-tradingbacktestingbitcoinccxtcrypto-tradingllm-agentoffline-backtestingopen-source-tradingpine-scriptpine-to-pythonpinescriptpynecorepythonquantquantitative-financestrategy-backtestingtechnical-analysistrading-indicatorstrading-strategiestradingview

Settings

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

SettingTypeDefaultVariable
lengthint24length
backpaintboolfalsebackpaint
Trailing Maximum Colorcolorcolor.tealmaxCss
Trailing Minimum Colorcolorcolor.redminCss
Trailing Maximum Colorcolor#ff5d00avgCss
Uptrend Areacolorcolor.new(color.teal,80)bullFill
Downtrend Areacolorcolor.new(color.red,80)bearFill
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

Swing labels

by tradoor-terminal
320 stars

Calculates swing highs and swing lows on the price chart using pivot high and pivot low detection over a specified length. It places labels above pivot highs and below pivot lows to highlight key structural swing points. The indicator measures and displays the

Indicator v6 Support & resistanceTrend
Updated 3 days ago · AGPL-3.0 GitHub repository of Swing labels

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 5 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 2 months 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 4 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

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