本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) Bitpunk
//@version=6
indicator("VWAP Reclaim Signals [bitpunk]", shorttitle="bitpunk.vwap-reclaim", overlay=true)
// Independent public definition. See research/design.md and the family design review.
string dailyAnchor = input.string("Month", "Daily Anchor", options=["Month", "Week"], tooltip="Anchor used on standard 1D charts. Month accumulates this month's daily HLC3 × volume; Week restarts each week and leaves fewer candles for confirmation. Intraday charts always use the exchange-day anchor. A new anchor cancels pending acceptance.", display=display.none)
int validityBars = input.int(5, "Acceptance Window", minval=1, maxval=30, tooltip="Maximum following candles available after a fresh VWAP cross, ending earlier at a new anchor. A later close must clear the crossing candle's high/low while holding the VWAP side. A close at/through VWAP cancels; wick-only tests are allowed. Another event needs a new cross.", display=display.none)
float maxExtension = input.float(1.0, "Max Entry Extension (TR)", minval=0, maxval=3, step=0.1, tooltip="Maximum entry-close distance from current VWAP, in the cross's prior 20-bar mean true range. Zero disables this cap. An oversized confirmation consumes that cross; it cannot become a later catch-up entry.", display=display.none)
bool bodyConfirmation = input.bool(true, "Body Confirmation", group="Signals", tooltip="Provisional local timing mode: break the frozen candle-body extreme instead of its wick. Earlier confirmation can be weaker; market efficacy is unproven. Turn off to restore Original wick confirmation. Recreate alerts after switching. Review by 2027-01-01.", display=display.none)
bool enableSignals = input.bool(true, "Enable Signals", group="Signals", tooltip="Enable entry markers and signal alerts. Disabling keeps structure, setup state and context readings active. Recreate existing alerts after changing this setting.", display=display.none)
bool showStructure = input.bool(true, "Structure", group="Display", tooltip="Show the reference, decision area and frozen confirmation gate. Display switches never change events or alerts.", display=display.none)
string panelChoice = input.string("bottom", "Status Panel", options=["none", "bottom", "top", "bottom-left", "top-left"], group="Display", display=display.none)
string panelSize = input.string("small", "Panel Size", options=["small", "medium", "large"], group="Display", display=display.none)
string structureEffects = input.string("Enhanced", "Structure Effects", options=["Normal", "Enhanced"], group="Display", tooltip="Enhanced uses quiet native glass with a transparent center. Exact boundaries and the signal definition are unchanged.", display=display.none)
string signalEffects = input.string("Enhanced", "Signal Effects", options=["Normal", "Enhanced"], group="Display", tooltip="White ▲/▼ on theme backgrounds. Enhanced adds a close pin, soft glow and larger labels, without grading the signal.", display=display.none)
color bullColor = input.color(color.rgb(0, 194, 255), "Bull", inline="theme", group="Theme Colors", display=display.none)
color bearColor = input.color(color.rgb(255, 56, 100), "Bear", inline="theme", group="Theme Colors", display=display.none)
// Pure visual helpers from the public Compression Break product; no private dependency.
// Public presentation helpers: opaque primary ink and readable white glyphs.
f_v_linear(float channel) =>
float unit = channel / 255.0
unit <= 0.04045 ? unit / 12.92 : math.pow((unit + 0.055) / 1.055, 2.4)
f_v_luminance(color value) =>
0.2126 * f_v_linear(color.r(value)) + 0.7152 * f_v_linear(color.g(value)) + 0.0722 * f_v_linear(color.b(value))
f_v_contrast(color first, color second) =>
float a = f_v_luminance(first)
float b = f_v_luminance(second)
(math.max(a, b) + 0.05) / (math.min(a, b) + 0.05)
f_v_ink(color source) =>
color result = color.rgb(color.r(source), color.g(source), color.b(source))
bool lightBackground = f_v_luminance(chart.bg_color) > 0.179
for step = 0 to 15
if f_v_contrast(result, chart.bg_color) < 3.2
result := lightBackground ? color.rgb(math.floor(color.r(result) * 0.8), math.floor(color.g(result) * 0.8), math.floor(color.b(result) * 0.8)) : color.rgb(math.ceil(color.r(result) + (255 - color.r(result)) * 0.2), math.ceil(color.g(result) + (255 - color.g(result)) * 0.2), math.ceil(color.b(result) + (255 - color.b(result)) * 0.2))
result
f_v_label(color source) =>
color result = color.rgb(color.r(source), color.g(source), color.b(source))
for step = 0 to 15
if f_v_luminance(result) > 0.175
result := color.rgb(math.floor(color.r(result) * 0.9), math.floor(color.g(result) * 0.9), math.floor(color.b(result) * 0.9))
result
var color f_vBullInk = f_v_ink(bullColor)
var color f_vBearInk = f_v_ink(bearColor)
var color f_vBullLabel = f_v_label(bullColor)
var color f_vBearLabel = f_v_label(bearColor)
f_panel_position(string choice) =>
switch choice
"top" => position.top_right
"top-left" => position.top_left
"bottom-left" => position.bottom_left
=> position.bottom_right
f_dim(color source, float strength) =>
color.from_gradient(strength, 0, 1, chart.bg_color, source)
f_ink(color source) =>
float luminance = 0.299 * color.r(chart.bg_color) + 0.587 * color.g(chart.bg_color) + 0.114 * color.b(chart.bg_color)
luminance > 128 ? color.from_gradient(0.35, 0, 1, source, chart.fg_color) : source
f_signal(color source) =>
float luminance = 0.299 * color.r(chart.bg_color) + 0.587 * color.g(chart.bg_color) + 0.114 * color.b(chart.bg_color)
luminance > 128 ? color.from_gradient(0.50, 0, 1, source, chart.fg_color) : color.from_gradient(0.78, 0, 1, chart.bg_color, source)
f_tf() =>
float seconds = timeframe.isticks ? na : timeframe.in_seconds()
seconds < 60 ? str.tostring(seconds) + "s" : seconds < 3600 ? str.tostring(seconds / 60) + "m" : seconds < 86400 ? str.tostring(seconds / 3600) + "h" : timeframe.period == "D" ? "1D" : timeframe.period == "W" ? "1W" : timeframe.period == "M" ? "1M" : timeframe.period
// Dimensionless comparisons avoid Pine's absolute nine-decimal comparison
// floor on small quotes. This adds no price tolerance or user parameter.
f_distance(float price, float referencePrice, float scale) =>
(price - referencePrice) / scale
var color neutralColor = chart.fg_color
var color bullInk = f_v_ink(bullColor)
var color bearInk = f_v_ink(bearColor)
var color bullSignal = f_signal(bullColor)
var color bearSignal = f_signal(bearColor)
// VWAP is a stable incremental weighted mean of this chart's HLC3 candles.
float meanTR = ta.sma(ta.tr(true), 20)
float priorScale = meanTR[1]
bool dailyChart = timeframe.isdaily and timeframe.multiplier == 1
string anchorTimeframe = dailyChart ? (dailyAnchor == "Week" ? "1W" : "1M") : "1D"
bool supported = chart.is_standard and (timeframe.isintraday or dailyChart) and not timeframe.isticks and not na(time_close)
bool anchorReset = timeframe.change(anchorTimeframe) or barstate.isfirst
bool pricesReady = (not bodyConfirmation or (not na(open) and open >= low and open <= high)) and not na(high) and not na(low) and not na(close) and high >= low and close >= low and close <= high
// ta.tr(true) substitutes high-low when the previous close is missing. That
// fallback can draw an envelope, but cannot qualify a complete entry scale.
bool trueRangeReady = pricesReady and not na(close[1])
bool scaleReady = math.sum(trueRangeReady ? 1.0 : 0.0, 20)[1] == 20
var bool anchorValid = false
var float totalVolume = 0
var float vwap = na
var float previousClose = na
var bool previousEligible = false
var int pendingSide = 0
var int startedAt = na
var float frozenGate = na
var float frozenScale = na
var string status = "Await complete anchor"
bool longSignal = false
bool shortSignal = false
bool newCross = false
if barstate.isconfirmed
if anchorReset
// The first loaded candle also needs the exact selected boundary.
anchorValid := time == time(anchorTimeframe)
totalVolume := 0
vwap := na
previousClose := na
previousEligible := false
pendingSide := 0
status := anchorValid ? "New " + anchorTimeframe + " anchor" : "Incomplete " + anchorTimeframe + " boundary"
if not supported or not pricesReady or na(volume) or volume < 0
anchorValid := false
vwap := na
pendingSide := 0
previousEligible := false
status := supported ? "Invalid price/volume anchor" : "Standard intraday or 1D chart required"
else if anchorValid
float priorVwap = vwap
if volume == 0
pendingSide := 0
previousEligible := false
previousClose := close
status := "Zero volume · no entry"
else
if pendingSide == 0
status := not scaleReady or na(priorScale) ? "Await 20 complete true ranges" : math.sign(priorScale) != 1 ? "No positive TR scale" : "Await fresh VWAP cross"
float nextVolume = totalVolume + volume
vwap := na(vwap) ? hlc3 : vwap + volume / nextVolume * (hlc3 - vwap)
totalVolume := nextVolume
int crossSide = previousEligible and not na(priorVwap) and math.sign(priorScale) == 1 ? (f_distance(previousClose, priorVwap, priorScale) <= 0 and f_distance(close, vwap, priorScale) > 0 ? 1 : f_distance(previousClose, priorVwap, priorScale) >= 0 and f_distance(close, vwap, priorScale) < 0 ? -1 : 0) : 0
if pendingSide != 0
if bar_index - startedAt > validityBars
pendingSide := 0
status := "Expired · wait for new cross"
else if pendingSide * f_distance(close, vwap, frozenScale) <= 0
pendingSide := 0
status := "VWAP lost · wait for new cross"
else if bar_index > startedAt and pendingSide * f_distance(close, frozenGate, frozenScale) > 0
bool withinCap = maxExtension == 0 or pendingSide * f_distance(close, vwap, frozenScale) <= maxExtension
longSignal := pendingSide == 1 and withinCap
shortSignal := pendingSide == -1 and withinCap
pendingSide := 0
status := withinCap ? (longSignal ? "Long confirmed" : "Short confirmed") : "Extended · new cross required"
if not longSignal and not shortSignal and crossSide != 0 and pendingSide == 0 and scaleReady and not na(priorScale) and math.sign(priorScale) == 1
pendingSide := crossSide
startedAt := bar_index
frozenGate := crossSide > 0 ? (bodyConfirmation ? math.max(open, close) : high) : (bodyConfirmation ? math.min(open, close) : low)
frozenScale := priorScale
newCross := true
status := crossSide > 0 ? "Long cross · wait for price break" : "Short cross · wait for price break"
previousClose := close
previousEligible := true
// The quarter-TR envelope is display depth only; entries use the exact center.
// The first open candle of a new anchor cannot display yesterday's pending gate.
bool newAnchorPending = supported and anchorReset and not barstate.isconfirmed
bool anchorReady = anchorValid and not newAnchorPending
float displayScale = barstate.isconfirmed ? meanTR : meanTR[1]
float reference = showStructure and anchorReady and not anchorReset ? vwap : na
color accent = f_distance(previousClose, vwap, displayScale) > 0 ? bullInk : f_distance(previousClose, vwap, displayScale) < 0 ? bearInk : f_dim(neutralColor, 0.55)
bool activeReference = pendingSide != 0 or longSignal or shortSignal
bool enhancedStructure = structureEffects == "Enhanced"
float entryLimit = anchorReady and activeReference and maxExtension > 0 ? vwap + (pendingSide != 0 ? pendingSide : longSignal ? 1 : -1) * maxExtension * frozenScale : na
color edgeColor = color.new(accent, enhancedStructure ? (activeReference ? 42 : 68) : 82)
plot(enhancedStructure ? reference : na, "VWAP Edge Glow", color.new(accent, activeReference ? 86 : 94), 6, plot.style_linebr, display=display.pane, editable=false)
centerPlot = plot(reference, "Anchored VWAP", color.new(accent, activeReference ? 0 : 15), 2, plot.style_linebr, display=display.pane)
upperPlot = plot(reference + 0.25 * displayScale, "Visual Envelope Upper", edgeColor, 1, plot.style_linebr, display=display.pane)
lowerPlot = plot(reference - 0.25 * displayScale, "Visual Envelope Lower", edgeColor, 1, plot.style_linebr, display=display.pane)
color glass = color.new(accent, enhancedStructure ? (activeReference ? 92 : 97) : 98)
fill(upperPlot, centerPlot, reference + 0.25 * displayScale, reference, glass, color.new(accent, 100), title="Upper Glass", fillgaps=false)
fill(centerPlot, lowerPlot, reference, reference - 0.25 * displayScale, color.new(accent, 100), glass, title="Lower Glass", fillgaps=false)
plot(showStructure and anchorReady and not newCross and (pendingSide != 0 or longSignal or shortSignal) ? frozenGate : na, "Frozen Price Gate", color.new(pendingSide < 0 or shortSignal ? bearInk : bullInk, 32), 1, plot.style_linebr, display=display.pane, linestyle=plot.linestyle_dotted)
plot(showStructure and not newCross ? entryLimit : na, "Entry Extension Limit", color.new(accent, 65), 1, plot.style_linebr, display=display.pane, linestyle=plot.linestyle_dashed)
plot(anchorReady ? vwap : na, "Exact VWAP", display=display.data_window)
plot(anchorReady and (pendingSide != 0 or longSignal or shortSignal) ? frozenGate : na, "Exact Price Gate", display=display.data_window)
plot(anchorReady ? pendingSide : 0, "Pending Side", display=display.data_window)
plot(volume, "Raw Volume", display=display.data_window)
plot(anchorReady ? totalVolume : na, "Anchor Volume", display=display.data_window)
plot(entryLimit, "Exact Entry Limit", display=display.data_window)
plot(anchorReady and pendingSide != 0 ? math.max(0, validityBars - ((barstate.isconfirmed ? bar_index : bar_index - 1) - startedAt)) : na, "Eligible Future Closes", display=display.data_window)
int closesLeft = pendingSide != 0 ? math.max(0, validityBars - ((barstate.isconfirmed ? bar_index : bar_index - 1) - startedAt)) : 0
string detail = anchorTimeframe + " anchor · " + (anchorReady ? (pendingSide != 0 ? "Gate " + str.tostring(frozenGate, format.mintick) + " · " + str.tostring(closesLeft) + " closes left" : "VWAP " + (na(vwap) ? "n/a" : str.tostring(vwap, format.mintick))) : "Observed boundary required")
// Fixed sizes keep plotshape parameters const; every display switch is cosmetic.
bool enhancedSignal = signalEffects == "Enhanced"
color entryAccent = longSignal ? bullColor : bearColor
color entryInk = longSignal ? bullSignal : bearSignal
plotshape(enableSignals and (enhancedSignal and (longSignal or shortSignal)) ? close : na, "Entry Glow", shape.circle, location.absolute, color.new(entryAccent, 80), size=size.large, display=display.pane)
plotshape(enableSignals and (enhancedSignal and (longSignal or shortSignal)) ? close : na, "Entry Close Pin", shape.circle, location.absolute, longSignal ? f_vBullInk : f_vBearInk, size=size.tiny, display=display.pane)
plotshape(enableSignals and (enhancedSignal and longSignal), "Long Entry", shape.labelup, location.belowbar, f_vBullLabel, text="▲", textcolor=color.white, size=size.small, display=display.pane)
plotshape(enableSignals and (not enhancedSignal and longSignal), "Long Normal", shape.labelup, location.belowbar, f_vBullLabel, text="▲", textcolor=color.white, size=size.tiny, display=display.pane)
plotshape(enableSignals and (enhancedSignal and shortSignal), "Short Entry", shape.labeldown, location.abovebar, f_vBearLabel, text="▼", textcolor=color.white, size=size.small, display=display.pane)
plotshape(enableSignals and (not enhancedSignal and shortSignal), "Short Normal", shape.labeldown, location.abovebar, f_vBearLabel, text="▼", textcolor=color.white, size=size.tiny, display=display.pane)
plot(enableSignals and (longSignal) ? close : na, "Long Entry Close", display=display.data_window)
plot(enableSignals and (shortSignal) ? close : na, "Short Entry Close", display=display.data_window)
// Select a directional condition OR Any alert() function call to avoid duplicates.
alertcondition(enableSignals and (longSignal), "Long Entry", "{{interval}} ▲ LONG @{{close}}")
alertcondition(enableSignals and (shortSignal), "Short Entry", "{{interval}} ▼ SHORT @{{close}}")
if enableSignals and barstate.isrealtime and barstate.isconfirmed and (longSignal or shortSignal)
alert(f_tf() + (longSignal ? " ▲ LONG @" : " ▼ SHORT @") + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)
// The card reports a completed setup, never a probability or an open position.
var table panel = na
if barstate.islast
if panelChoice == "none"
if not na(panel)
table.delete(panel)
panel := na
else
if na(panel)
panel := table.new(position.bottom_right, 1, 3, bgcolor=f_dim(chart.fg_color, 0.025), frame_color=f_dim(chart.fg_color, 0.12), frame_width=1, border_width=0)
table.set_position(panel, f_panel_position(panelChoice))
string panelTextSize = panelSize == "small" ? size.tiny : panelSize == "large" ? size.normal : size.small
string panelHeadingSize = panelSize == "small" ? size.small : panelSize == "large" ? size.large : size.normal
color panelAccent = anchorReady and (pendingSide > 0 or longSignal) ? bullColor : anchorReady and (pendingSide < 0 or shortSignal) ? bearColor : neutralColor
table.cell(panel, 0, 0, " VWAP RECLAIM · " + f_tf() + " ", bgcolor=f_dim(panelAccent, 0.06), text_color=f_ink(panelAccent), text_size=panelHeadingSize, text_halign=text.align_left)
table.cell(panel, 0, 1, " " + (newAnchorPending ? "New anchor · wait for close" : status) + " ", text_color=f_dim(chart.fg_color, 0.86), text_size=panelTextSize, text_halign=text.align_left)
table.cell(panel, 0, 2, " " + detail + " ", text_color=f_dim(chart.fg_color, 0.60), text_size=panelTextSize, text_halign=text.align_left)