INVITE-ONLY SCRIPT

RSI WMA VWMA Divergence Indicator

76
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at mozilla.org/MPL/2.0/
// © Kenndjk

//version=6
indicator(title="RSI WMA VWMA Divergence Indicator", shorttitle="Kenndjk", format=format.price, precision=2)

oscType = input.string("RSI", "Oscillator Type", options = ["RSI", "WMA + VWMA"], group="General Settings")

// RSI Settings
rsiGroup = "RSI Settings"
rsiLengthInput = input.int(14, minval=1, title="RSI Length", group=rsiGroup)
rsiSourceInput = input.source(close, "Source", group=rsiGroup)

// WMA VWMA
wmaLength = input.int(9, "WMA Length", minval=1, group="WMA Settings")
vwmaLength = input.int(3, "VWMA Length", minval=1, group="WMA Settings")
wma = ta.wma(close, wmaLength)
vwma = ta.vwma(close, vwmaLength)
useVWMA = input.bool(true, "Use VWMA for Divergence (when WMA + VWMA mode)", group="WMA Settings")

// Oscillator selection
rsi = ta.rsi(rsiSourceInput, rsiLengthInput) // Calculate RSI always, but use conditionally
osc = oscType == "RSI" ? rsi : useVWMA ? vwma : wma

