lovetotrade
Member
- Platform
-
- TradingView
here is the TV code:
Code:
//@version=4
study("RSI Div at VWAP StDev", overlay=true, resolution="")
// RSI Inputs
len = input(14, minval=1, title="Length")
os = input(30, minval=1, title="Oversold")
ob = input(70, minval=1, title="Overbought")
hr = input(50, minval=1, title="Bear Div Reset level")
lr = input(50, minval=1, title="Bull Div Reset level")
xbars = input(defval=14, title="Div lookback period (bars)?", type=input.integer, minval=1)
// VWAP Inputs
useVWAP = input(true, title="Use Std Dev")
devUp = input(1.51, title="Stdev above")
devDn = input(1.51, title="Stdev below")
// DIVS code
rsi = rsi(close, len)
hb = abs(highestbars(rsi, xbars)) // Finds bar with highest value in last X bars
lb = abs(lowestbars(rsi, xbars)) // Finds bar with lowest value in last X bars
max = float(na)
max_rsi = float(na)
min = float(na)
min_rsi = float(na)
divbear = bool(na)
divbull = bool(na)
// If bar with lowest / highest is current bar and rsi is oversold/overbought, use it's value
max := hb == 0 and rsi > ob ? close : na(max[1]) ? close : max[1]
max_rsi := hb == 0 and rsi > ob ? rsi : na(max_rsi[1]) ? rsi : max_rsi[1]
min := lb == 0 and rsi < os ? close : na(min[1]) ? close : min[1]
min_rsi := lb == 0 and rsi < os ? rsi : na(min_rsi[1]) ? rsi : min_rsi[1]
// Reset if RSI crosses the Reset level
if (rsi < hr)
max_rsi := float(na)
if (rsi > lr)
min_rsi := float(na)
// Compare high of current bar being examined with previous bar's high
// If curr bar high is higher than the max bar high in the lookback window range
if close > max // we have a new high
max := close // change variable "max" to use current bar's high value
if rsi > max_rsi and rsi > ob // we have a new high
max_rsi := rsi // change variable "max_rsi" to use current bar's RSI value
if close < min // we have a new low
min := close // change variable "min" to use current bar's low value
if rsi < min_rsi and rsi < os // we have a new low
min_rsi := rsi // change variable "min_rsi" to use current bar's RSI value
// Detects divergences between price and indicator with 1 candle delay so it filters out repeating divergences
if (max[1] > max[2]) and (rsi[1] < max_rsi) and (rsi <= rsi[1])
divbear := true
if (min[1] < min[2]) and (rsi[1] > min_rsi) and (rsi >= rsi[1])
divbull := true
// VWAP Code
tickerid = tickerid(syminfo.prefix, syminfo.ticker, session.regular, adjustment.splits)
start = security(tickerid, "D", time)
newSession = iff(change(start), 1, 0)
vwapsum = 0.0
volumesum = 0.0
v2sum = 0.0
myvwap = 0.0
dev = 0.0
pos = 0
possig = 1
vwapsum := iff(newSession, hl2*volume, vwapsum[1]+hl2*volume)
volumesum := iff(newSession, volume, volumesum[1]+volume)
v2sum := iff(newSession, volume*hl2*hl2, v2sum[1]+volume*hl2*hl2)
myvwap := vwapsum/volumesum
dev := sqrt(max(v2sum/volumesum - myvwap*myvwap, 0))
U2=myvwap + devUp * dev
D2=myvwap - devDn * dev
pos := 0
if (useVWAP)
if (high > U2 and divbear)
pos := 1
if (low < D2 and divbull)
pos := -1
else
if (divbear)
pos := 1
if (divbull)
pos := -1
plotshape(pos == -1, "long", location = location.belowbar, color = color.green, style=shape.triangleup, size=size.tiny)
plotshape(pos == 1, "short", location = location.abovebar, color = color.red, style=shape.triangledown, size=size.tiny)
Last edited by a moderator: