OPEN-SOURCE SCRIPT

LT's RSI Invalidation Targets

110
//version=5
indicator("Triple RSI Divergence", overlay=true)

// === INPUTS ===
rsiLength = input.int(14, title="RSI Length")
src = input.source(close, "Source")
lookback = input.int(50, title="Lookback Period")

// === RSI ===
rsi = ta.rsi(src, rsiLength)

// === Find local peaks and troughs ===
isTop = ta.pivothigh(rsi, 5, 5)
isBottom = ta.pivotlow(rsi, 5, 5)

var float[] rsiTroughs = array.new_float()
var float[] priceTroughs = array.new_float()
var int[] rsiTroughBars = array.new_int()

if isBottom
array.unshift(rsiTroughs, rsi[5])
array.unshift(priceTroughs, low[5])
array.unshift(rsiTroughBars, bar_index[5])

if array.size(rsiTroughs) > 3
array.pop(rsiTroughs)
array.pop(priceTroughs)
array.pop(rsiTroughBars)

// === Check for triple bullish divergence ===
bullDiv = false
if array.size(rsiTroughs) == 3
r1 = array.get(rsiTroughs, 2)
r2 = array.get(rsiTroughs, 1)
r3 = array.get(rsiTroughs, 0)

p1 = array.get(priceTroughs, 2)
p2 = array.get(priceTroughs, 1)
p3 = array.get(priceTroughs, 0)

// Price: Lower lows, RSI: Higher lows
if p1 > p2 and p2 > p3 and r1 < r2 and r2 < r3
bullDiv := true
label.new(array.get(rsiTroughBars, 0), low, "Triple Bullish Divergence", style=label.style_label_up, color=color.green, textcolor=color.white)


// === Same for triple bearish divergence ===
var float[] rsiPeaks = array.new_float()
var float[] pricePeaks = array.new_float()
var int[] rsiPeakBars = array.new_int()

if isTop
array.unshift(rsiPeaks, rsi[5])
array.unshift(pricePeaks, high[5])
array.unshift(rsiPeakBars, bar_index[5])

if array.size(rsiPeaks) > 3
array.pop(rsiPeaks)
array.pop(pricePeaks)
array.pop(rsiPeakBars)

bearDiv = false
if array.size(rsiPeaks) == 3
r1 = array.get(rsiPeaks, 2)
r2 = array.get(rsiPeaks, 1)
r3 = array.get(rsiPeaks, 0)

p1 = array.get(pricePeaks, 2)
p2 = array.get(pricePeaks, 1)
p3 = array.get(pricePeaks, 0)

// Price: Higher highs, RSI: Lower highs
if p1 < p2 and p2 < p3 and r1 > r2 and r2 > r3
bearDiv := true
label.new(array.get(rsiPeakBars, 0), high, "Triple Bearish Divergence", style=label.style_label_down, color=color.red, textcolor=color.white)

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.