本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Volume Impact Efficiency Lens [Pineify]", shorttitle="Impact Lens [Pineify]", overlay=false, max_bars_back=500, precision=2)
// Independent implementation. All baselines precede the evaluated displacement window.
int windowBars = input.int(12, "Impact window", minval=3, maxval=60, group="Measurement")
int baselineBars = input.int(60, "Prior volume baseline", minval=10, maxval=200, group="Measurement")
int atrBars = input.int(14, "ATR length", minval=2, maxval=100, group="Measurement")
float volumeCap = input.float(4.0, "Per-bar relative volume cap", minval=2.0, maxval=10.0, step=0.25, group="Measurement")
int memoryBars = input.int(8, "Finite occupancy memory", minval=3, maxval=30, group="State")
int confirmBars = input.int(2, "Consecutive candidate closes", minval=1, maxval=5, group="State")
float enterShare = input.float(0.65, "Entry occupancy", minval=0.55, maxval=0.95, step=0.05, group="State")
float exitShare = input.float(0.40, "Hold occupancy", minval=0.10, maxval=0.50, step=0.05, group="State")
float driveScore = input.float(12.0, "Advance score threshold", minval=5.0, maxval=40.0, step=1.0, group="State")
float driveEfficiency = input.float(0.45, "Advance path efficiency", minval=0.30, maxval=0.90, step=0.05, group="State")
float highVolume = input.float(1.60, "High participation ratio", minval=1.10, maxval=2.0, step=0.10, group="State")
float absorbPace = input.float(0.12, "Absorption maximum net ATR/bar", minval=0.02, maxval=0.30, step=0.01, group="State")
float frictionEfficiency = input.float(0.25, "Friction maximum path efficiency", minval=0.05, maxval=0.25, step=0.05, group="State")
float frictionTravel = input.float(0.25, "Friction minimum path ATR/bar", minval=0.05, maxval=1.0, step=0.05, group="State")
bool showHalo = input.bool(true, "Participation halo", group="Visuals")
bool showMemory = input.bool(false, "Directional occupancy columns", group="Visuals")
bool showBackground = input.bool(false, "Confirmed state background", group="Visuals")
bool showMarkers = input.bool(true, "State transition markers", group="Visuals")
bool showTable = input.bool(true, "Summary table", group="Visuals")
f_measure(int n, int b, float cap, float scale) =>
array<float> priorVolumes = array.new<float>()
bool valid = bar_index >= n + b - 1 and not na(scale) and scale > 0 and not na(close[n])
for i = n to n + b - 1
float v = volume[i]
if na(v) or v <= 0
valid := false
else
array.push(priorVolumes, v)
float base = array.size(priorVolumes) == b ? array.median(priorVolumes) : na
float path = 0.0
float participation = 0.0
for i = 0 to n - 1
bool priceOk = not na(open[i]) and not na(high[i]) and not na(low[i]) and not na(close[i]) and not na(close[i + 1])
priceOk := priceOk and high[i] >= math.max(open[i], close[i]) and low[i] <= math.min(open[i], close[i])
bool volumeOk = not na(volume[i]) and volume[i] > 0 and not na(base) and base > 0
if not priceOk or not volumeOk
valid := false
if priceOk
path += math.abs(close[i] - close[i + 1])
if volumeOk
participation += math.min(cap, volume[i] / base) / n
float net = close - close[n]
float efficiency = valid and path > 0 ? math.min(1.0, math.abs(net) / path) : 0.0
float pace = valid ? net / (n * scale) : na
float travel = valid ? path / (n * scale) : na
float r = valid ? participation : na
float impulse = valid ? pace * efficiency * math.min(1.0, r) / math.max(1.0, r) : na
float score = valid ? 100.0 * impulse / (1.0 + math.abs(impulse)) : na
[valid, score, r, efficiency, pace, travel]
f_state(int s) =>
switch s
1 => "Advance up"
-1 => "Advance down"
2 => "Friction"
3 => "Absorption proxy"
4 => "Decaying"
=> "Neutral"
f_number(float x) =>
na(x) ? "n/a" : str.tostring(x, "#.##")
float atrSeries = ta.atr(atrBars)
[validNow, scoreNow, ratioNow, efficiencyNow, paceNow, travelNow] = f_measure(windowBars, baselineBars, volumeCap, atrSeries[windowBars])
// Exclusive window votes: high-volume low-net behavior has precedence over friction.
var array<int> votes = array.new<int>()
var int state = 0
var int candidate = 0
var int candidateAge = 0
var bool coverage = false
var bool sampleValid = false
var int memoryCount = 0
var float score = na
var float ratio = na
var float efficiency = na
var float pace = na
var float travel = na
var float upShare = na
var float downShare = na
var float absorptionShare = na
var float frictionShare = na
bool changed = false
bool coverageLost = false
bool coverageRestored = false
bool fadedToNeutral = false
if barstate.isconfirmed
int previousState = state
bool previousCoverage = coverage
sampleValid := validNow
if not validNow
array.clear(votes)
state := 0
candidate := 0
candidateAge := 0
coverage := false
score := na
ratio := na
efficiency := na
pace := na
travel := na
upShare := na
downShare := na
absorptionShare := na
frictionShare := na
else
score := scoreNow
ratio := ratioNow
efficiency := efficiencyNow
pace := paceNow
travel := travelNow
bool absorb = ratio >= highVolume and math.abs(pace) <= absorbPace
bool advance = math.abs(score) >= driveScore and efficiency >= driveEfficiency
bool friction = efficiency <= frictionEfficiency and travel >= frictionTravel
int vote = absorb ? 3 : advance ? (score > 0 ? 1 : -1) : friction ? 2 : 0
array.push(votes, vote)
if array.size(votes) > memoryBars
array.shift(votes)
int ups = 0
int downs = 0
int absorptions = 0
int frictions = 0
for v in votes
ups += v == 1 ? 1 : 0
downs += v == -1 ? 1 : 0
absorptions += v == 3 ? 1 : 0
frictions += v == 2 ? 1 : 0
int count = array.size(votes)
upShare := float(ups) / count
downShare := float(downs) / count
absorptionShare := float(absorptions) / count
frictionShare := float(frictions) / count
coverage := count == memoryBars
int target = 0
if coverage
if vote == 3 and absorptionShare >= (state == 3 ? exitShare : enterShare)
target := 3
else if vote == 1 and upShare >= (state == 1 ? exitShare : enterShare)
target := 1
else if vote == -1 and downShare >= (state == -1 ? exitShare : enterShare)
target := -1
else if vote == 2 and frictionShare >= (state == 2 ? exitShare : enterShare)
target := 2
else if (math.abs(state) == 1 or state == 4) and math.abs(score) > driveScore * 0.35
target := 4
if target == state
candidate := state
candidateAge := 0
else
candidateAge := target == candidate ? candidateAge + 1 : 1
candidate := target
if candidateAge >= confirmBars
state := target
candidateAge := 0
else
state := 0
candidate := 0
candidateAge := 0
memoryCount := array.size(votes)
changed := coverage and state != previousState
coverageLost := previousCoverage and not coverage
coverageRestored := not previousCoverage and coverage
fadedToNeutral := changed and state == 0 and (math.abs(previousState) == 1 or previousState == 4)
color upColor = color.rgb(37, 99, 235)
color downColor = color.rgb(210, 73, 62)
color frictionColor = color.rgb(177, 119, 8)
color absorptionColor = color.rgb(132, 73, 200)
color stateColor = state == 1 ? upColor : state == -1 ? downColor : state == 2 ? frictionColor : state == 3 ? absorptionColor : color.new(chart.fg_color, state == 4 ? 25 : 45)
float haloWidth = not na(ratio) ? 3.0 + 10.0 * ratio / volumeCap : na
pUpper = plot(showHalo ? score + haloWidth : na, "Participation upper", color=na, style=plot.style_linebr)
pLower = plot(showHalo ? score - haloWidth : na, "Participation lower", color=na, style=plot.style_linebr)
fill(pUpper, pLower, color=color.new(stateColor, 78), title="Capped participation width", fillgaps=false)
plot(showMemory and coverage ? 25.0 * (upShare - downShare) : na, "Directional occupancy x25", color=color.new(upShare >= downShare ? upColor : downColor, 75), style=plot.style_columns)
plot(score, "Impact efficiency", color=stateColor, linewidth=3, style=plot.style_linebr)
hline(0, "Zero", color=color.new(chart.fg_color, 60))
hline(100, "Bound +100", color=color.new(chart.fg_color, 90), linestyle=hline.style_dotted)
hline(-100, "Bound -100", color=color.new(chart.fg_color, 90), linestyle=hline.style_dotted)
plot(driveScore, "Advance upper threshold", color=color.new(upColor, 72), linewidth=1)
plot(-driveScore, "Advance lower threshold", color=color.new(downColor, 72), linewidth=1)
bgcolor(showBackground and coverage and state != 0 ? color.new(stateColor, 94) : na, title="Confirmed state")
plotshape(showMarkers and changed and state == 1 ? score : na, "Advance up event", shape.triangleup, location.absolute, upColor, text="U", textcolor=upColor, size=size.tiny)
plotshape(showMarkers and changed and state == -1 ? score : na, "Advance down event", shape.triangledown, location.absolute, downColor, text="D", textcolor=downColor, size=size.tiny)
plotshape(showMarkers and changed and state == 3 ? score : na, "Absorption event", shape.diamond, location.absolute, absorptionColor, text="A", textcolor=absorptionColor, size=size.tiny)
plotshape(showMarkers and changed and state == 2 ? score : na, "Friction event", shape.square, location.absolute, frictionColor, text="F", textcolor=frictionColor, size=size.tiny)
plotshape(showMarkers and changed and state == 4 ? score : na, "Decay event", shape.xcross, location.absolute, chart.fg_color, size=size.tiny)
plot(ratio, "Capped relative volume", display=display.data_window)
plot(efficiency, "Absolute path efficiency", display=display.data_window)
plot(pace, "Signed net ATR per bar", display=display.data_window)
plot(travel, "Gross path ATR per bar", display=display.data_window)
plot(coverage ? absorptionShare : na, "Absorption occupancy", display=display.data_window)
plot(coverage ? frictionShare : na, "Friction occupancy", display=display.data_window)
var table dashboard = table.new(position.top_right, 2, 8, bgcolor=color.new(chart.bg_color, 5), frame_color=color.new(chart.fg_color, 65), frame_width=1)
if barstate.islast and showTable
table.cell(dashboard, 0, 0, "Impact lens", text_color=chart.fg_color)
table.cell(dashboard, 1, 0, coverage ? f_state(state) : sampleValid ? "Memory warming" : "Data unavailable", text_color=stateColor)
table.cell(dashboard, 0, 1, "Impact [-100,100]", text_color=chart.fg_color)
table.cell(dashboard, 1, 1, f_number(score), text_color=stateColor)
table.cell(dashboard, 0, 2, "Capped volume", text_color=chart.fg_color)
table.cell(dashboard, 1, 2, f_number(ratio) + "x", text_color=chart.fg_color)
table.cell(dashboard, 0, 3, "Path efficiency", text_color=chart.fg_color)
table.cell(dashboard, 1, 3, f_number(efficiency), text_color=chart.fg_color)
table.cell(dashboard, 0, 4, "Net / gross ATR-bar", text_color=chart.fg_color)
table.cell(dashboard, 1, 4, f_number(pace) + " / " + f_number(travel), text_color=chart.fg_color)
table.cell(dashboard, 0, 5, "Up / down occupancy", text_color=chart.fg_color)
table.cell(dashboard, 1, 5, f_number(upShare) + " / " + f_number(downShare), text_color=chart.fg_color)
table.cell(dashboard, 0, 6, "Absorb / friction", text_color=chart.fg_color)
table.cell(dashboard, 1, 6, f_number(absorptionShare) + " / " + f_number(frictionShare), text_color=chart.fg_color)
table.cell(dashboard, 0, 7, "Memory / candidate", text_color=chart.fg_color)
table.cell(dashboard, 1, 7, str.tostring(memoryCount) + "/" + str.tostring(memoryBars) + " | " + f_state(candidate) + ":" + str.tostring(candidateAge), text_color=chart.fg_color)
alertcondition(changed and state == 1, "Confirmed advance up", "Impact lens: confirmed efficient upward advance.")
alertcondition(changed and state == -1, "Confirmed advance down", "Impact lens: confirmed efficient downward advance.")
alertcondition(changed and state == 3, "Confirmed absorption proxy", "Impact lens: persistent high participation with low net displacement; absorption proxy only.")
alertcondition(changed and state == 2, "Confirmed friction", "Impact lens: persistent path cancellation with material gross travel.")
alertcondition(changed and state == 4, "Confirmed efficiency decay", "Impact lens: prior directional advance has lost occupancy support.")
alertcondition(fadedToNeutral, "Advance faded to neutral", "Impact lens: prior advance or decay has returned to neutral.")
alertcondition(coverageLost, "Coverage lost", "Impact lens: required price, volume, or scale data unavailable; memory cleared.")
alertcondition(coverageRestored, "Coverage restored", "Impact lens: complete valid finite memory available.")