本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Wyckoff Distribution Tracker", "Wyckoff Distribution Tracker", overlay = true, max_labels_count = 500, max_lines_count = 500)
// The mirror of the Accumulation Tracker. Tracks a Wyckoff distribution range as it develops:
// BC -> AR -> ST -> (UT/UTAD -> Test) -> SOW (break of the Ice) -> LPSY
// BC Buying Climax: panic high on climactic volume and spread, in an uptrend
// AR Automatic Reaction: the first swing low after the climax sets the bottom of the range (the Ice)
// ST Secondary Test: revisits the BC area on lighter volume
// UT Upthrust (UTAD when it comes late): brief push above resistance that is quickly rejected
// Test lower high on lighter volume after the upthrust
// SOW Sign of Weakness: wide-spread, high-volume close below the Ice
// LPSY Last Point of Supply: rally back to the Ice on lighter volume, old support now resistance
// Each event can only print once the earlier ones exist. If price violates the structure
// (breaks resistance, the upthrust turns into a real breakout, the range times out), the whole
// structure is invalidated and the scanner starts over.
// ───────── Inputs ─────────
grpD = "Detection"
trendLen = input.int(50, "Uptrend MA length", minval = 5, group = grpD)
bcLookback = input.int(50, "BC: highest high of last N bars", minval = 5, group = grpD)
volLen = input.int(20, "Volume average length", minval = 2, group = grpD)
atrLen = input.int(14, "ATR length", minval = 1, group = grpD)
climaxVol = input.float(2.0, "Climax volume (× avg)", minval = 1.0, step = 0.1, group = grpD)
climaxRng = input.float(1.5, "Climax spread (× ATR)", minval = 0.5, step = 0.1, group = grpD)
pivLen = input.int(3, "Swing confirmation bars", minval = 1, group = grpD, tooltip = "AR, ST, Test and LPSY are swing points, confirmed this many bars after they print.")
minRangeAtr = input.float(2.0, "Min range height (× ATR)", minval = 0.5, step = 0.1, group = grpD)
stZone = input.float(0.33, "ST/Test zone (fraction of range below resistance)", minval = 0.1, maxval = 0.9, step = 0.01, group = grpD)
stVolMax = input.float(0.8, "ST volume ≤ × BC volume", minval = 0.1, step = 0.05, group = grpD)
sowVol = input.float(1.5, "SOW volume (× avg)", minval = 1.0, step = 0.1, group = grpD)
sowTol = input.float(0.25, "SOW must close below the Ice by (× ATR)", minval = 0.0, step = 0.05, group = grpD)
grpI = "Invalidation"
breakTol = input.float(1.0, "Resistance break before ST (× ATR above BC)", minval = 0.1, step = 0.1, group = grpI)
utMax = input.float(1.5, "Max upthrust height (× ATR)", minval = 0.1, step = 0.1, group = grpI)
utBars = input.int(3, "Closes allowed above resistance before breakout", minval = 1, group = grpI)
lpsyTol = input.float(0.5, "LPSY may poke above the Ice (× ATR)", minval = 0.0, step = 0.1, group = grpI)
failTol = input.float(1.0, "Failed SOW: close back above the Ice (× ATR)", minval = 0.1, step = 0.1, group = grpI)
maxBars = input.int(300, "Abandon range after N bars", minval = 20, group = grpI)
onFail = input.string("Gray out", "Invalidated structures", options = ["Gray out", "Delete"], group = grpI)
grpV = "Display"
showTable = input.bool(true, "Show status table", group = grpV)
colDist = input.color(color.new(#c62828, 0), "Event label", group = grpV)
colBC = input.color(color.new(#6a1b9a, 0), "Climax label", group = grpV)
colRes = input.color(color.new(#ef6c00, 0), "Resistance line", group = grpV)
colIce = input.color(color.new(#00838f, 0), "Ice line", group = grpV)
// ───────── Core series ─────────
atr = ta.atr(atrLen)
vol = nz(volume)
avgVol = ta.sma(vol, volLen)
trendMA = ta.sma(close, trendLen)
spread = high - low
highestHi = ta.highest(high, bcLookback)
ph = ta.pivothigh(high, pivLen, pivLen)
pl = ta.pivotlow(low, pivLen, pivLen)
pivBar = bar_index - pivLen
pivVol = vol[pivLen]
// Climactic buying bar in an uptrend
isBC = high >= highestHi and close > trendMA and vol >= climaxVol * avgVol and spread >= climaxRng * atr
// Strong supply bar: wide spread, heavy volume, closes in the lower half
isSOW = vol >= sowVol * avgVol and spread >= atr and close < hl2
// ───────── State ─────────
// 0 scanning | 1 BC | 2 AR | 3 ST (Phase B) | 4 Upthrust (Phase C) | 5 SOW (Phase D) | 6 LPSY (Phase E)
var int state = 0
var float bcHigh = na
var float bcVol = na
var int bcBar = na
var float arLow = na
var int arBar = na
var float ice = na
var float utHigh = na
var float utVol = na
var int utBar = na
var bool testDone = false
var float penHigh = na
var float penVol = na
var int penBar = na
var int aboveCnt = 0
var int sowBar = na
var float sowVolume = na
var line resLine = na
var line iceLine = na
var label bcLabel = na
var label sowLabel = na
var array<label> cycLabels = array.new<label>()
var string lastEvent = "—"
// ───────── Helpers ─────────
mark(int x, float y, string txt, color col, bool below, string tip) =>
label.new(x, y, txt, style = below ? label.style_label_up : label.style_label_down, color = col, textcolor = color.white, size = size.small, tooltip = tip)
retire(array<label> labs, line l1, line l2, bool del) =>
for lb in labs
if del
label.delete(lb)
else
label.set_color(lb, color.new(color.gray, 40))
if not na(l1)
if del
line.delete(l1)
else
line.set_color(l1, color.new(color.gray, 50))
line.set_style(l1, line.style_dotted)
if not na(l2)
if del
line.delete(l2)
else
line.set_color(l2, color.new(color.gray, 50))
line.set_style(l2, line.style_dotted)
array.clear(labs)
// ───────── Per-bar flags ─────────
bool evBC = false
bool evAR = false
bool evST = false
bool evUT = false
bool evTest = false
bool evSOW = false
bool evSOWfail = false
bool evLPSY = false
bool evInv = false
bool invalidate = false
string invReason = ""
// ───────── State machine ─────────
if state >= 1 and state <= 5 and bar_index - bcBar > maxBars
invalidate := true
invReason := "Range abandoned: no resolution within " + str.tostring(maxBars) + " bars"
if not invalidate
if state == 1
// Phase A: BC printed, waiting for the Automatic Reaction
if isBC and high > bcHigh
// higher climax — re-anchor the BC
bcHigh := high
bcVol := vol
bcBar := bar_index
label.set_xy(bcLabel, bar_index, high)
evBC := true
else if high > bcHigh + breakTol * atr
invalidate := true
invReason := "New high without climactic volume: BC didn't hold"
else if not na(pl) and pivBar > bcBar and bcHigh - pl >= minRangeAtr * atr
arLow := pl
arBar := pivBar
ice := pl
state := 2
evAR := true
lastEvent := "AR"
array.push(cycLabels, mark(pivBar, pl, "AR", colDist, true, "Automatic Reaction: its low sets the bottom of the range (the Ice)"))
resLine := line.new(bcBar, bcHigh, bar_index, bcHigh, color = colRes, width = 2)
iceLine := line.new(arBar, ice, bar_index, ice, color = colIce, width = 2, style = line.style_dashed)
else if state == 2
// Phase A: range defined, waiting for the Secondary Test
height = bcHigh - arLow
if close > bcHigh + breakTol * atr
invalidate := true
invReason := "Closed above resistance before a Secondary Test"
else if not na(ph) and pivBar > arBar and ph >= bcHigh - stZone * height and ph <= bcHigh + utMax * atr and pivVol <= stVolMax * bcVol
state := 3
evST := true
lastEvent := "ST"
array.push(cycLabels, mark(pivBar, ph, "ST", colDist, false, "Secondary Test: revisits the BC area on lighter volume"))
else if state == 3 or state == 4
height = bcHigh - ice
// Phase C failure: closing above the upthrust high means it was a real breakout
if state == 4 and close > utHigh
invalidate := true
invReason := "Closed above the Upthrust high: breakout, not an upthrust"
// Phase B: watch for an Upthrust (brief push above resistance that is rejected)
if state == 3 and not invalidate
if high > bcHigh
if na(penHigh) or high > penHigh
if na(penHigh)
aboveCnt := 0
penHigh := high
penVol := vol
penBar := bar_index
if not na(penHigh)
if close > bcHigh
aboveCnt += 1
if penHigh > bcHigh + utMax * atr
invalidate := true
invReason := "Resistance taken out too far: breakout, not an upthrust"
else if aboveCnt > utBars
invalidate := true
invReason := "Held above resistance: breakout"
else if close <= bcHigh
utHigh := penHigh
utVol := penVol
utBar := penBar
penHigh := na
penVol := na
penBar := na
aboveCnt := 0
state := 4
evUT := true
lastEvent := "Upthrust"
array.push(cycLabels, mark(utBar, utHigh, "UT", colDist, false, "Upthrust: push above resistance that was quickly rejected (UTAD if late in the range)"))
// Phase C: low-volume test that holds below the upthrust high
if state == 4 and not invalidate and not testDone and not na(ph) and pivBar > utBar and ph <= utHigh and ph >= bcHigh - stZone * height and pivVol < utVol
testDone := true
evTest := true
lastEvent := "Test"
array.push(cycLabels, mark(pivBar, ph, "Test", colDist, false, "Test of the Upthrust: lower high on lighter volume"))
// Phase D: Sign of Weakness — break of the Ice (upthrust is optional in Wyckoff)
if not invalidate and (state == 3 or state == 4) and close < ice - sowTol * atr and isSOW
sowBar := bar_index
sowVolume := vol
state := 5
evSOW := true
lastEvent := "SOW"
sowLabel := mark(bar_index, low, na(utHigh) ? "SOW / Ice" : "SOW", colDist, true, "Sign of Weakness: wide-spread, high-volume close below the Ice" + (na(utHigh) ? " (no upthrust in this range)" : ""))
array.push(cycLabels, sowLabel)
else if state == 5
// Phase D: waiting for the Last Point of Supply
if close > ice + failTol * atr
evSOWfail := true
lastEvent := "SOW failed"
label.set_color(sowLabel, color.new(color.gray, 40))
label.set_text(sowLabel, "SOW ✕")
label.set_tooltip(sowLabel, "Breakdown failed: closed back inside the range")
sowBar := na
sowVolume := na
state := na(utHigh) ? 3 : 4
else if not na(ph) and pivBar > sowBar and ph <= ice + lpsyTol * atr and pivVol < sowVolume
state := 6
evLPSY := true
lastEvent := "LPSY"
array.push(cycLabels, mark(pivBar, ph, "LPSY", colDist, false, "Last Point of Supply: rally back to the Ice on lighter volume, old support now resistance"))
line.set_x2(resLine, bar_index)
line.set_x2(iceLine, bar_index)
// ───────── Invalidation ─────────
if invalidate
evInv := true
lastEvent := "Invalidated"
if onFail == "Gray out"
label.new(bar_index, high, "✕", style = label.style_label_down, color = color.new(color.gray, 20), textcolor = color.white, size = size.tiny, tooltip = invReason)
retire(cycLabels, resLine, iceLine, onFail == "Delete")
resLine := na
iceLine := na
state := 0
// ───────── New cycle: Buying Climax ─────────
if (state == 0 or state == 6) and isBC
array.clear(cycLabels) // a completed cycle stays on the chart as-is
resLine := na
iceLine := na
bcHigh := high
bcVol := vol
bcBar := bar_index
arLow := na
arBar := na
ice := na
utHigh := na
utVol := na
utBar := na
testDone := false
penHigh := na
penVol := na
penBar := na
aboveCnt := 0
sowBar := na
sowVolume := na
sowLabel := na
state := 1
evBC := true
lastEvent := "BC"
bcLabel := mark(bar_index, high, "BC", colBC, false, "Buying Climax: euphoric high on climactic volume and spread")
array.push(cycLabels, bcLabel)
// Keep the range lines extending while the structure is live
if state >= 2 and state <= 5
line.set_x2(resLine, bar_index)
line.set_x2(iceLine, bar_index)
// ───────── Status table ─────────
var table tbl = table.new(position.top_right, 2, 3, bgcolor = color.new(color.black, 75), border_width = 1)
if showTable and barstate.islast
phaseTxt = switch state
0 => "Scanning for a Buying Climax"
1 => "Phase A: BC printed, waiting for AR"
2 => "Phase A: range set, waiting for ST"
3 => "Phase B: building cause (watch Upthrust / SOW)"
4 => "Phase C: Upthrust printed, waiting for SOW"
5 => "Phase D: SOW, waiting for LPSY"
6 => "Phase E: markdown"
=> "—"
levels = state >= 2 ? str.tostring(bcHigh, format.mintick) + " / " + str.tostring(ice, format.mintick) : "—"
table.cell(tbl, 0, 0, "Phase", text_color = color.white, text_size = size.small, text_halign = text.align_left)
table.cell(tbl, 1, 0, phaseTxt, text_color = color.white, text_size = size.small, text_halign = text.align_left)
table.cell(tbl, 0, 1, "Last event", text_color = color.white, text_size = size.small, text_halign = text.align_left)
table.cell(tbl, 1, 1, lastEvent, text_color = color.white, text_size = size.small, text_halign = text.align_left)
table.cell(tbl, 0, 2, "Resistance / Ice", text_color = color.white, text_size = size.small, text_halign = text.align_left)
table.cell(tbl, 1, 2, levels, text_color = color.white, text_size = size.small, text_halign = text.align_left)
// ───────── Alerts (set to "Once Per Bar Close") ─────────
alertcondition(evBC, "Buying Climax", "Wyckoff: Buying Climax on {{ticker}}")
alertcondition(evAR, "Automatic Reaction", "Wyckoff: Automatic Reaction confirmed on {{ticker}}")
alertcondition(evST, "Secondary Test", "Wyckoff: Secondary Test (distribution) confirmed on {{ticker}}")
alertcondition(evUT, "Upthrust", "Wyckoff: Upthrust on {{ticker}}")
alertcondition(evTest, "Test of Upthrust", "Wyckoff: Test of the Upthrust on {{ticker}}")
alertcondition(evSOW, "Sign of Weakness", "Wyckoff: Sign of Weakness — Ice broken on {{ticker}}")
alertcondition(evSOWfail, "SOW Failed", "Wyckoff: SOW failed, back in range on {{ticker}}")
alertcondition(evLPSY, "LPSY", "Wyckoff: Last Point of Supply on {{ticker}}")
alertcondition(evInv, "Structure Invalidated", "Wyckoff: distribution structure invalidated on {{ticker}}")