PROTECTED SOURCE SCRIPT

RSI WMA VWMA Divergence Indicator

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

oscType = input.string("RSI", "Oscillator Type", options = ["RSI", "WMA9 + VWMA3"], 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
wma9 = ta.wma(close, 9)
vwma3 = ta.vwma(close, 3)
useVWMA = input.bool(true, "Use VWMA3 for Divergence (when WMA9 + VWMA3 mode)", group="WMA Settings")

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

// 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
plot(oscType != "RSI" ? wma9 : na, "WMA9", color=oscType != "RSI" ? color.blue : na)
plot(oscType != "RSI" ? vwma3 : na, "VWMA3", color=oscType != "RSI" ? color.orange : na)

// Smoothing MA inputs (only for RSI)
GRP = "Smoothing (RSI only)"
TT_BB = "Only applies when 'SMA + Bollinger Bands' is selected. Determines the distance between the SMA and the bands."
maTypeInput = input.string("SMA", "Type", options = ["None", "SMA", "SMA + Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group = GRP, display = display.data_window)
maLengthInput = input.int(14, "Length", 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)
enableMA = maTypeInput != "None" and oscType == "RSI"
isBB = maTypeInput == "SMA + Bollinger Bands" and oscType == "RSI"

// Smoothing MA Calculation
ma(source, length, MAtype) =>
switch MAtype
"SMA" => ta.sma(source, length)
"SMA + Bollinger Bands" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)

// Smoothing MA plots
smoothingMA = enableMA ? ma(rsi, maLengthInput, maTypeInput) : na
smoothingStDev = isBB ? ta.stdev(rsi, maLengthInput) * bbMultInput : na
plot(smoothingMA, "RSI-based MA", color=enableMA ? color.yellow : na, display = enableMA ? display.all : display.none, editable = enableMA)
bbUpperBand = plot(isBB ? smoothingMA + smoothingStDev : na, title = "Upper Bollinger Band", color=isBB ? color.green : na, display = isBB ? display.all : display.none, editable = isBB)
bbLowerBand = plot(isBB ? smoothingMA - smoothingStDev : na, title = "Lower Bollinger Band", color=isBB ? color.green : na, display = isBB ? display.all : display.none, editable = isBB)
fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 90) : na, title="Bollinger Bands Background Fill", display = isBB ? display.all : display.none, editable = isBB)

// 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.")

免責聲明

這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。