本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("[DAOL] Event Value Shift", shorttitle="DAOL Value Shift", overlay=true, max_bars_back=600)
// Written for DAOL. No Community script source copied.
// Foundations: Anchored VWAP and J. Welles Wilder's ATR.
// https://www.tradingview.com/support/solutions/43000669764-anchored-vwap-drawing-tool/
// https://www.tradingview.com/support/solutions/43000501823-average-true-range-atr/
// Combines signed price distance and signed VWAP displacement; neither is
// order flow, actual holder cost, fair value, or a forecast. No novelty claim
// for VWAP, ATR, or their general combination.
string anchorMode = input.string("Exchange day", "Anchor mode", options=["Exchange day", "Selected time"], group="Anchor")
int selectedTime = input.time(timestamp("01 Oct 2026 00:00 +0700"), "Selected anchor time", group="Anchor")
int shiftBars = input.int(5, "VWAP displacement lookback", minval=1, maxval=250, group="Calculation")
int atrLength = input.int(14, "ATR length", minval=2, maxval=250, group="Calculation")
float minDistance = input.float(0.25, "Minimum signed price distance (ATR)", minval=0.01, maxval=5.0, step=0.05, group="Classification")
float farDistance = input.float(1.50, "Far from VWAP (ATR)", minval=0.1, maxval=10.0, step=0.1, group="Classification")
float minShift = input.float(0.10, "Meaningful VWAP displacement (ATR)", minval=0.01, maxval=3.0, step=0.01, group="Classification")
int confirmBars = input.int(2, "Consecutive closed bars to confirm", minval=1, maxval=10, group="Classification")
bool showEnvelope = input.bool(true, "Show distance envelope", group="Display")
bool shadeBackground = input.bool(false, "Shade confirmed state", group="Display")
bool showMarkers = input.bool(true, "Show state changes", group="Display")
bool showDashboard = input.bool(true, "Show dashboard", group="Display")
if not chart.is_standard
runtime.error("Use standard time-based candles/bars, not synthetic charts.")
if anchorMode == "Exchange day" and not timeframe.isintraday
runtime.error("Exchange-day mode requires an intraday chart. Use Selected time for daily or higher timeframes.")
if farDistance <= minDistance
runtime.error("Far distance must exceed minimum price distance.")
bool newDay = timeframe.change("1D")
// Require an observed crossing for selected-time anchoring: do not silently
// start at dataset beginning when the requested anchor is outside history.
bool crossedAnchor = not na(time[1]) and time >= selectedTime and time[1] < selectedTime
float atr = ta.atr(atrLength)
var bool anchored = false
var bool invalidVolume = false
var float weightedSum = 0.0
var float totalVolume = 0.0
var int age = 0
var int actualAnchor = na
var array<float> vwapHistory = array.new_float(0)
var float closedVWAP = na
var float closedATR = na
var float closedDistance = na
var float closedShift = na
var bool ready = false
var int rawState = 0
var int candidate = 0
var int streak = 0
var int confirmedState = 0
var bool initialized = false
bool changed = false
bool reset = false
// State: 1 price above / VWAP up, -1 price below / VWAP down,
// 2 far / VWAP displacement small, 3 price and VWAP displacement opposed,
// 0 near/unclear. Opposed state takes precedence over aligned states.
f_name(int s) =>
s == 1 ? "Above / VWAP rising" : s == -1 ? "Below / VWAP falling" : s == 2 ? "Far / VWAP moving little" : s == 3 ? "Price / VWAP opposed" : "Near / unclear"
f_color(int s) =>
s == 1 ? color.lime : s == -1 ? color.red : s == 2 ? color.rgb(230, 175, 40) : s == 3 ? color.aqua : color.gray
if barstate.isconfirmed
// In day mode skip the first partial loaded day; start at an observed day
// boundary. TradingView's exchange-day boundary is not a user session.
reset := anchorMode == "Exchange day" ? newDay and bar_index > 0 : crossedAnchor
if reset
anchored := true
invalidVolume := false
weightedSum := 0.0
totalVolume := 0.0
age := 0
actualAnchor := time
array.clear(vwapHistory)
initialized := false
streak := 0
confirmedState := 0
closedVWAP := na
closedATR := atr[1]
ready := false
closedDistance := na
closedShift := na
rawState := 0
if anchored
age += 1
if na(volume) or volume < 0.0
invalidVolume := true
else if not invalidVolume
weightedSum += hlc3 * volume
totalVolume += volume
// Any missing volume invalidates the complete anchor accumulation
// until next reset, rather than silently computing a partial VWAP.
closedVWAP := not invalidVolume and totalVolume > 0.0 ? weightedSum / totalVolume : na
array.push(vwapHistory, closedVWAP)
if array.size(vwapHistory) > shiftBars + 1
array.shift(vwapHistory)
float oldVWAP = array.size(vwapHistory) == shiftBars + 1 ? array.get(vwapHistory, 0) : na
ready := not na(closedVWAP) and not na(oldVWAP) and not na(closedATR) and closedATR > 0.0
if ready
closedDistance := (close - closedVWAP) / closedATR
closedShift := (closedVWAP - oldVWAP) / closedATR
bool above = closedDistance >= minDistance
bool below = closedDistance <= -minDistance
bool rising = closedShift >= minShift
bool falling = closedShift <= -minShift
bool farQuiet = math.abs(closedDistance) >= farDistance and math.abs(closedShift) < minShift
if (above and falling) or (below and rising)
rawState := 3
else if above and rising
rawState := 1
else if below and falling
rawState := -1
else if farQuiet
rawState := 2
if streak == 0 or rawState != candidate
candidate := rawState
streak := 1
else
streak := math.min(streak + 1, confirmBars)
if streak >= confirmBars
changed := initialized and candidate != confirmedState
confirmedState := candidate
initialized := true
else
streak := 0
initialized := false
// Break the plotted line on reset to avoid connecting different anchors.
color stateColor = ready and initialized ? f_color(confirmedState) : color.gray
plot(reset ? na : closedVWAP, "Anchored VWAP (closed bars)", color=stateColor, linewidth=2, style=plot.style_linebr)
pUpper = plot(showEnvelope and not reset ? closedVWAP + farDistance * closedATR : na, "Far-distance upper envelope", color=color.new(color.gray, 70), style=plot.style_linebr)
pLower = plot(showEnvelope and not reset ? closedVWAP - farDistance * closedATR : na, "Far-distance lower envelope", color=color.new(color.gray, 70), style=plot.style_linebr)
fill(pUpper, pLower, color=color.new(color.gray, 96), title="Distance envelope (not statistical bands)")
bgcolor(shadeBackground and ready and initialized ? color.new(stateColor, 93) : na)
plot(closedDistance, "Signed price distance (prior ATR)", display=display.data_window)
plot(closedShift, "Signed VWAP displacement (prior ATR)", display=display.data_window)
plot(age, "Bars since anchor (includes anchor bar)", display=display.data_window)
plotshape(showMarkers and changed and confirmedState == 1, title="Aligned up", style=shape.triangleup, location=location.belowbar, color=color.lime, size=size.tiny)
plotshape(showMarkers and changed and confirmedState == -1, title="Aligned down", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.tiny)
plotshape(showMarkers and changed and confirmedState == 2, title="Far quiet", style=shape.diamond, location=location.abovebar, color=color.rgb(230, 175, 40), size=size.tiny)
plotshape(showMarkers and changed and confirmedState == 3, title="Opposed", style=shape.circle, location=location.abovebar, color=color.aqua, size=size.tiny)
var table dashboard = table.new(position.top_right, 2, 6, border_width=1)
if barstate.islast and showDashboard
color panel = color.rgb(35, 35, 35)
string dataStatus = not anchored ? "Waiting for observed anchor" : invalidVolume ? "Missing volume: anchor invalid" : totalVolume <= 0 ? "No positive cumulative volume" : not ready ? "Building VWAP / ATR history" : "Closed-bar context"
table.cell(dashboard, 0, 0, "DAOL Event Value Shift", text_color=color.white, bgcolor=color.rgb(19, 62, 103))
table.cell(dashboard, 1, 0, dataStatus, text_color=color.white, bgcolor=color.rgb(19, 62, 103))
table.cell(dashboard, 0, 1, "Actual anchor", text_color=color.white, bgcolor=panel)
table.cell(dashboard, 1, 1, na(actualAnchor) ? "N/A" : str.format_time(actualAnchor, "yyyy-MM-dd HH:mm", syminfo.timezone), text_color=color.white, bgcolor=panel)
table.cell(dashboard, 0, 2, "Price distance", text_color=color.white, bgcolor=panel)
table.cell(dashboard, 1, 2, na(closedDistance) ? "N/A" : str.tostring(closedDistance, "#.00") + " ATR", text_color=color.white, bgcolor=panel)
table.cell(dashboard, 0, 3, "VWAP shift / " + str.tostring(shiftBars) + " bars", text_color=color.white, bgcolor=panel)
table.cell(dashboard, 1, 3, na(closedShift) ? "N/A" : str.tostring(closedShift, "#.00") + " ATR", text_color=color.white, bgcolor=panel)
table.cell(dashboard, 0, 4, "Latest classification", text_color=color.white, bgcolor=panel)
table.cell(dashboard, 1, 4, ready ? f_name(rawState) : "Waiting", text_color=f_color(rawState), bgcolor=panel)
table.cell(dashboard, 0, 5, "Confirmed state", text_color=color.white, bgcolor=panel)
table.cell(dashboard, 1, 5, ready and initialized ? f_name(confirmedState) : "Waiting", text_color=stateColor, bgcolor=panel)
alertcondition(changed and confirmedState == 1, "DAOL: Above / VWAP rising", "DAOL Event Value Shift: price above VWAP and VWAP displacement upward. {{ticker}} | {{interval}}")
alertcondition(changed and confirmedState == -1, "DAOL: Below / VWAP falling", "DAOL Event Value Shift: price below VWAP and VWAP displacement downward. {{ticker}} | {{interval}}")
alertcondition(changed and confirmedState == 2, "DAOL: Far / little VWAP shift", "DAOL Event Value Shift: price far from VWAP with small VWAP displacement. Not a reversal prediction. {{ticker}} | {{interval}}")
alertcondition(changed and confirmedState == 3, "DAOL: Price / VWAP opposed", "DAOL Event Value Shift: price side and VWAP displacement oppose. {{ticker}} | {{interval}}")
alertcondition(changed, "DAOL: Any confirmed state change", "DAOL Event Value Shift: confirmed context changed. {{ticker}} | {{interval}}")
// USAGE: Exchange day requires intraday standard chart and starts at the first
// observed 1D boundary after dataset start. This is NOT custom Day/Night Session.
// Selected time starts on first bar opening at/after selection; needs a loaded
// bar before selection to observe crossing. An event within a bar is rounded
// to next bar opening. Anchor-bar volume is included in full.
// VWAP uses HLC3*reported volume; zero-volume bars do not affect its value.
// Need shiftBars+1 valid VWAP values within the SAME anchor plus prior ATR.
// Current close and VWAP contribute jointly: metrics are not independent.
// VWAP increasingly inert with anchor age; small shift need not mean weakness.
// Envelope uses ATR distance, not confidence/probability or true holder costs.
// Only closed bars update calculations. First confirmed state per anchor is
// silent. Alerts only later transitions; select Once Per Bar Close.
// Choosing anchors after seeing outcomes creates selection bias. Fix rules
// before evaluating. Feed revisions/rollovers/session changes affect results.
// No future bars, pivots or MTF requests. External numerical checks only;
// TradingView compilation and market-feed validation pending.