Indicator · Pine Script v5 · On the chart

Price Action Candles with Pivot Trendlines

by g-moe· code credited to goodgains

This price-chart indicator finds swing highs and lows with a user-set pivot length and classifies them as higher high, lower high, higher low, or lower low.

View on GitHub

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

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

price-action-candles-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("Price Action Candles with Pivot Trendlines", overlay = true, max_lines_count = 500, max_labels_count = 500)

// @keywords: price-action-swing-trend, rsi-pivot-divergence, stochastic-pivot-divergence, double-top-bottom, atr-pattern-sensitivity, pivot-trendlines, swing-high-low-labels, hh-hl-lh-ll-structure, divergence-scanner, pattern-alert-shapes, multi-indicator-pivots, trend-state-tracking, last-signal-markers, price-swing-values, rsi-swing-mirror, stoch-swing-mirror, double-bottom-top-scan, pivot-length-setting, trendline-from-pivots, confluence-divergence

f_get_swing_values(sd_length, _high, _low) =>
    sh = ta.pivothigh(high, sd_length, sd_length)
    sl = ta.pivotlow(low, sd_length, sd_length)

    csh = ta.valuewhen(ta.pivothigh(high, sd_length, sd_length), _high[sd_length], 0)
    csl = ta.valuewhen(ta.pivotlow(low, sd_length, sd_length), _low[sd_length], 0)
    psh = ta.valuewhen(ta.pivothigh(high, sd_length, sd_length), _high[sd_length], 1)
    psl = ta.valuewhen(ta.pivotlow(low, sd_length, sd_length), _low[sd_length], 1)

    csh_bn = ta.valuewhen(ta.pivothigh(high, sd_length, sd_length), time[sd_length], 0)
    psh_bn = ta.valuewhen(ta.pivothigh(high, sd_length, sd_length), time[sd_length], 1)
    csl_bn = ta.valuewhen(ta.pivotlow(low, sd_length, sd_length), time[sd_length], 0)
    psl_bn = ta.valuewhen(ta.pivotlow(low, sd_length, sd_length), time[sd_length], 1)

    hh = sh >= psh
    lh = sh < psh
    ll = sl <= psl
    hl = sl > psl

    var int trend = na
    trend := ((hh and _high >= psh) or _high > csh) ? 1 : ((ll and _low <= psl) or _low < csl) ? -1 : lh or hl ? 0 : trend[1]

    var int last_signal = na
    last_signal := hh ? 2 : lh ? -1 : ll ? -2 : hl ? 1 : last_signal[1]

    [sh, sl, psh, psl, csh, csl, hh, lh, ll, hl, trend, last_signal, csh_bn, psh_bn, csl_bn, psl_bn]


// create price pivots
pivot_length = input.int(3, title = 'Swing Length')
[sh, sl, psh, psl, csh, csl, hh, lh, ll, hl, trend, last_signal, csh_bn, psh_bn, csl_bn, psl_bn] = f_get_swing_values(pivot_length, high, low)


// create rsi pivots
rsi_value = ta.rsi(close, 14)
[rsi_sh, rsi_sl, rsi_psh, rsi_psl, rsi_csh, rsi_csl, rsi_hh, rsi_lh, rsi_ll, rsi_hl, rsi_trend, rsi_last_signal,
 rsi_csh_bn, rsi_psh_bn, rsi_csl_bn, rsi_psl_bn] = f_get_swing_values(pivot_length, rsi_value, rsi_value)


// create stoch pivots
k = ta.sma(ta.stoch(close, high, low, 14), 3)
stoch_value = ta.sma(k, 3)
[stoch_sh, stoch_sl, stoch_psh, stoch_psl, stoch_csh, stoch_csl, stoch_hh, stoch_lh, stoch_ll, stoch_hl, 
 stoch_trend, stoch_last_signal, stoch_csh_bn, stoch_psh_bn, stoch_csl_bn, stoch_psl_bn] = f_get_swing_values(pivot_length, stoch_value, stoch_value)


// atr
price_atr = ta.atr(50)


// find double bottoms / double tops
var bool double_btm = na
var bool double_top = na

atrpct = input.float(0.4, title = 'Pattern Sensitivity', minval = 0.00, maxval = 5.00, step = 0.1, 
 group = 'Double Bottom / Double Top Scan Settings', inline = '1')

rsi_divpct = input.float(5.00, title = 'RSI Divergence   ', minval = 0.00, maxval = 100.00, step = 1.0, 
 group = 'Double Bottom / Double Top Scan Settings', inline = '2')
rsi_dt = input.bool(true, title = 'DT', group = 'Double Bottom / Double Top Scan Settings', inline = '2')
rsi_db = input.bool(true, title = 'DB', group = 'Double Bottom / Double Top Scan Settings', inline = '2')

stoch_divpct = input.float(5.00, title = 'Stoch Divergence ', minval = 0.00, maxval = 100.00, step = 1.0,
 group = 'Double Bottom / Double Top Scan Settings', inline = '3')
stoch_dt = input.bool(true, title = 'DT', group = 'Double Bottom / Double Top Scan Settings', inline = '3')
stoch_db = input.bool(true, title = 'DB', group = 'Double Bottom / Double Top Scan Settings', inline = '3')

double_btm := sl and (math.abs(csl-psl) <= price_atr * atrpct) ? true : false
double_top := sh and (math.abs(csh-psh) <= price_atr * atrpct) ? true : false

plotshape(double_top ? high[pivot_length] : na, title = 'Double Top', style = shape.circle, 
 location = location.absolute, offset = -pivot_length, color = color.red, size= size.small)
plotshape(double_btm ? low[pivot_length]  : na,  title = 'Double Bottom', style = shape.circle,
 location = location.absolute, offset = -pivot_length, color = color.green, size= size.small)

plotshape(sh ? high[pivot_length] : na, title = 'PH', style = shape.circle, location = location.absolute,
 offset = -pivot_length, color = color.black)
plotshape(sl ? low[pivot_length]  : na,  title = 'PL', style = shape.circle, location = location.absolute,
 offset = -pivot_length, color = color.black)

db_rsidiv = rsi_db and double_btm and (rsi_csl >= rsi_psl) and (math.abs(rsi_csl - rsi_psl) >= rsi_divpct) ? true : false
dt_rsidiv = rsi_dt and double_top and (rsi_csh <= rsi_psh) and (math.abs(rsi_csh - rsi_psh) >= rsi_divpct) ? true : false

db_stochdiv = stoch_db and double_btm and (stoch_csl >= stoch_psl) and (math.abs(stoch_csl - stoch_psl) >= stoch_divpct) ? true : false
dt_stochdiv = stoch_dt and double_top and (stoch_csh <= stoch_psh) and (math.abs(stoch_csh - stoch_psh) >= stoch_divpct) ? true : false

str_db = db_rsidiv and db_stochdiv ? ("DB - RSI & STOCH")
 : db_stochdiv ? ("DB - STOCH") 
 : db_rsidiv   ? ("DB - RSI")
 : na 

str_dt = dt_rsidiv and dt_stochdiv ? ("DT - RSI & STOCH")
 : dt_stochdiv ? ("DT - STOCH") 
 : dt_rsidiv   ? ("DT - RSI")
 : na 


// price action scans versus rsi & stochs 
rsi_lldivpct = input.float(5.00, title = 'RSI Divergence   ', minval = 0.00, maxval = 100.00, step = 1.0,
 group = 'Price-Action Scan Settings', inline = '2')
_hh_rsi = input.bool(true, title = 'HH', group = 'Price-Action Scan Settings', inline = '2')
_ll_rsi = input.bool(true, title = 'LL', group = 'Price-Action Scan Settings', inline = '2')

stoch_lldivpct = input.float(5.00, title = 'Stoch Divergence ', minval = 0.00, maxval = 100.00, step = 1.0,
 group = 'Price-Action Scan Settings', inline = '3')
_hh_stoch = input.bool(true, title = 'HH', group = 'Price-Action Scan Settings', inline = '3')
_ll_stoch = input.bool(true, title = 'LL', group = 'Price-Action Scan Settings', inline = '3')


ll_rsi   = _ll_rsi   and sl and ll and (rsi_csl >= rsi_psl) and (math.abs(rsi_csl - rsi_psl) >= rsi_divpct) 
ll_stoch = _ll_stoch and sl and ll and (stoch_csl >= stoch_psl) and (math.abs(stoch_csl - stoch_psl) >= stoch_divpct)

hh_rsi   = _hh_rsi   and sh and hh and (rsi_csh <= rsi_psh) and (math.abs(rsi_csh - rsi_psh) >= rsi_divpct) 
hh_stoch = _hh_stoch and sh and hh and (stoch_csh <= stoch_psh) and (math.abs(stoch_csh - stoch_psh) >= stoch_divpct)

str_ll = ll_rsi and ll_stoch ? ("LL - RSI & STOCH")
 : ll_stoch ? ("LL - STOCH") 
 : ll_rsi   ? ("LL - RSI")
 : na 

str_hh = hh_rsi and hh_stoch ? ("HH - RSI & STOCH")
 : hh_stoch ? ("HH - STOCH") 
 : hh_rsi   ? ("HH - RSI")
 : na 


// bullish divergences
str_bull = not na(str_db) and not na(str_ll) ? (str_db + "\n" + str_ll) 
 : not na(str_db) ? str_db
 : str_ll
