OPEN-SOURCE SCRIPT

Precise RSI Divergence & Volume Indicator

//version=5
indicator("Precise RSI Divergence & Volume Indicator", overlay=true)

// Input Settings
length = input(14, title="RSI Length")
source = close
rsi = ta.rsi(source, length)
volThreshold = input(1.5, title="Volume Threshold Multiplier")

// Function to detect pivot highs
pivotHigh(series, left, right) =>
ta.valuewhen(ta.pivothigh(series, left, right), series, 0)

// Function to detect pivot lows
pivotLow(series, left, right) =>
ta.valuewhen(ta.pivotlow(series, left, right), series, 0)

// Detect RSI and Price Highs and Lows
priceHigh = pivotHigh(high, 5, 5)
priceLow = pivotLow(low, 5, 5)
rsiHigh = pivotHigh(rsi, 5, 5)
rsiLow = pivotLow(rsi, 5, 5)

// Bullish Divergence Condition
bullishDivergence = not na(priceLow) and not na(rsiLow) and priceLow < priceLow[1] and rsiLow > rsiLow[1]

// Bearish Divergence Condition
bearishDivergence = not na(priceHigh) and not na(rsiHigh) and priceHigh > priceHigh[1] and rsiHigh < rsiHigh[1]

// Volume Condition for Major Volume Spikes
avgVol = ta.sma(volume, length)
highVolume = volume > avgVol * volThreshold

// Plot Bullish Divergence
plotshape(bullishDivergence and highVolume, location=location.belowbar, color=color.green, style=shape.labelup, title="Bullish Divergence w/ Volume", size=size.small)

// Plot Bearish Divergence
plotshape(bearishDivergence and highVolume, location=location.abovebar, color=color.red, style=shape.labeldown, title="Bearish Divergence w/ Volume", size=size.small)

// Alert Conditions
alertcondition(bullishDivergence and highVolume, title="Bullish Divergence Alert", message="Bullish RSI Divergence with High Volume Detected!")
alertcondition(bearishDivergence and highVolume, title="Bearish Divergence Alert", message="Bearish RSI Divergence with High Volume Detected!")

Penafian