本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Directional Volume Absorption Ladder [Pineify]", shorttitle="Volume Absorption Ladder", overlay=true, max_bars_back=600)
// The event reference is fixed at a confirmed directional volume shock. All
// subsequent measures start at zero; the shock itself never votes for a state.
int baseLen = input.int(40, "Prior volume samples", minval=10, maxval=150, group="Shock")
int atrLen = input.int(14, "ATR length", minval=2, maxval=100, group="Shock")
float surprise = input.float(2.0, "Volume surprise in robust scales", minval=0.5, maxval=10.0, step=0.1, group="Shock")
float minMultiple = input.float(1.5, "Minimum volume / prior median", minval=1.0, maxval=10.0, step=0.1, group="Shock")
float minBody = input.float(0.3, "Minimum body / prior ATR", minval=0.05, maxval=3.0, step=0.05, group="Shock")
int window = input.int(16, "Maximum post-shock bars", minval=4, maxval=100, group="Aftermath")
int confirm = input.int(2, "Consecutive state confirmations", minval=1, maxval=5, group="Aftermath")
float breach = input.float(0.5, "Adverse breach / frozen ATR", minval=0.1, maxval=5.0, step=0.1, group="Aftermath")
float absorbResidence = input.float(0.60, "Absorption minimum residence", minval=0.3, maxval=0.95, step=0.05, group="Aftermath")
float absorbImpact = input.float(0.12, "Absorption maximum impact per volume unit", minval=0.01, maxval=1.0, step=0.01, group="Aftermath")
float releaseMove = input.float(0.5, "Release minimum move / ATR", minval=0.1, maxval=5.0, step=0.1, group="Aftermath")
float releaseEfficiency = input.float(0.4, "Release minimum path efficiency", minval=0.05, maxval=0.95, step=0.05, group="Aftermath")
bool showFrontier = input.bool(true, "Show response frontier", group="Display")
bool showMarkers = input.bool(true, "Show state markers", group="Display")
bool showTable = input.bool(true, "Show current ledger", group="Display")
float tfSeconds = timeframe.in_seconds()
bool supportedTf = not na(tfSeconds) and tfSeconds >= 300 and tfSeconds <= 86400
bool priceOK = not na(open) and not na(high) and not na(low) and not na(close) and high >= low and high >= math.max(open, close) and low <= math.min(open, close)
bool barOK = priceOK and not na(volume) and volume > 0
float atrSeries = ta.atr(atrLen)
float priorAtr = atrSeries[1]
// A complete, strictly positive preceding volume sample prevents silent gaps.
array<float> sample = array.new_float()
bool sampleOK = bar_index >= baseLen
if sampleOK
for i = 1 to baseLen
float v = volume[i]
if na(v) or v <= 0
sampleOK := false
else
array.push(sample, v)
float medianVol = sampleOK ? array.median(sample) : na
array<float> deviations = array.new_float()
if sampleOK
for i = 0 to array.size(sample) - 1
array.push(deviations, math.abs(array.get(sample, i) - medianVol))
float mad = sampleOK ? array.median(deviations) : na
// A 10% median floor gives tied or nearly tied volume samples a finite scale.
float scale = sampleOK ? math.max(1.4826 * mad, 0.1 * medianVol) : na
bool baselineOK = sampleOK and not na(medianVol) and medianVol > 0 and not na(priorAtr) and priorAtr > 0
bool shock = supportedTf and barOK and baselineOK and volume >= medianVol + surprise * scale and volume / medianVol >= minMultiple and math.abs(close - open) / priorAtr >= minBody and close != open
// state: 0 idle, 1 shock observation, 2 absorption proxy, 3 release.
var int state = 0
var int direction = 0
var float eventHigh = na
var float eventLow = na
var float eventClose = na
var float eventAtr = na
var float eventMedian = na
var int age = 0
var int insideCount = 0
var float totalVol = 0.0
var float path = 0.0
var float worstAdverse = 0.0
var float residence = na
var float forward = na
var float efficiency = na
var float impact = na
var int candidate = 0
var int candidateCount = 0
bool newShock = false
bool newAbsorption = false
bool newRelease = false
bool invalidated = false
if barstate.isconfirmed
if state == 0
if shock
state := 1
direction := close > open ? 1 : -1
eventHigh := high
eventLow := low
eventClose := close
eventAtr := priorAtr
eventMedian := medianVol
age := 0
insideCount := 0
totalVol := 0.0
path := 0.0
worstAdverse := 0.0
residence := na
forward := na
efficiency := na
impact := na
candidate := 0
candidateCount := 0
newShock := true
else
// An incomplete observation terminates the event, never compresses time.
if not supportedTf or not barOK or na(close[1])
state := 0
candidate := 0
candidateCount := 0
invalidated := true
else
age += 1
if age > window
state := 0
candidate := 0
candidateCount := 0
invalidated := true
else
insideCount += close >= eventLow and close <= eventHigh ? 1 : 0
totalVol += volume
path += math.abs(close - close[1])
float adverseNow = direction == 1 ? math.max(0.0, eventLow - low) : math.max(0.0, high - eventHigh)
worstAdverse := math.max(worstAdverse, adverseNow / eventAtr)
residence := insideCount / float(age)
float signedMove = direction * (close - eventClose)
forward := math.max(0.0, signedMove / eventAtr)
efficiency := path > 0 ? math.max(0.0, math.min(1.0, signedMove / path)) : 0.0
float volumeUnits = totalVol / eventMedian
impact := volumeUnits > 0 ? forward / volumeUnits : na
if worstAdverse > breach
state := 0
candidate := 0
candidateCount := 0
invalidated := true
else
float boundary = direction == 1 ? eventHigh : eventLow
bool outside = direction * (close - boundary) > 0
bool releaseEvidence = age >= 2 and outside and forward >= releaseMove and efficiency >= releaseEfficiency and impact >= absorbImpact
bool absorbEvidence = age >= 2 and residence >= absorbResidence and forward <= releaseMove * 0.5 and impact <= absorbImpact
// Softer retention limits reduce state chatter without forecasting.
bool retainRelease = state == 3 and forward >= releaseMove * 0.6 and direction * (close - boundary) >= -0.2 * eventAtr and efficiency >= releaseEfficiency * 0.6
bool retainAbsorb = state == 2 and residence >= absorbResidence * 0.75 and forward <= releaseMove and impact <= absorbImpact * 1.5
int desired = releaseEvidence ? 3 : absorbEvidence ? 2 : retainRelease ? 3 : retainAbsorb ? 2 : 1
if desired == state
candidate := 0
candidateCount := 0
else
if candidate == desired
candidateCount += 1
else
candidate := desired
candidateCount := 1
if candidateCount >= confirm
state := desired
newAbsorption := state == 2
newRelease := state == 3
candidate := 0
candidateCount := 0
color waitingColor = color.rgb(83, 124, 176)
color absorbingColor = color.rgb(218, 145, 28)
color upColor = color.rgb(15, 150, 110)
color downColor = color.rgb(207, 69, 91)
color stateColor = state == 2 ? absorbingColor : state == 3 ? (direction == 1 ? upColor : downColor) : waitingColor
int opacity = state == 2 and not na(residence) ? int(math.round(91 - 35 * residence)) : 88
float top = state != 0 ? eventHigh : na
float bottom = state != 0 ? eventLow : na
pTop = plot(top, "Shock high", color=state != 0 ? stateColor : na, linewidth=2, style=plot.style_linebr)
pBottom = plot(bottom, "Shock low", color=state != 0 ? stateColor : na, linewidth=2, style=plot.style_linebr)
fill(pTop, pBottom, color=state != 0 ? color.new(stateColor, opacity) : na, title="Event zone / absorption residence", fillgaps=false)
float frontier = state != 0 and not na(forward) ? eventClose + direction * forward * eventAtr : na
plot(showFrontier ? frontier : na, "Directional response frontier", color=state != 0 ? stateColor : na, linewidth=3, style=plot.style_linebr)
plotshape(showMarkers and newShock, title="Confirmed volume shock", style=shape.diamond, location=location.belowbar, color=waitingColor, size=size.tiny, text="S", textcolor=chart.fg_color)
plotshape(showMarkers and newAbsorption, title="Confirmed absorption proxy", style=shape.circle, location=location.belowbar, color=absorbingColor, size=size.tiny, text="A", textcolor=chart.fg_color)
plotshape(showMarkers and newRelease, title="Confirmed directional release", style=shape.triangleup, location=location.belowbar, color=direction == 1 ? upColor : downColor, size=size.tiny, text="R", textcolor=chart.fg_color)
plotshape(showMarkers and invalidated, title="Event invalidated or expired", style=shape.xcross, location=location.abovebar, color=color.gray, size=size.tiny, text="X", textcolor=chart.fg_color)
var table ledger = table.new(position.top_right, 2, 7, border_width=1)
if barstate.islast
table.clear(ledger, 0, 0, 1, 6)
if showTable
string label = not supportedTf ? "Use 5m–1D" : state == 0 ? "No active event" : state == 1 ? "Shock / observing" : state == 2 ? "Absorption proxy" : "Directional release"
color bg = color.new(state == 0 ? waitingColor : stateColor, 70)
table.cell(ledger, 0, 0, "Volume ladder", text_color=chart.fg_color, bgcolor=bg)
table.cell(ledger, 1, 0, label, text_color=chart.fg_color, bgcolor=bg)
table.cell(ledger, 0, 1, "Direction / age", text_color=chart.fg_color, bgcolor=chart.bg_color)
table.cell(ledger, 1, 1, state == 0 ? "—" : (direction == 1 ? "Up / " : "Down / ") + str.tostring(age) + "/" + str.tostring(window), text_color=chart.fg_color, bgcolor=chart.bg_color)
table.cell(ledger, 0, 2, "Inside closes", text_color=chart.fg_color, bgcolor=chart.bg_color)
table.cell(ledger, 1, 2, state == 0 or na(residence) ? "—" : str.tostring(residence * 100, "#.0") + "%", text_color=chart.fg_color, bgcolor=chart.bg_color)
table.cell(ledger, 0, 3, "Forward / ATR", text_color=chart.fg_color, bgcolor=chart.bg_color)
table.cell(ledger, 1, 3, state == 0 or na(forward) ? "—" : str.tostring(forward, "#.##"), text_color=chart.fg_color, bgcolor=chart.bg_color)
table.cell(ledger, 0, 4, "Path efficiency", text_color=chart.fg_color, bgcolor=chart.bg_color)
table.cell(ledger, 1, 4, state == 0 or na(efficiency) ? "—" : str.tostring(efficiency, "#.##"), text_color=chart.fg_color, bgcolor=chart.bg_color)
table.cell(ledger, 0, 5, "Move / volume unit", text_color=chart.fg_color, bgcolor=chart.bg_color)
table.cell(ledger, 1, 5, state == 0 or na(impact) ? "—" : str.tostring(impact, "#.###"), text_color=chart.fg_color, bgcolor=chart.bg_color)
table.cell(ledger, 0, 6, "Worst breach / ATR", text_color=chart.fg_color, bgcolor=chart.bg_color)
table.cell(ledger, 1, 6, state == 0 ? "—" : str.tostring(worstAdverse, "#.##"), text_color=chart.fg_color, bgcolor=chart.bg_color)
alertcondition(newShock, "Volume shock", "A directional volume shock was confirmed at bar close.")
alertcondition(newAbsorption, "Absorption proxy", "The event entered the confirmed absorption proxy state.")
alertcondition(newRelease, "Directional release", "The event entered the confirmed directional release state.")
alertcondition(invalidated, "Event ended", "The event expired, breached the adverse limit, or lost valid data.")