Reputation: 1
I'm trying to code a Cumulative Volume Delta indicator that defines divergences to plot on chart. I've used an already made CVD code and i've added the divergence snippet of code from the RSI divergence indicator made by tradingview.
Here is what it looks like:
//@version=5
indicator(title = "Cumulative Volume Delta", shorttitle = "CVD", overlay = false, format = format.volume, precision = 1, timeframe = "", timeframe_gaps = true)
lbR = input(title="Pivot Lookback Right", defval=14, tooltip = "For bearish Div")
lbL = input(title="Pivot Lookback Left", defval=14, tooltip = "For bullish Div")
// Calculate true range (tw), body width (bw), and body (body)
tw = high - math.max(open, close)
bw = math.min(open, close) - low
body = math.abs(close - open)
// Function to calculate rate based on a condition (cond)
_rate(cond) =>
ret = 0.5 * (tw + bw + (cond ? 2 * body : 0)) / (tw + bw + body)
ret := nz(ret) == 0 ? 0.5 : ret
ret
// Calculate delta for up and down movements
deltaup = volume * _rate(open <= close)
deltadown = volume * _rate(open > close)
delta = close >= open ? deltaup : -deltadown
// Calculate cumulative delta
cumdelta = ta.cum(delta)
plFound = na(ta.pivotlow(cumdelta, lbL, lbR)) ? false : true
phFound = na(ta.pivothigh(cumdelta, lbL, lbR)) ? false : true
// Regular Bullish
// Osc: Higher Low
cvdHL = cumdelta[lbR] > ta.valuewhen(plFound, cumdelta[lbR], 1)
// Price: Lower Low
priceLL = low[lbR] < ta.valuewhen(plFound, low[lbR], 1)
bullCond = plFound and cvdHL and priceLL
plot(plFound ? cumdelta[lbR] : na, offset = -lbR, title="Regular Bullish", linewidth=2, color=(bullCond ? color.green : na))
plotshape(bullCond ? cumdelta[lbR] : na,offset = -lbR, title="Regular Bullish Label", text=" Bull ", style=shape.labelup, location=location.absolute, color=color.green, textcolor=color.white)
// Regular Bearish
// Osc: Lower High
cvdLH = cumdelta[lbR] < ta.valuewhen(phFound, cumdelta[lbR], 1)
// Price: Higher High
priceHH = high[lbR] > ta.valuewhen(phFound, high[lbR], 1)
bearCond = phFound and cvdLH and priceHH
plot(phFound ? cumdelta[lbR] : na, offset = -lbR, title="Regular Bearish",linewidth=2,color=(bearCond ? color.red : na))
plotshape(bearCond ? cumdelta[lbR] : na,offset = -lbR,title="Regular Bearish Label",text=" Bear ",style=shape.labeldown,location=location.absolute,color=color.red,textcolor=color.white)
plot(cumdelta)
plotchar(bullCond ? cumdelta[lbR] : na, offset = -lbR, "CVDBull", "4", location.bottom, color = color.rgb(0, 255, 21), size = size.small)
plotchar(bearCond ? cumdelta[lbR] : na, offset = -lbR, "CVDBear", "8", location.top, color = color.rgb(255, 0, 0), size = size.small)
I have found out this problem when debugging, in the image attached you can clearly see that the bullish/bearish conditions are plotted correctly but the engine reads it x bars later.
I'm new to coding and any help is much appreciated. IMAGE SHOWING THE PROBLEM!
I believe the problem is around the offset setting, i've tried to change it to +lbR, to -lbR, to an intager or boolean series but nothing worked.
Upvotes: 0
Views: 191