// RSI plots (conditional)
isRSI = oscType == "RSI"
rsiPlot = plot(isRSI ? rsi : na, "RSI", color=isRSI ? #7E57C2 : na)
rsiUpperBand = hline(isRSI ? 70 : na, "RSI Upper Band", color=isRSI ? #787B86 : na)
midline = hline(isRSI ? 50 : na, "RSI Middle Band", color=isRSI ? color.new(#787B86, 50) : na)
rsiLowerBand = hline(isRSI ? 30 : na, "RSI Lower Band", color=isRSI ? #787B86 : na)
fill(rsiUpperBand, rsiLowerBand, color=isRSI ? color.rgb(126, 87, 194, 90) : na, title="RSI Background Fill")
midLinePlot = plot(isRSI ? 50 : na, color = na, editable = false, display = display.none)
fill(rsiPlot, midLinePlot, 100, 70, top_color = isRSI ? color.new(color.green, 0) : na, bottom_color = isRSI ? color.new(color.green, 100) : na, title = "Overbought Gradient Fill")
fill(rsiPlot, midLinePlot, 30, 0, top_color = isRSI ? color.new(color.red, 100) : na, bottom_color = isRSI ? color.new(color.red, 0) : na, title = "Oversold Gradient Fill")

// WMA VWMA plots
wmaColor = oscType != "RSI" ? (useVWMA ? color.new(color.blue, 70) : color.blue) : na
wmaWidth = useVWMA ? 1 : 2
vwmaColor = oscType != "RSI" ? (useVWMA ? color.orange : color.new(color.orange, 70)) : na
vwmaWidth = useVWMA ? 2 : 1
plot(oscType != "RSI" ? wma : na, "WMA", color=wmaColor, linewidth=wmaWidth)
plot(oscType != "RSI" ? vwma : na, "VWMA", color=vwmaColor, linewidth=vwmaWidth)

// Smoothing MA inputs (only for RSI)
GRP = "Smoothing (RSI only)"
TT_BB = "Only applies when 'Show Bollinger Bands' is selected. Determines the distance between the SMA and the bands."
maLengthSMA = input.int(14, "SMA Length", minval=1, group=GRP, display=display.data_window)
maLengthEMA = input.int(14, "EMA Length", minval=1, group=GRP, display=display.data_window)
maLengthRMA = input.int(14, "SMMA (RMA) Length", minval=1, group=GRP, display=display.data_window)
maLengthWMA = input.int(14, "WMA Length", minval=1, group=GRP, display=display.data_window)
maLengthVWMA = input.int(14, "VWMA Length", minval=1, group=GRP, display=display.data_window)
bbMultInput = input.float(2.0, "BB StdDev", minval=0.001, maxval=50, step=0.5, tooltip=TT_BB, group=GRP, display=display.data_window)
showSMA = input.bool(false, "Show SMA", group=GRP)
showEMA = input.bool(false, "Show EMA", group=GRP)
showRMA = input.bool(false, "Show SMMA (RMA)", group=GRP)
showWMAsmooth = input.bool(false, "Show WMA", group=GRP)
showVWMAsmooth = input.bool(false, "Show VWMA", group=GRP)
showBB = input.bool(false, "Show SMA + Bollinger Bands", group=GRP, tooltip=TT_BB)

// Smoothing MA Calculations
sma_val = (showSMA or showBB) and isRSI ? ta.sma(rsi, maLengthSMA) : na
ema_val = showEMA and isRSI ? ta.ema(rsi, maLengthEMA) : na
rma_val = showRMA and isRSI ? ta.rma(rsi, maLengthRMA) : na
wma_val = showWMAsmooth and isRSI ? ta.wma(rsi, maLengthWMA) : na
vwma_val = showVWMAsmooth and isRSI ? ta.vwma(rsi, maLengthVWMA) : na
smoothingStDev = showBB and isRSI ? ta.stdev(rsi, maLengthSMA) * bbMultInput : na

// Smoothing MA plots
plot(sma_val, "RSI-based SMA", color=(showSMA or showBB) ? color.yellow : na, display=(showSMA or showBB) ? display.all : display.none, editable=(showSMA or showBB))
plot(ema_val, "RSI-based EMA", color=showEMA ? color.purple : na, display=showEMA ? display.all : display.none, editable=showEMA)
plot(rma_val, "RSI-based RMA", color=showRMA ? color.red : na, display=showRMA ? display.all : display.none, editable=showRMA)
plot(wma_val, "RSI-based WMA", color=showWMAsmooth ? color.blue : na, display=showWMAsmooth ? display.all : display.none, editable=showWMAsmooth)
plot(vwma_val, "RSI-based VWMA", color=showVWMAsmooth ? color.orange : na, display=showVWMAsmooth ? display.all : display.none, editable=showVWMAsmooth)
bbUpperBand = plot(showBB ? sma_val + smoothingStDev : na, title="Upper Bollinger Band", color=showBB ? color.green : na, display=showBB ? display.all : display.none, editable=showBB)
bbLowerBand = plot(showBB ? sma_val - smoothingStDev : na, title="Lower Bollinger Band", color=showBB ? color.green : na, display=showBB ? display.all : display.none, editable=showBB)
fill(bbUpperBand, bbLowerBand, color=showBB ? color.new(color.green, 90) : na, title="Bollinger Bands Background Fill", display=showBB ? display.all : display.none, editable=showBB)

// Divergence Settings
divGroup = "Divergence Settings"
calculateDivergence = input.bool(true, title="Calculate Divergence", group=divGroup, tooltip = "Calculating divergences is needed in order for divergence alerts to fire.")
lookbackLeft = input.int(5, "Pivot Lookback Left", minval=1, group=divGroup)
lookbackRight = input.int(5, "Pivot Lookback Right", minval=1, group=divGroup)
rangeLower = input.int(5, "Min Range for Divergence", minval=0, group=divGroup)
rangeUpper = input.int(60, "Max Range for Divergence", minval=1, group=divGroup)
showHidden = input.bool(true, "Show Hidden Divergences", group=divGroup)

bearColor = color.red
bullColor = color.green
textColor = color.white
noneColor = color.new(color.white, 100)

_inRange(cond) =>
bars = ta.barssince(cond)
rangeLower <= bars and bars <= rangeUpper

bool plFound = false
bool phFound = false

bool bullCond = false
bool bearCond = false
bool hiddenBullCond = false
bool hiddenBearCond = false

float oscLBR = na
float lowLBR = na
float highLBR = na
float prevPlOsc = na
float prevPlLow = na
float prevPhOsc = na
float prevPhHigh = na

if calculateDivergence
plFound := not na(ta.pivotlow(osc, lookbackLeft, lookbackRight))
phFound := not na(ta.pivothigh(osc, lookbackLeft, lookbackRight))

oscLBR := osc[lookbackRight]
lowLBR := low[lookbackRight]
highLBR := high[lookbackRight]

prevPlOsc := ta.valuewhen(plFound, oscLBR, 1)
prevPlLow := ta.valuewhen(plFound, lowLBR, 1)
prevPhOsc := ta.valuewhen(phFound, oscLBR, 1)
prevPhHigh := ta.valuewhen(phFound, highLBR, 1)

// Regular Bullish
oscHL = oscLBR > prevPlOsc and _inRange(plFound[1])
priceLL = lowLBR < prevPlLow
bullCond := priceLL and oscHL and plFound

// Regular Bearish
oscLL = oscLBR < prevPhOsc and _inRange(phFound[1])
priceHH = highLBR > prevPhHigh
bearCond := priceHH and oscLL and phFound

// Hidden Bullish
oscLL_hidden = oscLBR < prevPlOsc and _inRange(plFound[1])
priceHL = lowLBR > prevPlLow
hiddenBullCond := priceHL and oscLL_hidden and plFound and showHidden

// Hidden Bearish
oscHH_hidden = oscLBR > prevPhOsc and _inRange(phFound[1])
priceLH = highLBR < prevPhHigh
hiddenBearCond := priceLH and oscHH_hidden and phFound and showHidden

// Plot divergences (lines and labels on pane)
if bullCond
leftBar = ta.valuewhen(plFound, bar_index[lookbackRight], 1)
line.new(leftBar, prevPlOsc, bar_index[lookbackRight], oscLBR, xloc=xloc.bar_index, color=bullColor, width=2)
label.new(bar_index[lookbackRight], oscLBR, "R Bull", style=label.style_label_up, color=noneColor, textcolor=textColor)

if bearCond
leftBar = ta.valuewhen(phFound, bar_index[lookbackRight], 1)
line.new(leftBar, prevPhOsc, bar_index[lookbackRight], oscLBR, xloc=xloc.bar_index, color=bearColor, width=2)
label.new(bar_index[lookbackRight], oscLBR, "R Bear", style=label.style_label_down, color=noneColor, textcolor=textColor)

if hiddenBullCond
leftBar = ta.valuewhen(plFound, bar_index[lookbackRight], 1)
line.new(leftBar, prevPlOsc, bar_index[lookbackRight], oscLBR, xloc=xloc.bar_index, color=bullColor, width=2, style=line.style_dashed)
label.new(bar_index[lookbackRight], oscLBR, "H Bull", style=label.style_label_up, color=noneColor, textcolor=textColor)

if hiddenBearCond
leftBar = ta.valuewhen(phFound, bar_index[lookbackRight], 1)
line.new(leftBar, prevPhOsc, bar_index[lookbackRight], oscLBR, xloc=xloc.bar_index, color=bearColor, width=2, style=line.style_dashed)
label.new(bar_index[lookbackRight], oscLBR, "H Bear", style=label.style_label_down, color=noneColor, textcolor=textColor)

// Alert conditions
alertcondition(bullCond, title="Regular Bullish Divergence", message="Found a new Regular Bullish Divergence, Pivot Lookback Right number of bars to the left of the current bar.")
alertcondition(bearCond, title="Regular Bearish Divergence", message="Found a new Regular Bearish Divergence, Pivot Lookback Right number of bars to the left of the current bar.")
alertcondition(hiddenBullCond, title="Hidden Bullish Divergence", message="Found a new Hidden Bullish Divergence, Pivot Lookback Right number of bars to the left of the current bar.")
alertcondition(hiddenBearCond, title="Hidden Bearish Divergence", message="Found a new Hidden Bearish Divergence, Pivot Lookback Right number of bars to the left of the current bar.")

Declinazione di responsabilità

Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.