本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("A/D Oscillator – Buy/Sell Zones", shorttitle="A/D Zones", overlay=false, max_labels_count=500, max_lines_count=500, max_boxes_count=500)
// ── Inputs
fastLen = input.int(9, "Fast length", minval=1)
slowLen = input.int(15, "Slow length", minval=1)
maType = input.string("SMA", "MA type", options=["SMA", "EMA"])
normalize = input.bool(true, "Normalise by avg volume")
normLen = input.int(50, "Normalisation length", minval=1)
showBull = input.bool(true, "Show bullish markers")
showBear = input.bool(true, "Show bearish markers")
showLabels = input.bool(true, "Show Accum/Dist labels on price chart")
showState = input.bool(true, "Show current state label")
grpD = "Divergences"
showReg = input.bool(true, "Regular divergences", group=grpD)
showHid = input.bool(false, "Hidden divergences", group=grpD)
lbL = input.int(5, "Pivot lookback left", minval=1, group=grpD)
lbR = input.int(5, "Pivot lookback right", minval=1, group=grpD)
rangeMin = input.int(5, "Min bars between pivots", minval=1, group=grpD)
rangeMax = input.int(60, "Max bars between pivots", minval=1, group=grpD)
divOnPrice = input.bool(true, "Draw divergence lines on price chart", group=grpD)
divOnOsc = input.bool(true, "Draw divergence lines on oscillator", group=grpD)
grpZ = "Buy / Sell zones"
showZones = input.bool(true, "Show buy/sell zones", group=grpZ)
zoneLen = input.int(20, "Swing lookback for zone", minval=2, group=grpZ)
zoneDepth = input.float(0.5, "Zone depth (fraction of swing → signal close)", minval=0.1, maxval=1.0, step=0.05, group=grpZ)
bufAtr = input.float(0.25, "Buffer beyond swing (ATR)", minval=0.0, step=0.05, group=grpZ)
minWAtr = input.float(0.5, "Min zone width (ATR)", minval=0.0, step=0.05, group=grpZ)
invAtr = input.float(0.5, "Invalidate when close beyond zone by (ATR)", minval=0.0, step=0.05, group=grpZ)
ladderN = input.int(5, "Ladder levels", minval=1, maxval=10, group=grpZ)
extBars = input.int(15, "Extend right (bars)", minval=1, group=grpZ)
showOscZone = input.bool(true, "Show zone on oscillator", group=grpZ)
bgInZone = input.bool(true, "Highlight osc pane when price is in zone", group=grpZ)
confirmBar = input.bool(true, "Only draw zone after bar closes (no flicker)", group=grpZ)
deleteUsed = input.bool(true, "Delete zone once used (tagged then left)", group=grpZ)
bullCol = color.new(color.lime, 0)
bearCol = color.new(color.red, 0)
hBullCol = color.new(color.lime, 40)
hBearCol = color.new(color.red, 40)
buyFill = color.new(color.teal, 80)
sellFill = color.new(color.maroon, 75)
// ── A/D line (div-by-zero + na-volume safe)
barRange = high - low
mfm = barRange == 0 ? 0.0 : ((close - low) - (high - close)) / barRange
ad = ta.cum(mfm * nz(volume))
ma(src, len) => maType == "EMA" ? ta.ema(src, len) : ta.sma(src, len)
diff = ma(ad, fastLen) - ma(ad, slowLen)
avgVol = ta.sma(nz(volume), normLen)
osc = normalize and avgVol > 0 ? diff / avgVol : diff
// ── Cross signals
bullCross = ta.crossover(osc, 0)
bearCross = ta.crossunder(osc, 0)
// ── Zone series (computed every bar for consistency)
atr = ta.atr(14)
swingLo = ta.lowest(low, zoneLen)
swingHi = ta.highest(high, zoneLen)
oscLo = ta.lowest(osc, zoneLen)
oscHi = ta.highest(osc, zoneLen)
// ── Plots
lineCol = osc >= 0 ? color.green : color.red
plot(osc, "A/D diff", color=lineCol, linewidth=2)
plot(osc, "Area", color=color.new(color.silver, 70), style=plot.style_area)
hline(0, "Zero", color=color.gray, linestyle=hline.style_dotted)
plotshape(showBull and bullCross ? osc : na, "Bull cross", shape.circle, location.absolute, color.teal, size=size.tiny)
plotshape(showBear and bearCross ? osc : na, "Bear cross", shape.circle, location.absolute, color.maroon, size=size.tiny)
// ── Accum / Dist labels on price chart
if showLabels and showBull and bullCross
label.new(bar_index, low, "Accumulation", style=label.style_label_up, color=color.teal, textcolor=color.white, size=size.small, force_overlay=true)
if showLabels and showBear and bearCross
label.new(bar_index, high, "Distribution", style=label.style_label_down, color=color.maroon, textcolor=color.white, size=size.small, force_overlay=true)
// ── Zone helpers
buildLadder(array<line> lns, array<label> lbs, float top, float bot, int n, float ref, color col) =>
for i = 0 to n - 1
lvl = n == 1 ? (top + bot) / 2 : top - (top - bot) * i / (n - 1)
pct = (lvl - ref) / ref * 100
lns.push(line.new(bar_index, lvl, bar_index + extBars, lvl, color=color.new(col, 30), style=line.style_dotted, force_overlay=true))
lbs.push(label.new(bar_index + extBars, lvl, str.tostring(lvl, format.mintick) + " | " + str.tostring(pct, "+0.00;-0.00") + "%", style=label.style_label_left, color=color.new(col, 70), textcolor=color.white, size=size.tiny, force_overlay=true))
true
killZone(box bx, box ox, array<line> lns, array<label> lbs) =>
box.delete(bx)
box.delete(ox)
for ln in lns
line.delete(ln)
lns.clear()
for lb in lbs
label.delete(lb)
lbs.clear()
extendZone(box bx, box ox, array<line> lns, array<label> lbs, int r) =>
box.set_right(bx, r)
if not na(ox)
box.set_right(ox, r)
for ln in lns
line.set_x2(ln, r)
for lb in lbs
label.set_x(lb, r)
true
// ── Zone state
var box bBox = na
var box bOscBox = na
var array<line> bLines = array.new<line>()
var array<label> bLbls = array.new<label>()
var bool bActive = false
var bool bTouched = false
var float bTop = na
var float bBot = na
var box sBox = na
var box sOscBox = na
var array<line> sLines = array.new<line>()
var array<label> sLbls = array.new<label>()
var bool sActive = false
var bool sTouched = false
var float sTop = na
var float sBot = na
// Zones only start when the state has actually flipped (and the bar has closed, if enabled)
okBar = not confirmBar or barstate.isconfirmed
bullSig = showZones and bullCross and okBar
bearSig = showZones and bearCross and okBar
// ── Flipped to DISTRIBUTION → remove any buy zone, draw sell zone
if bearSig
if bActive
killZone(bBox, bOscBox, bLines, bLbls)
bActive := false
if sActive
killZone(sBox, sOscBox, sLines, sLbls)
sTop := swingHi + atr * bufAtr
sBot := math.max(close, math.min(sTop - atr * minWAtr, swingHi - (swingHi - close) * zoneDepth))
sBox := box.new(bar_index, sTop, bar_index + extBars, sBot, border_color=color.new(color.red, 20), bgcolor=sellFill, text="SELL ZONE", text_color=color.white, text_size=size.small, text_halign=text.align_left, text_valign=text.align_bottom, force_overlay=true)
sOscBox := showOscZone ? box.new(bar_index, oscHi, bar_index + extBars, 0, border_color=color.new(color.red, 20), bgcolor=sellFill, text="SELL", text_color=color.white, text_size=size.tiny, text_halign=text.align_left, text_valign=text.align_top) : na
buildLadder(sLines, sLbls, sTop, sBot, ladderN, close, color.red)
sActive := true
sTouched := false
// ── Flipped to ACCUMULATION → remove any sell zone, draw buy zone
if bullSig
if sActive
killZone(sBox, sOscBox, sLines, sLbls)
sActive := false
if bActive
killZone(bBox, bOscBox, bLines, bLbls)
bBot := swingLo - atr * bufAtr
bTop := math.min(close, math.max(bBot + atr * minWAtr, swingLo + (close - swingLo) * zoneDepth))
bBox := box.new(bar_index, bTop, bar_index + extBars, bBot, border_color=color.new(color.teal, 20), bgcolor=buyFill, text="BUY ZONE", text_color=color.white, text_size=size.small, text_halign=text.align_left, text_valign=text.align_top, force_overlay=true)
bOscBox := showOscZone ? box.new(bar_index, 0, bar_index + extBars, oscLo, border_color=color.new(color.teal, 20), bgcolor=buyFill, text="BUY", text_color=color.white, text_size=size.tiny, text_halign=text.align_left, text_valign=text.align_bottom) : na
buildLadder(bLines, bLbls, bTop, bBot, ladderN, close, color.teal)
bActive := true
bTouched := false
inBuy = bActive and not bullSig and low <= bTop and high >= bBot
inSell = sActive and not bearSig and high >= sBot and low <= sTop
// ── Buy zone: extend, or delete when invalidated / used
if bActive and not bullSig
if inBuy
bTouched := true
bInvalid = close < bBot - atr * invAtr
bUsed = deleteUsed and bTouched and close > bTop
if bInvalid or bUsed
killZone(bBox, bOscBox, bLines, bLbls)
bActive := false
else
extendZone(bBox, bOscBox, bLines, bLbls, bar_index + extBars)
// ── Sell zone: extend, or delete when invalidated / used
if sActive and not bearSig
if inSell
sTouched := true
sInvalid = close > sTop + atr * invAtr
sUsed = deleteUsed and sTouched and close < sBot
if sInvalid or sUsed
killZone(sBox, sOscBox, sLines, sLbls)
sActive := false
else
extendZone(sBox, sOscBox, sLines, sLbls, bar_index + extBars)
bgcolor(bgInZone and inBuy ? color.new(color.lime, 85) : na, title="In buy zone")
bgcolor(bgInZone and inSell ? color.new(color.red, 85) : na, title="In sell zone")
// ── Divergence drawing helper
drawDiv(int x1, float p1, float o1, int x2, float p2, float o2, color col, bool hidden, bool isBull, string txt) =>
lStyle = hidden ? line.style_dashed : line.style_solid
if divOnPrice
line.new(x1, p1, x2, p2, color=col, width=2, style=lStyle, force_overlay=true)
if divOnOsc
line.new(x1, o1, x2, o2, color=col, width=2, style=lStyle)
label.new(x2, p2, txt, style=isBull ? label.style_label_up : label.style_label_down, color=col, textcolor=color.black, size=size.small, force_overlay=true)
// ── Pivot tracking
plFound = not na(ta.pivotlow(osc, lbL, lbR))
phFound = not na(ta.pivothigh(osc, lbL, lbR))
var float prevPLosc = na
var float prevPLprice = na
var int prevPLbar = na
var float prevPHosc = na
var float prevPHprice = na
var int prevPHbar = na
regBullDiv = false
hidBullDiv = false
regBearDiv = false
hidBearDiv = false
if plFound
curOsc = osc[lbR]
curPrice = low[lbR]
curBar = bar_index - lbR
if not na(prevPLbar)
dist = curBar - prevPLbar
if dist >= rangeMin and dist <= rangeMax
if showReg and curPrice < prevPLprice and curOsc > prevPLosc
regBullDiv := true
drawDiv(prevPLbar, prevPLprice, prevPLosc, curBar, curPrice, curOsc, bullCol, false, true, "A/D Bull Div")
if showHid and curPrice > prevPLprice and curOsc < prevPLosc
hidBullDiv := true
drawDiv(prevPLbar, prevPLprice, prevPLosc, curBar, curPrice, curOsc, hBullCol, true, true, "A/D H Bull Div")
prevPLosc := curOsc
prevPLprice := curPrice
prevPLbar := curBar
if phFound
curOsc = osc[lbR]
curPrice = high[lbR]
curBar = bar_index - lbR
if not na(prevPHbar)
dist = curBar - prevPHbar
if dist >= rangeMin and dist <= rangeMax
if showReg and curPrice > prevPHprice and curOsc < prevPHosc
regBearDiv := true
drawDiv(prevPHbar, prevPHprice, prevPHosc, curBar, curPrice, curOsc, bearCol, false, false, "A/D Bear Div")
if showHid and curPrice < prevPHprice and curOsc > prevPHosc
hidBearDiv := true
drawDiv(prevPHbar, prevPHprice, prevPHosc, curBar, curPrice, curOsc, hBearCol, true, false, "A/D H Bear Div")
prevPHosc := curOsc
prevPHprice := curPrice
prevPHbar := curBar
// ── Current state label
var label stateLbl = na
if showState and barstate.islast
label.delete(stateLbl)
isAcc = osc >= 0
stateLbl := label.new(bar_index + 2, osc, isAcc ? "Accumulation" : "Distribution", style=label.style_label_left, color=isAcc ? color.green : color.red, textcolor=color.white, size=size.small)
// ── Alerts
alertcondition(bullCross, "A/D bull cross", "Accumulation: A/D fast MA crossed above slow MA")
alertcondition(bearCross, "A/D bear cross", "Distribution: A/D fast MA crossed below slow MA")
alertcondition(regBullDiv, "A/D regular bull div", "A/D regular bullish divergence")
alertcondition(regBearDiv, "A/D regular bear div", "A/D regular bearish divergence")
alertcondition(hidBullDiv, "A/D hidden bull div", "A/D hidden bullish divergence")
alertcondition(hidBearDiv, "A/D hidden bear div", "A/D hidden bearish divergence")
alertcondition(inBuy and not inBuy[1], "Price entered BUY zone", "Price entered the A/D accumulation buy zone")
alertcondition(inSell and not inSell[1], "Price entered SELL zone", "Price entered the A/D distribution sell zone")