1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.
Source Code
102 lines of Pine Script v6 from scripts/divergence-engine.pine.txt, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.
divergence-engine.pine.txt
//@version=6
indicator("Divergence Engine", overlay = false, max_bars_back = 500)
rsiLen = input.int(14, "RSI length", minval = 2)
lb = input.int(5, "Pivot bars left", minval = 1)
rb = input.int(5, "Pivot bars right", minval = 1)
minGap = input.int(5, "Min bars between pivots", minval = 1)
maxGap = input.int(60, "Max bars between pivots", minval = 2)
showHidden = input.bool(true, "Show hidden divergences")
rsi = ta.rsi(close, rsiLen)
plot(rsi, "RSI", color.new(color.purple, 0), 2)
hline(70, "Overbought", color.new(color.gray, 40))
hline(30, "Oversold", color.new(color.gray, 40))
type Pivot
int bar
float osc
float price
var array<Pivot> lows = array.new<Pivot>()
var array<Pivot> highs = array.new<Pivot>()
pl = ta.pivotlow(rsi, lb, rb)
ph = ta.pivothigh(rsi, lb, rb)
bool bull = false
bool hBull = false
bool bear = false
bool hBear = false
if not na(pl)
Pivot c = Pivot.new(bar_index - rb, pl, low[rb])
n = array.size(lows)
if n > 0
for i = n - 1 to 0
Pivot p = array.get(lows, i)
gap = c.bar - p.bar
if gap > maxGap
break
if gap >= minGap
bull := c.price < p.price and c.osc > p.osc
hBull := showHidden and c.price > p.price and c.osc < p.osc
break
array.push(lows, c)
if array.size(lows) > 20
array.shift(lows)
if not na(ph)
Pivot c = Pivot.new(bar_index - rb, ph, high[rb])
n = array.size(highs)
if n > 0
for i = n - 1 to 0
Pivot p = array.get(highs, i)
gap = c.bar - p.bar
if gap > maxGap
break
if gap >= minGap
bear := c.price > p.price and c.osc < p.osc
hBear := showHidden and c.price < p.price and c.osc > p.osc
break
array.push(highs, c)
if array.size(highs) > 20
array.shift(highs)
plotshape(bull, "Bull", shape.labelup, location.bottom,
color.new(color.teal, 0), text = "Bull", textcolor = color.white)
plotshape(bear, "Bear", shape.labeldown, location.top,
color.new(color.red, 0), text = "Bear", textcolor = color.white)
plotshape(hBull, "H Bull", shape.triangleup, location.bottom,
color.new(color.teal, 40), text = "H Bull", textcolor = color.teal)
plotshape(hBear, "H Bear", shape.triangledown, location.top,
color.new(color.red, 40), text = "H Bear", textcolor = color.red)
var int nBull = 0
var int nBear = 0
var int nHBull = 0
var int nHBear = 0
var string lastType = "none"
var int lastBar = na
nBull += bull ? 1 : 0
nBear += bear ? 1 : 0
nHBull += hBull ? 1 : 0
nHBear += hBear ? 1 : 0
if bull or bear or hBull or hBear
lastBar := bar_index
lastType := bull ? "Bull" : bear ? "Bear" : hBull ? "H Bull" : "H Bear"
var table t = table.new(position.top_right, 2, 7,
bgcolor = color.new(color.black, 15), border_width = 1)
row(r, name, val, col) =>
table.cell(t, 0, r, name, text_color = col)
table.cell(t, 1, r, val, text_color = color.white)
if barstate.islast
age = na(lastBar) ? "-" : str.tostring(bar_index - lastBar)
row(0, "Divergence", "Count", color.white)
row(1, "Bull", str.tostring(nBull), color.teal)
row(2, "Bear", str.tostring(nBear), color.red)
row(3, "H Bull", str.tostring(nHBull), color.teal)
row(4, "H Bear", str.tostring(nHBear), color.red)
row(5, "Most recent", lastType, color.yellow)
row(6, "Age in bars", age, color.yellow)
Overview
Divergence Engine stores confirmed RSI pivot highs and lows, then compares each new pivot with the previous one inside a bar-gap window. A lower price low with a higher RSI low is marked Bull, and a higher price high with a lower RSI high is marked Bear. Hidden divergences can be shown with separate labels. A table counts each type and shows the latest signal and its age in bars. There are no trade entries, exits, or stops.
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
RSI length
int
14
rsiLen
Pivot bars left
int
5
lb
Pivot bars right
int
5
rb
Min bars between pivots
int
5
minGap
Max bars between pivots
int
60
maxGap
Show hidden divergences
bool
true
showHidden
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.
This indicator scans a pasted list of symbols for RSI directional momentum and shows matching long and short signals in a table in a separate pane. The user chooses continuous break, continuous retracement, flip break, flip retracement, divergence, or hidden d
This indicator calculates the Relative Strength Index (RSI) across four user-selected timeframes and plots the results in a separate pane. It identifies regular bullish and bearish divergences between price pivot points and the primary timeframe RSI value. Add
Oscillator Suite plots a normalised price oscillator in a separate pane and overlays a signal line that can use SMA, EMA, WMA, HMA, RMA, VWMA, T3, ZEMA, KAMA, or GKEMA. It marks bullish and bearish divergences, a smoothed money-flow series, confluence areas an
This indicator plots a smoothed Z-score of cumulative volume delta in a separate pane, showing whether buying or selling pressure is dominant and when the reading enters extreme zones. It marks spot accumulation, climax or exhaustion, and pivot-based price-vol
This indicator plots a heuristic volume-balance oscillator in a separate pane. It estimates signed volume from the close location inside each bar, multiplies that by volume, and sums the proxy over a rolling window. The sum is scaled between minus one and plus
Calculates a custom-period Relative Strength Index alongside an Exponential Moving Average of the RSI in a separate pane. It identifies pivot highs and lows directly on the RSI plot to highlight trendlines and potential divergence patterns. The area between th