CVD指标绘图异常:背离信号延迟显示问题求助
CVD背离指标信号延迟求助
我正在编写一款可识别背离并在图表上绘制信号的Cumulative Volume Delta(CVD)指标,基于已有CVD代码,并借鉴了TradingView官方RSI背离指标的代码片段。
以下是完整代码:
//@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)
调试时发现问题:看涨/看跌条件的绘图逻辑正确,但引擎会延迟x根K线才识别到信号。我是编程新手,怀疑问题出在offset设置上,尝试调整为+lbR、-lbR等参数均无效,恳请技术帮助。
内容的提问来源于stack exchange,提问作者Oraz94
相关产品推荐
相关产品推荐

