本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, obtain one at https://mozilla.org/MPL/2.0/.
// © EVLabs
//@version=6
indicator("EV Footprint Response", "EV FPR", overlay=true, max_boxes_count=100, max_labels_count=100)
// Open-source release 1.0. Footprint volume is classified from intrabar price action,
// not an exchange aggressor-side tape. Signals are evaluated only at bar close.
string GF = "01 · Footprint"
int ticks = input.int(100, "Ticks per row", minval=1, group=GF, tooltip="Row height = ticks × minimum tick. Adjust for each symbol and timeframe; too few rows can suppress stacked imbalances.")
int imbalance = input.int(300, "Diagonal imbalance (%)", minval=100, step=50, group=GF)
int minStack = input.int(2, "Consecutive imbalanced rows", minval=1, maxval=10, group=GF)
float minDelta = input.float(15, "Minimum absolute delta (%)", minval=0, maxval=100, step=1, group=GF)
int volLength = input.int(20, "Prior footprint volume average", minval=2, maxval=200, group=GF)
float minRelative = input.float(1.2, "Minimum relative volume", minval=0.1, step=0.1, group=GF)
string GM = "02 · Response model"
int atrLength = input.int(14, "ATR length", minval=2, group=GM)
float maxBody = input.float(0.5, "Maximum body / range", minval=0, maxval=1, step=0.05, group=GM)
float minRejection = input.float(0.5, "Minimum rejection from extreme", minval=0.1, maxval=0.9, step=0.05, group=GM, tooltip="For a bullish candidate: (close-low)/range. For bearish: (high-close)/range.")
float minRange = input.float(0.25, "Minimum range / prior ATR", minval=0, step=0.05, group=GM)
int life = input.int(5, "Confirmation window (bars)", minval=1, maxval=30, group=GM)
float confirmATR = input.float(0.05, "Confirmation buffer / frozen ATR", minval=0, step=0.01, group=GM)
float invalidATR = input.float(0.05, "Invalidation buffer / frozen ATR", minval=0, step=0.01, group=GM)
int cooldown = input.int(3, "Bars after a resolved candidate", minval=0, maxval=100, group=GM)
string GV = "03 · Appearance"
color bull = input.color(#0f9488, "Bullish response", group=GV)
color bear = input.color(#e1556f, "Bearish response", group=GV)
color pending = input.color(#cd9539, "Pending candidate", group=GV)
bool showCandidates = input.bool(true, "Show candidate zones", group=GV)
bool showFailed = input.bool(false, "Keep failed / expired zones", group=GV)
bool showPOC = input.bool(false, "Show each bar's footprint POC", group=GV)
bool showPanel = input.bool(true, "Show compact panel", group=GV)
int history = input.int(12, "Zones to retain", minval=1, maxval=50, group=GV)
string panelPlace = input.string("Top Right", "Panel position", options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group=GV)
// One footprint request. No synthetic OHLCV fallback when unavailable.
footprint fp = request.footprint(ticks, 70, imbalance)
bool hasData = not na(fp)
float fpVolume = hasData ? fp.total_volume() : na
float delta = hasData ? fp.delta() : na
float deltaPct = hasData and fpVolume > 0 ? 100 * delta / fpVolume : na
float priorAverage = ta.sma(fpVolume, volLength)[1]
float relativeVolume = not na(priorAverage) and priorAverage > 0 ? fpVolume / priorAverage : na
float priorATR = ta.atr(atrLength)[1]
float pocPrice = na
int maxBuy = 0
int maxSell = 0
if hasData
volume_row poc = fp.poc()
if not na(poc)
pocPrice := (poc.up_price() + poc.down_price()) / 2
int buyRun = 0
int sellRun = 0
float previousUpper = na
for row in fp.rows()
bool adjacent = na(previousUpper) or math.abs(row.down_price() - previousUpper) <= syminfo.mintick * 0.5
buyRun := row.has_buy_imbalance() ? (adjacent ? buyRun + 1 : 1) : 0
sellRun := row.has_sell_imbalance() ? (adjacent ? sellRun + 1 : 1) : 0
maxBuy := math.max(maxBuy, buyRun)
maxSell := math.max(maxSell, sellRun)
previousUpper := row.up_price()
float candleRange = high - low
bool ready = hasData and not na(relativeVolume) and not na(priorATR) and priorATR > 0 and candleRange > 0
bool effort = ready and relativeVolume >= minRelative and candleRange >= priorATR * minRange and math.abs(close - open) / candleRange <= maxBody
// Negative classified delta + sell stack + rejection of the low = bullish candidate.
// The inverse is bearish. Neither candidate is labelled confirmed absorption.
bool bullCandidate = effort and deltaPct <= -minDelta and delta < 0 and maxSell >= minStack and (close - low) / candleRange >= minRejection
bool bearCandidate = effort and deltaPct >= minDelta and delta > 0 and maxBuy >= minStack and (high - close) / candleRange >= minRejection
var int direction = 0
var int startBar = na
var int lastResolved = na
var float trigger = na
var float invalidation = na
var box activeZone = na
var array<box> zones = array.new<box>()
var string lastEvent = "Waiting for candidate"
var int candidates = 0
var int confirmed = 0
var int invalidated = 0
var int expired = 0
var int missing = 0
bool bullSignal = false
bool bearSignal = false
bool newBull = false
bool newBear = false
if barstate.isconfirmed
if not hasData
missing += 1
bool resolvedNow = false
if direction != 0
int age = bar_index - startBar
if not na(activeZone)
box.set_right(activeZone, bar_index)
// Invalidation has precedence if a bar both breaches invalidation and closes
// beyond the trigger. No unknown intrabar execution sequence is assumed.
bool broken = direction == 1 ? low < invalidation : high > invalidation
bool crossed = direction == 1 ? close > trigger : close < trigger
bool timeout = age > life
bool success = not broken and not timeout and age >= 1 and hasData and crossed
if broken or timeout or success
if success
bullSignal := direction == 1
bearSignal := direction == -1
confirmed += 1
lastEvent := direction == 1 ? "Bullish response confirmed" : "Bearish response confirmed"
else if broken
invalidated += 1
lastEvent := "Candidate invalidated"
else
expired += 1
lastEvent := "Candidate expired"
if not na(activeZone)
color resolvedColor = success ? (direction == 1 ? bull : bear) : color.gray
box.set_border_color(activeZone, color.new(resolvedColor, 45))
box.set_bgcolor(activeZone, color.new(resolvedColor, 93))
box.set_text_color(activeZone, resolvedColor)
box.set_text(activeZone, success ? "RESPONSE ✓" : broken ? "INVALID" : "EXPIRED")
if not success and not showFailed
// The last retained box is always the current candidate.
box.delete(array.pop(zones))
activeZone := na
direction := 0
lastResolved := bar_index
resolvedNow := true
bool cooled = na(lastResolved) or bar_index - lastResolved > cooldown
if direction == 0 and not resolvedNow and cooled and (bullCandidate or bearCandidate)
direction := bullCandidate ? 1 : -1
startBar := bar_index
trigger := direction == 1 ? high + priorATR * confirmATR : low - priorATR * confirmATR
invalidation := direction == 1 ? low - priorATR * invalidATR : high + priorATR * invalidATR
candidates += 1
newBull := direction == 1
newBear := direction == -1
lastEvent := direction == 1 ? "Bullish candidate · pending" : "Bearish candidate · pending"
if showCandidates
activeZone := box.new(bar_index, high, bar_index + 1, low, border_color=color.new(pending, 45), bgcolor=color.new(pending, 93), text="PENDING", text_color=pending, text_size=size.tiny)
array.push(zones, activeZone)
if array.size(zones) > history
box.delete(array.shift(zones))
// Markers appear on the actual confirmation bar, never backdated to the candidate.
plotshape(bullSignal, "Bullish response", shape.triangleup, location.belowbar, bull, size=size.small, text="R", textcolor=bull)
plotshape(bearSignal, "Bearish response", shape.triangledown, location.abovebar, bear, size=size.small, text="R", textcolor=bear)
plot(showPOC and barstate.isconfirmed ? pocPrice : na, "Bar footprint POC", color.new(#4b77be, 40), style=plot.style_circles)
plot(direction != 0 ? trigger : na, "Pending confirmation level", color.new(pending, 15), style=plot.style_linebr, display=display.pane + display.data_window)
plot(direction != 0 ? invalidation : na, "Pending invalidation", color.new(color.gray, 55), style=plot.style_linebr, display=display.pane + display.data_window)
plot(deltaPct, "Classified delta (%)", display=display.data_window)
plot(relativeVolume, "Relative footprint volume", display=display.data_window)
plot(maxBuy, "Longest buy imbalance stack", display=display.data_window)
plot(maxSell, "Longest sell imbalance stack", display=display.data_window)
string pos = switch panelPlace
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
"Bottom Left" => position.bottom_left
=> position.top_right
var table panel = table.new(pos, 2, 9, bgcolor=color.new(chart.bg_color, 8), border_width=0)
f_cell(int r, string name, string value, color accent) =>
table.cell(panel, 0, r, name, text_color=color.new(chart.fg_color, 30), text_size=size.small, text_halign=text.align_left)
table.cell(panel, 1, r, value, text_color=accent, text_size=size.small, text_halign=text.align_right)
if barstate.islast and showPanel
f_cell(0, "EV FOOTPRINT", "RESPONSE · 1.0", #4b77be)
f_cell(1, "Data", not hasData ? "Unavailable" : barstate.isconfirmed ? "Closed bar" : "Live · provisional", hasData ? chart.fg_color : pending)
f_cell(2, "Delta / RVOL", str.tostring(deltaPct, "#.0") + "% / " + str.tostring(relativeVolume, "#.00") + "×", chart.fg_color)
f_cell(3, "Stacks B / S", str.tostring(maxBuy) + " / " + str.tostring(maxSell), chart.fg_color)
f_cell(4, "State", direction == 0 ? lastEvent : direction == 1 ? "Bull candidate" : "Bear candidate", direction == 0 ? chart.fg_color : pending)
f_cell(5, "Confirm / Invalidate", direction == 0 ? "—" : str.tostring(trigger, format.mintick) + " / " + str.tostring(invalidation, format.mintick), pending)
f_cell(6, "Candidates / Responses", str.tostring(candidates) + " / " + str.tostring(confirmed), chart.fg_color)
f_cell(7, "Invalid / Expired", str.tostring(invalidated) + " / " + str.tostring(expired), chart.fg_color)
f_cell(8, "No-data bars", str.tostring(missing), color.new(chart.fg_color, 30))
alertcondition(bullSignal, "Bullish response confirmed", "EV Footprint Response: bullish response confirmed on {{ticker}} {{interval}} at {{close}}.")
alertcondition(bearSignal, "Bearish response confirmed", "EV Footprint Response: bearish response confirmed on {{ticker}} {{interval}} at {{close}}.")
alertcondition(newBull or newBear, "New footprint candidate", "EV Footprint Response: new candidate, awaiting a later closing confirmation on {{ticker}} {{interval}}.")