if db_rsidiv or db_stochdiv or ll_rsi or ll_stoch
    alert("Bullish Alert: \n" + str_bull, alert.freq_once_per_bar_close)
    label.new(bar_index[pivot_length], low[pivot_length], text = str_bull, yloc = yloc.belowbar, style = label.style_label_up,
     color = color.new(#787b85, 0), textcolor = color.white)

    line.new(x1=psl_bn, y1=psl, x2=csl_bn, y2=csl, xloc=xloc.bar_time, color=color.green)



// bearish divergences
str_bear = not na(str_dt) and not na(str_hh) ? (str_dt + "\n" + str_hh) 
 : not na(str_dt) ? str_dt
 : str_hh
if dt_rsidiv or dt_stochdiv or hh_rsi or hh_stoch
    alert("Bearish Alert: \n" + str_bear, alert.freq_once_per_bar_close)
    label.new(bar_index[pivot_length], high[pivot_length], text = str_bear, yloc = yloc.abovebar, style = label.style_label_down,
     color = color.new(#787b85, 0), textcolor = color.white)

    line.new(x1=psh_bn, y1=psh, x2=csh_bn, y2=csh, xloc=xloc.bar_time, color=color.red)

Overview

Price Action Candles with Pivot Trendlines tracks a trend state and the latest swing signal from those pivots. The same swing comparison is run on a 14-period RSI and a smoothed stochastic, then combined with ATR to scan for double tops, double bottoms, and divergence. Sensitivity, divergence percent, and pattern toggles change which scans appear. The script only plots signals; it has no entry or exit rules.

Details

Published by

g-moe on GitHub

Code credit

goodgains, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Candlestick patterns, Support & resistance, Divergence, Oscillators & momentum

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
Swing Lengthint3pivot_length
Pattern Sensitivityfloat0.4atrpct
RSI Divergence   float5.00rsi_divpct
DTbooltruersi_dt
DBbooltruersi_db
Stoch Divergence float5.00stoch_divpct
DTbooltruestoch_dt
DBbooltruestoch_db
RSI Divergence   float5.00rsi_lldivpct
HHbooltrue_hh_rsi
LLbooltrue_ll_rsi
Stoch Divergence float5.00stoch_lldivpct
Show all 14 settings
SettingTypeDefaultVariable
HHbooltrue_hh_stoch
LLbooltrue_ll_stoch
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

23 stars

This indicator collection calculates and plots previous daily high and low price levels on the chart to identify potential supply and demand zones. It includes real-time intraday high and low calculations, RSI divergence detection across a customizable period

Indicator v4 Support & resistanceDivergence
Updated 3 years ago · None stated GitHub repository of Daily High and Low Strategy

This indicator implements a full Chanlun pipeline on the price chart. It merges contained bars, marks top and bottom fractals, and connects alternating fractals into strokes. Overlapping strokes form pivot boxes, and MACD area ratios flag trend or consolidatio

Indicator v6 Support & resistanceDivergence
Updated 2 months ago · GPL-3.0 GitHub repository of 缠论 - 分型与笔 [Chanlun Core]
5 stars

This indicator plots RSI(14) on the price chart along with regular and hidden bullish and bearish divergences. It detects divergences by comparing price highs and lows to RSI values using pivot points with configurable lookback bars. Signals appear as triangle

Indicator v4 Oscillators & momentumDivergence
Updated 2 years ago · None stated GitHub repository of CB RSI (Divergence and OB/OS)

SAB PRO Pivot Master

by IAMATOMIC001
3 stars

SAB PRO Pivot Master is a price-chart overlay that builds an RSX-style oscillator from a chosen source and length, then compares price extremes with that oscillator over a lookback window to mark pivots and bullish or bearish divergence. Overbought and oversol

Indicator v5 Oscillators & momentumVolatility & bands
Updated 12 months ago · None stated GitHub repository of SAB PRO Pivot Master
2 stars

This indicator detects regular and hidden divergences across multiple technical indicators including RSI, MACD, Stochastic, CCI, momentum, OBV, and others. It calculates pivot points on a chosen source to identify bullish and bearish divergences, then plots co

Indicator v4 DivergenceOscillators & momentum
Updated 4 years ago · MIT GitHub repository of Divergence for Many Indicators v4
2 stars

Divergence-Support/Resistence combines dual Zigzag pivot detection with momentum oscillators to identify divergence patterns and plot support and resistance levels directly on the price chart. Users can select from various built-in oscillators, such as RSI, CC

Indicator v5 DivergenceSupport & resistance Repaints
Updated 8 months ago · None stated GitHub repository of Divergence-Support/Resistence

More indicators

Trade Your Charts With a Regulated Broker

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

Compare brokers