本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © dub4art
//@version=6
indicator('Zero-Lag Divergence', shorttitle = 'ZeroDT+', max_labels_count = 100, overlay = true)
var gRSIF = "Zero-Lag Divergence"
showDivergence = input.bool(true, 'Zero-Lag Divergence ',inline = 'sym0', group = gRSIF)
d_mode = input.string('Agreement' , title = 'Mode' , options =['CVD', 'RSI', 'Stochastic', 'Agreement'], inline = 'sym0', group=gRSIF)
agreeFilter = input.string('All Three', 'Minimum Agreement', options = ['Any Two', 'All Three'], group = gRSIF)
cumulation_length = input(23, ' Cumulative Volume Delta (CVD): Length',group = gRSIF)
rsiLength = input(14, ' RSI: Length ', inline = 'rsid01', group = gRSIF)
rsiOB = input(60, 'OB', inline = 'rsid01', group = gRSIF)
rsiOS = input(40, 'OS', inline = 'rsid01', group = gRSIF)
periodK = input.int(18, title=" Stochastic: %K Length", minval=1, inline = 'sto1', group = gRSIF)
smoothK = input.int(3, title="Smoothing", minval=1, inline = 'sto1', group = gRSIF)
stoOB = input(80, ' OB', inline = 'sto2', group = gRSIF)
stoOS = input(20, 'OS', inline = 'sto2', group = gRSIF)
showLabel = input.bool(true, 'Show Labels', group = gRSIF)
bullDivTxt = input.string('B', 'Bullish Label', inline = 'rsid3', group = gRSIF)
bullLabel_cvd = input.color(color.new(color.teal,30), 'CVD', inline = 'rsid3', group = gRSIF)
bullLabel_rsi = input.color(color.green, 'RSI', inline = 'rsid3', group = gRSIF)
bullLabel_sto = input.color(color.blue, 'Stochastic', inline = 'rsid3', group = gRSIF)
bearDivTxt = input.string('B', 'Bearish Label', inline = 'rsid4', group = gRSIF)
bearLabel_cvd = input.color(color.new(#E71A64,30), 'CVD', inline = 'rsid4', group = gRSIF)
bearLabel_rsi = input.color(color.red, 'RSI', inline = 'rsid4', group = gRSIF)
bearLabel_sto = input.color(color.orange, 'Stochastic', inline = 'rsid4', group = gRSIF)
// Agreement mode settings
showALL = input.bool(true, 'All Oscillators Agree', inline = 'ag0', group = gRSIF)
bullAgree_ALL = input.color(color.new(color.green, 50), '', inline = 'ag0', group = gRSIF)
bearAgree_ALL = input.color(color.new(color.red,50), '', inline = 'ag0', group = gRSIF)
allDivTxt = input.string('ALL', ' Divergence Text', inline = 'ag0', group = gRSIF)
showRSISto = input.bool(true, 'RSI + Stochastic', inline = 'ag1', group = gRSIF)
bullAgree_RSISto = input.color(color.new(#316457, 50), '', inline = 'ag1', group = gRSIF)
bearAgree_RSISto = input.color(color.new(color.maroon, 50), '', inline = 'ag1', group = gRSIF)
rsistoDivTxt = input.string('RSISto', ' Divergence Text', inline = 'ag1', group = gRSIF)
showCVDSto = input.bool(true, 'CVD + Stochastic', inline = 'ag2', group = gRSIF)
bullAgree_CVDSto = input.color(color.new(#316457, 50), '', inline = 'ag2', group = gRSIF)
bearAgree_CVDSto = input.color(color.new(color.maroon,50), '', inline = 'ag2', group = gRSIF)
cvdstoDivTxt = input.string('CVDSto', ' Divergence Text', inline = 'ag2', group = gRSIF)
showCVDRSI = input.bool(true, 'CVD + RSI', inline = 'ag3', group = gRSIF)
bullAgree_CVDRSI = input.color(color.new(#316457, 50), '', inline = 'ag3', group = gRSIF)
bearAgree_CVDRSI = input.color(color.new(color.maroon, 50), '', inline = 'ag3', group = gRSIF)
cvdrsiDivTxt = input.string('CVDRSI', ' Divergence Text', inline = 'ag3', group = gRSIF)
showCVD = input.bool(true, 'CVD Numerical Value', group = gRSIF)
showOBOS = input.bool(true, 'RSI Divergence Label Only if RSI >= OB or RSI <= OS', group = gRSIF)
showOBOS_sto = input.bool(true, 'Stochastic Divergence Label Only if Stochastic >= OB or Stochastic <= OS', group = gRSIF)
showLines = input.bool(false, 'Show Lines', group = gRSIF)
div_short = input.bool(true, '', inline = 'rsid1', group = gRSIF)
div_len1 = input(15, 'Short Period', inline = 'rsid1', group = gRSIF)
color1 = input.color(color.purple, 'Line', inline = 'rsid1', group = gRSIF)
div_long = input.bool(true, '', inline = 'rsid2', group = gRSIF)
div_len2 = input(40, 'Long Period', inline = 'rsid2', group = gRSIF)
color2 = input.color(color.blue, 'Line', inline = 'rsid2', group = gRSIF)
multi = input.float(0.08, 'Distance Multiplier', minval=0, maxval=1, step=0.01, inline = 'disp3', group = gRSIF)
atr_v = input.int(100, ' ATR', inline = 'disp3', group = gRSIF, tooltip = "Used to calculate label distance")
// ── Oscillators ──────────────────────────────────────────────────────────────
upper_wick = close > open ? high - close : high - open
lower_wick = close > open ? open - low : close - low
spread = high - low
body_length = spread - (upper_wick + lower_wick)
percent_upper_wick = upper_wick / spread
percent_lower_wick = lower_wick / spread
percent_body_length = body_length / spread
buying_volume = close > open ? (percent_body_length + (percent_upper_wick + percent_lower_wick) / 2) * volume : (percent_upper_wick + percent_lower_wick) / 2 * volume
selling_volume = close < open ? (percent_body_length + (percent_upper_wick + percent_lower_wick) / 2) * volume : (percent_upper_wick + percent_lower_wick) / 2 * volume
cumulative_buying_volume = ta.ema(buying_volume, cumulation_length)
cumulative_selling_volume = ta.ema(selling_volume, cumulation_length)
cumulative_volume_delta = cumulative_buying_volume - cumulative_selling_volume
osc_cvd = cumulative_volume_delta
osc_rsi = ta.rsi(close, rsiLength)
rsi_ob = osc_rsi >= rsiOB
rsi_os = osc_rsi <= rsiOS
osc_sto = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
sto_ob = osc_sto >= stoOB
sto_os = osc_sto <= stoOS
// Active oscillator for single-mode operation
osc = d_mode == 'Stochastic' ? osc_sto : d_mode == 'RSI' ? osc_rsi : d_mode == 'CVD' ? osc_cvd : osc_sto
// ── Helpers ───────────────────────────────────────────────────────────────────
f_highest(src, length, startIndex) =>
float maxValue = src[startIndex]
int _indx = 0
for i = startIndex to length by 1
if src[i] > maxValue
maxValue := src[i]
_indx := i
[maxValue, _indx]
f_lowest(src, length, startIndex) =>
float minValue = src[startIndex]
int _indx = 0
for i = startIndex to length by 1
if src[i] < minValue
minValue := src[i]
_indx := i
[minValue, _indx]
// Returns [bearishDiv, bullishDiv] for any oscillator over a given lookback
f_divSignals(oscSrc, length) =>
bullishCandle_ = close > open
bearishCandle_ = close < open
[hp, _] = f_highest(high, length, 2)
isTop_ = bearishCandle_ and bullishCandle_[1] and hp <= high[1]
[lp, _] = f_lowest(low, length, 2)
isBot_ = bullishCandle_ and bearishCandle_[1] and lp >= low[1]
[hosc, _] = f_highest(oscSrc, length, 2)
bear_ = isTop_ and (hosc > oscSrc[1])
[losc, _] = f_lowest(oscSrc, length, 2)
bull_ = isBot_ and (losc < oscSrc[1])
[bear_, bull_]
bullishCandle = close > open
bearishCandle = close < open
// ── Divergence detection (short + long, all three oscillators) ────────────────
[bearDiv_rsi_s, bullDiv_rsi_s] = f_divSignals(osc_rsi, div_len1)
[bearDiv_sto_s, bullDiv_sto_s] = f_divSignals(osc_sto, div_len1)
[bearDiv_cvd_s, bullDiv_cvd_s] = f_divSignals(osc_cvd, div_len1)
[bearDiv_rsi_l, bullDiv_rsi_l] = f_divSignals(osc_rsi, div_len2)
[bearDiv_sto_l, bullDiv_sto_l] = f_divSignals(osc_sto, div_len2)
[bearDiv_cvd_l, bullDiv_cvd_l] = f_divSignals(osc_cvd, div_len2)
// OB/OS gate applied per oscillator
bearDiv_rsi_s_gated = bearDiv_rsi_s and (not showOBOS or rsi_ob)
bullDiv_rsi_s_gated = bullDiv_rsi_s and (not showOBOS or rsi_os)
bearDiv_sto_s_gated = bearDiv_sto_s and (not showOBOS_sto or sto_ob)
bullDiv_sto_s_gated = bullDiv_sto_s and (not showOBOS_sto or sto_os)
bearDiv_rsi_l_gated = bearDiv_rsi_l and (not showOBOS or rsi_ob)
bullDiv_rsi_l_gated = bullDiv_rsi_l and (not showOBOS or rsi_os)
bearDiv_sto_l_gated = bearDiv_sto_l and (not showOBOS_sto or sto_ob)
bullDiv_sto_l_gated = bullDiv_sto_l and (not showOBOS_sto or sto_os)
// CVD has no OB/OS gate – use as-is
bearDiv_cvd_s_gated = bearDiv_cvd_s
bullDiv_cvd_s_gated = bullDiv_cvd_s
bearDiv_cvd_l_gated = bearDiv_cvd_l
bullDiv_cvd_l_gated = bullDiv_cvd_l
// ── Single-oscillator mode helpers (preserves original behaviour) ─────────────
[highestPrice, highestPriceIndx] = f_highest(high, div_len1, 2)
isTop = bearishCandle and bullishCandle[1] and highestPrice <= high[1]
[lowestPrice, lowestPriceIndx] = f_lowest(low, div_len1, 2)
isBottom = bullishCandle and bearishCandle[1] and lowestPrice >= low[1]
[highestPrice2, highestPriceindx2] = f_highest(high, div_len2, 2)
isTop2 = bearishCandle and bullishCandle[1] and highestPrice2 <= high[1]
[lowestPrice2, lowestPriceindx2] = f_lowest(low, div_len2, 2)
isBottom2 = bullishCandle and bearishCandle[1] and lowestPrice2 >= low[1]
bearishDiv = false
[highestOSC, highestOSCindx] = f_highest(osc, div_len1, 2)
if isTop
bearishDiv := highestOSC > osc[1]
bullishDiv = false
[lowestOSC, lowestOSCindx] = f_lowest(osc, div_len1, 2)
if isBottom
bullishDiv := lowestOSC < osc[1]
bearishDiv2 = false
[highestOSC2, highestOSCindx2] = f_highest(osc, div_len2, 2)
if isTop2
bearishDiv2 := highestOSC2 > osc[1]
bullishDiv2 = false
[lowestOSC2, lowestOSCindx2] = f_lowest(osc, div_len2, 2)
if isBottom2
bullishDiv2 := lowestOSC2 < osc[1]
// ── Label drawing helper ──────────────────────────────────────────────────────
f_drawLabel(isBear, yPrice, labelTxt, labelColor, labelStyle) =>
if showLabel
label.new(bar_index, yPrice, text = labelTxt, textcolor = color.white, style = labelStyle, color = labelColor, size = size.small)
// ── Single-oscillator label logic (short period) ──────────────────────────────
if isTop and bearishDiv and showDivergence and div_short
if showLines
line.new(bar_index - highestOSCindx, high[highestOSCindx], bar_index, high, xloc = xloc.bar_index, style = line.style_dashed, color = color1)
if showLabel
if d_mode == 'RSI'
label.new(bar_index, high + atr_v * multi, text = bearDivTxt, textcolor = rsi_ob and showOBOS ? color.white : #00000000, style = label.style_label_down, color = rsi_ob and showOBOS ? bearLabel_rsi : #00000000, size = size.tiny, tooltip = str.tostring(osc_rsi, "#"))
if d_mode == 'Stochastic'
label.new(bar_index, high + atr_v * multi, text = bearDivTxt, textcolor = sto_ob and showOBOS_sto ? color.white : #00000000, style = label.style_label_down, color = sto_ob and showOBOS_sto ? bearLabel_sto : #00000000, size = size.tiny, tooltip = str.tostring(osc_sto, "#"))
if d_mode == 'CVD'
label.new(bar_index, high + atr_v * multi, text = showCVD ? str.tostring(osc_cvd, "#") : bearDivTxt, textcolor = color.white, style = label.style_label_down, color = bearLabel_cvd, size = showCVD ? size.small : size.tiny, tooltip = str.tostring(osc_cvd, "#"))
if isBottom and bullishDiv and showDivergence and div_short
if showLines
line.new(bar_index - lowestOSCindx, low[lowestOSCindx], bar_index, low, xloc = xloc.bar_index, style = line.style_dashed, color = color1)
if showLabel
if d_mode == 'RSI'
label.new(bar_index, low - atr_v * multi, text = bearDivTxt, textcolor = rsi_os and showOBOS ? color.white : #00000000, style = label.style_label_down, color = rsi_os and showOBOS ? bullLabel_rsi : #00000000, size = size.tiny, tooltip = str.tostring(osc_rsi, "#"))
if d_mode == 'Stochastic'
label.new(bar_index, low - atr_v * multi, text = bullDivTxt, textcolor = sto_os and showOBOS_sto ? color.white : #00000000, style = label.style_label_up, color = sto_os and showOBOS_sto ? bullLabel_sto : #00000000, size = size.tiny, tooltip = str.tostring(osc_sto, "#"))
if d_mode == 'CVD'
label.new(bar_index, low - atr_v * multi, text = showCVD ? str.tostring(osc_cvd, "#") : bullDivTxt, textcolor = color.white, style = label.style_label_up, color = bullLabel_cvd, size = showCVD ? size.small : size.tiny, tooltip = str.tostring(osc_cvd, "#"))
// ── Single-oscillator label logic (long period) ───────────────────────────────
if isTop2 and bearishDiv2 and showDivergence and div_long
if showLines
line.new(bar_index - highestOSCindx2, high[highestOSCindx2], bar_index, high, xloc = xloc.bar_index, style = line.style_dashed, color = color2)
if showLabel
if d_mode == 'RSI'
label.new(bar_index, high + atr_v * multi, text = bearDivTxt, textcolor = rsi_ob and showOBOS ? color.white : #00000000, style = label.style_label_down, color = rsi_ob and showOBOS ? bearLabel_rsi : #00000000, size = size.tiny, tooltip = str.tostring(osc_rsi, "#"))
if d_mode == 'Stochastic'
label.new(bar_index, high + atr_v * multi, text = bearDivTxt, textcolor = sto_ob and showOBOS_sto ? color.white : #00000000, style = label.style_label_down, color = sto_ob and showOBOS_sto ? bearLabel_sto : #00000000, size = size.tiny, tooltip = str.tostring(osc_sto, "#"))
if d_mode == 'CVD'
label.new(bar_index, high + atr_v * multi, text = showCVD ? str.tostring(osc_cvd, "#") : bearDivTxt, textcolor = color.white, style = label.style_label_down, color = bearLabel_cvd, size = showCVD ? size.small : size.tiny, tooltip = str.tostring(osc_cvd, "#"))
if isBottom2 and bullishDiv2 and showDivergence and div_long
if showLines
line.new(bar_index - lowestOSCindx2, low[lowestOSCindx2], bar_index, low, xloc = xloc.bar_index, style = line.style_dashed, color = color2)
if showLabel
if d_mode == 'RSI'
label.new(bar_index, low - atr_v * multi, text = bearDivTxt, textcolor = rsi_os and showOBOS ? color.white : #00000000, style = label.style_label_down, color = rsi_os and showOBOS ? bullLabel_rsi : #00000000, size = size.tiny, tooltip = str.tostring(osc_rsi, "#"))
if d_mode == 'Stochastic'
label.new(bar_index, low - atr_v * multi, text = bullDivTxt, textcolor = sto_os and showOBOS_sto ? color.white : #00000000, style = label.style_label_up, color = sto_os and showOBOS_sto ? bullLabel_sto : #00000000, size = size.tiny, tooltip = str.tostring(osc_sto, "#"))
if d_mode == 'CVD'
label.new(bar_index, low - atr_v * multi, text = showCVD ? str.tostring(osc_cvd, "#") : bullDivTxt, textcolor = color.white, style = label.style_label_up, color = bullLabel_cvd, size = showCVD ? size.small : size.tiny, tooltip = str.tostring(osc_cvd, "#"))
// ── Agreement mode ────────────────────────────────────────────────────────────
f_agreementLabels(bearRSI, bullRSI, bearSTO, bullSTO, bearCVD, bullCVD, periodLabel, lineColor) =>
yHigh = high + atr_v * multi
yLow = low - atr_v * multi
allBear = bearRSI and bearSTO and bearCVD
allBull = bullRSI and bullSTO and bullCVD
// All Three
if showALL and allBear
if showLines
line.new(bar_index - 1, high[1], bar_index, high, xloc = xloc.bar_index, style = line.style_dashed, color = lineColor)
f_drawLabel(true, yHigh, allDivTxt + periodLabel, bearAgree_ALL, label.style_label_down)
if showALL and allBull
if showLines
line.new(bar_index - 1, low[1], bar_index, low, xloc = xloc.bar_index, style = line.style_dashed, color = lineColor)
f_drawLabel(false, yLow, allDivTxt + periodLabel, bullAgree_ALL, label.style_label_up)
// Pairs — only when All Three has NOT already fired (avoid duplicate labels)
if not allBear and not allBull
if showRSISto and bearRSI and bearSTO
if showLines
line.new(bar_index - 1, high[1], bar_index, high, xloc = xloc.bar_index, style = line.style_dashed, color = lineColor)
f_drawLabel(true, yHigh, rsistoDivTxt + periodLabel, bearAgree_RSISto, label.style_label_down)
if showRSISto and bullRSI and bullSTO
if showLines
line.new(bar_index - 1, low[1], bar_index, low, xloc = xloc.bar_index, style = line.style_dashed, color = lineColor)
f_drawLabel(false, yLow, rsistoDivTxt + periodLabel, bullAgree_RSISto, label.style_label_up)
if showCVDSto and bearCVD and bearSTO
if showLines
line.new(bar_index - 1, high[1], bar_index, high, xloc = xloc.bar_index, style = line.style_dashed, color = lineColor)
f_drawLabel(true, yHigh, cvdstoDivTxt + periodLabel, bearAgree_CVDSto, label.style_label_down)
if showCVDSto and bullCVD and bullSTO
if showLines
line.new(bar_index - 1, low[1], bar_index, low, xloc = xloc.bar_index, style = line.style_dashed, color = lineColor)
f_drawLabel(false, yLow, cvdstoDivTxt + periodLabel, bullAgree_CVDSto, label.style_label_up)
if showCVDRSI and bearCVD and bearRSI
if showLines
line.new(bar_index - 1, high[1], bar_index, high, xloc = xloc.bar_index, style = line.style_dashed, color = lineColor)
f_drawLabel(true, yHigh, cvdrsiDivTxt + periodLabel, bearAgree_CVDRSI, label.style_label_down)
if showCVDRSI and bullCVD and bullRSI
if showLines
line.new(bar_index - 1, low[1], bar_index, low, xloc = xloc.bar_index, style = line.style_dashed, color = lineColor)
f_drawLabel(false, yLow, cvdrsiDivTxt + periodLabel, bullAgree_CVDRSI, label.style_label_up)
if d_mode == 'Agreement' and showDivergence
if div_short
f_agreementLabels(bearDiv_rsi_s_gated, bullDiv_rsi_s_gated, bearDiv_sto_s_gated, bullDiv_sto_s_gated, bearDiv_cvd_s_gated, bullDiv_cvd_s_gated,'', color1)
if div_long
f_agreementLabels(bearDiv_rsi_l_gated, bullDiv_rsi_l_gated, bearDiv_sto_l_gated, bullDiv_sto_l_gated, bearDiv_cvd_l_gated, bullDiv_cvd_l_gated,'', color2)