本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Anchored VWAP", "avwap", overlay = true, max_lines_count = 500)
// Anchored VWAP, companion to the Aggregated Rolling VWAP (rvwap).
// Two anchors (high and low). Each anchor is a manual date and time. Cumulative volume,
// volume*price and volume*price^2 run forward from the anchor bar; VWAP = sumSV / sumV and the
// bands use the weighted variance (or a percentage of VWAP).
// Per bar, volume is pooled across the selected crypto venues exactly as in rvwap.
// Weighting is decided ONCE per anchor, at the anchor bar, and never switched afterwards:
// 1) pooled venue volume, if any venue has volume on the anchor bar
// 2) otherwise the chart symbol's own volume, if it has any
// 3) otherwise equal weights (an unweighted average, not a true VWAP)
//
// Snap to extreme: finds the bar with the highest high (high anchor) or lowest low (low anchor)
// inside the anchor candle that contains the entered time. It uses a lookahead request, so it
// only works once that candle has closed. If the candle closes while the chart is open, reload
// the chart or change any setting once so the history is recalculated.
//
// To add an anchor: add its inputs, call f_anchor, add its band block and plots, and call
// f_projectAnchor. Plot titles are compile-time, so each new anchor needs its own letter.
//#region ---------------------------- Inputs
// General
GRP_S = "VWAP Settings"
src = input.source(hlc3, "Source", group = GRP_S, display = display.none)
CALC_MODE_TOOLTIP = "Determines the units used to calculate the distance of the bands. When 'Percentage' is selected, a multiplier of 1 means 1%, so the numbers in the plot titles (H +1, L -2, ...) then mean percent, not standard deviations."
calcModeInput = input.string("Standard Deviation", "Bands mode", options = ["Standard Deviation", "Percentage"], group = GRP_S, tooltip = CALC_MODE_TOOLTIP, display = display.none)
// Anchors
GRP_A = "Anchors"
DATE_TOOLTIP = "Date and time of the anchor, as shown in the picker. The picker's time zone is set by TradingView, not by this script, and the anchor is stored as an exact moment in time. Left at 1970, nothing is drawn. Use Defaults > Save as default to store dates. Dates are not symbol-specific, so re-enter them for other assets."
TF_TOOLTIP = "Timeframe of the candle that contains the entered time. Crypto daily candles open at 00:00 UTC, so an extreme late in the Pacific evening can belong to the next UTC day. If this is below the chart timeframe, the anchor snaps to the chart bar that contains the entered time."
SNAP_TOOLTIP = "On: the anchor starts on the bar with the extreme (highest high for the high anchor, lowest low for the low anchor) inside the anchor candle that contains the entered time. Ties use the first matching bar. Only works once that candle has closed; an unclosed candle draws nothing. Off: the anchor starts exactly at the entered time, which also works on a live candle."
highOn = input.bool(true, "High anchor", group = GRP_A)
highTime = input.time(timestamp("6 Oct 2025 11:00 -0700"), "High: date and time", group = GRP_A, tooltip = DATE_TOOLTIP)
highTf = input.timeframe("1D", "High: anchor candle timeframe", group = GRP_A, tooltip = TF_TOOLTIP)
highSnap = input.bool(false, "High: snap to extreme in candle", group = GRP_A, tooltip = SNAP_TOOLTIP)
lowOn = input.bool(true, "Low anchor", group = GRP_A)
lowTime = input.time(timestamp("30 Jun 2026 18:00 -0700"), "Low: date and time", group = GRP_A, tooltip = DATE_TOOLTIP)
lowTf = input.timeframe("1D", "Low: anchor candle timeframe", group = GRP_A, tooltip = TF_TOOLTIP)
lowSnap = input.bool(false, "Low: snap to extreme in candle", group = GRP_A, tooltip = SNAP_TOOLTIP)
// Bands (one toggle per multiplier switches the + and - line together)
GRP_B = "Bands"
showM1 = input.bool(true, "", group = GRP_B, inline = "m1", display = display.none)
mult1 = input.float(1.0, "Band multiplier 1", group = GRP_B, inline = "m1", minval = 0, step = 0.5, display = display.none)
showM2 = input.bool(false, "", group = GRP_B, inline = "m2", display = display.none)
mult2 = input.float(1.5, "Band multiplier 2", group = GRP_B, inline = "m2", minval = 0, step = 0.5, display = display.none)
showM3 = input.bool(false, "", group = GRP_B, inline = "m3", display = display.none)
mult3 = input.float(2.0, "Band multiplier 3", group = GRP_B, inline = "m3", minval = 0, step = 0.5, display = display.none, tooltip = "The plot titles are fixed at +/-1, 1.5 and 2. If you edit a multiplier value, the title keeps its original number.")
// Projection lines. The plotted lines are styled in the Style tab; these cover the projected
// lines only, because drawings can't be styled there.
GRP_J = "Projection Lines"
projVwapOn = input.bool(true, "", group = GRP_J, inline = "pv", tooltip = "Draws the latest value of the VWAP line forward past the last bar. Visual only: the plotted lines hold all the history.")
projVwapColor = input.color(#f48fb1, "VWAP", group = GRP_J, inline = "pv")
projBandOn = input.bool(true, "", group = GRP_J, inline = "pb", tooltip = "Draws the latest value of the visible band lines forward past the last bar. Which bands are projected follows the Bands settings.")
projBandColor = input.color(color.new(#f48fb1, 50), "Bands", group = GRP_J, inline = "pb")
projStyleIn = input.string("Dotted", "Line style", options = ["Solid", "Dashed", "Dotted"], group = GRP_J)
// Exchange aggregation. {B} is replaced with the chart's base asset (BTC, ETH, ...).
GRP_V = "Exchanges ({B} = base asset)"
useAgg = input.bool(false, "Aggregate exchanges", group = GRP_V, tooltip = "On: VWAP and bands use pooled volume from the venues below (the chart symbol only supplies the time axis and base asset, and is not counted separately). Off: the chart symbol's own volume is used. If no venue returns volume on the anchor bar, the chart symbol's volume is used. If the chart has no volume either, every bar is weighted equally (an unweighted average, not a true VWAP).")
spotOn = input.bool(true, "Include spot", group = GRP_V, inline = "m")
perpOn = input.bool(true, "Include perps", group = GRP_V, inline = "m")
s1On = input.bool(true, "", group = GRP_V, inline = "s1")
s1T = input.string("COINBASE:{B}USD", "Spot 1", group = GRP_V, inline = "s1")
s2On = input.bool(true, "", group = GRP_V, inline = "s2")
s2T = input.string("BINANCE:{B}USDT", "Spot 2", group = GRP_V, inline = "s2")
s3On = input.bool(true, "", group = GRP_V, inline = "s3")
s3T = input.string("BYBIT:{B}USDT", "Spot 3", group = GRP_V, inline = "s3")
s4On = input.bool(true, "", group = GRP_V, inline = "s4")
s4T = input.string("OKX:{B}USDT", "Spot 4", group = GRP_V, inline = "s4")
p1On = input.bool(true, "", group = GRP_V, inline = "p1x")
p1T = input.string("BINANCE:{B}USDT.P", "Perp 1", group = GRP_V, inline = "p1x")
p2On = input.bool(false, "", group = GRP_V, inline = "p2x")
p2T = input.string("BYBIT:{B}USDT.P", "Perp 2", group = GRP_V, inline = "p2x")
p3On = input.bool(false, "", group = GRP_V, inline = "p3x")
p3T = input.string("OKX:{B}USDT.P", "Perp 3", group = GRP_V, inline = "p3x")
p4On = input.bool(false, "", group = GRP_V, inline = "p4x")
p4T = input.string("HYPERLIQUID:{B}USDC.P", "Perp 4", group = GRP_V, inline = "p4x")
//#endregion
//#region ---------------------------- Aggregation (same as rvwap)
// gaps_on makes bars where a venue has no data come back as na (counted as zero volume),
// instead of repeating the previous bar's volume.
venueData(bool enabled, string tmpl) =>
float v = na
float sv = na
float ssv = na
if enabled
string sym = str.replace_all(tmpl, "{B}", syminfo.basecurrency)
[a, b, c] = request.security(sym, timeframe.period,
[volume, src * volume, src * src * volume],
gaps = barmerge.gaps_on, ignore_invalid_symbol = true)
v := a
sv := b
ssv := c
[nz(v), nz(sv), nz(ssv)]
[v_s1, sv_s1, ssv_s1] = venueData(useAgg and spotOn and s1On, s1T)
[v_s2, sv_s2, ssv_s2] = venueData(useAgg and spotOn and s2On, s2T)
[v_s3, sv_s3, ssv_s3] = venueData(useAgg and spotOn and s3On, s3T)
[v_s4, sv_s4, ssv_s4] = venueData(useAgg and spotOn and s4On, s4T)
[v_p1, sv_p1, ssv_p1] = venueData(useAgg and perpOn and p1On, p1T)
[v_p2, sv_p2, ssv_p2] = venueData(useAgg and perpOn and p2On, p2T)
[v_p3, sv_p3, ssv_p3] = venueData(useAgg and perpOn and p3On, p3T)
[v_p4, sv_p4, ssv_p4] = venueData(useAgg and perpOn and p4On, p4T)
float totVol = v_s1 + v_s2 + v_s3 + v_s4 + v_p1 + v_p2 + v_p3 + v_p4
float totSV = sv_s1 + sv_s2 + sv_s3 + sv_s4 + sv_p1 + sv_p2 + sv_p3 + sv_p4
float totSSV = ssv_s1 + ssv_s2 + ssv_s3 + ssv_s4 + ssv_p1 + ssv_p2 + ssv_p3 + ssv_p4
//#endregion
//#region ---------------------------- Anchored VWAP engine
// Time of the first loaded bar. An anchor before it, or a candle that starts before it, is hidden.
var int firstTime = time
// One anchor. Returns [vwap, band distance]. Anything invalid returns na and draws nothing.
// on anchor toggle
// anchorMs entered date and time
// reqTf timeframe of the candle request (the chart timeframe when the anchor candle is smaller)
// plain true = start at the chart bar containing the entered time (snap off, or candle below chart timeframe)
// isHigh true = high anchor, false = low anchor
f_anchor(bool on, int anchorMs, string reqTf, bool plain, bool isHigh, float tV, float tSV, float tSSV) =>
var bool found = false
var int mode = 0
var float sumV = 0.0
var float sumSV = 0.0
var float sumSSV = 0.0
// Anchor candle: open, close and its final high and low (lookahead, so only trusted once closed)
[cO, cC, cH, cL] = request.security(syminfo.tickerid, reqTf, [time, time_close, high, low],
lookahead = barmerge.lookahead_on)
bool hasDate = year(anchorMs) > 1970
bool ready = on and hasDate and chart.is_standard and not found and anchorMs >= firstTime
bool hit = false
if ready
if plain
// First bar that ends after the entered time (the bar containing it, or the next one across a gap)
hit := time_close > anchorMs
else
float tol = syminfo.mintick * 0.5
bool inCandle = anchorMs >= cO and anchorMs < cC
bool closed = cC <= timenow
bool whole = cO >= firstTime
bool atExt = isHigh ? math.abs(high - cH) <= tol : math.abs(low - cL) <= tol
// First bar of the candle sitting on the extreme
hit := inCandle and closed and whole and atExt
if hit
found := true
// Weighting is decided here, once, and never switched
mode := (useAgg and tV > 0) ? 1 : nz(volume) > 0 ? 2 : 3
if found
float bV = mode == 1 ? tV : mode == 2 ? nz(volume) : 1.0
float bSV = mode == 1 ? tSV : mode == 2 ? nz(src * volume) : src
float bSSV = mode == 1 ? tSSV : mode == 2 ? nz(src * src * volume) : src * src
sumV += bV
sumSV += bSV
sumSSV += bSSV
bool valid = found and sumV > 0
float vw = valid ? sumSV / sumV : na
float varc = valid ? math.max(sumSSV / sumV - vw * vw, 0.0) : na
float basis = calcModeInput == "Standard Deviation" ? math.sqrt(varc) : vw * 0.01
[vw, basis]
// An anchor candle below the chart timeframe snaps to the containing chart bar instead.
bool highBelow = timeframe.in_seconds(highTf) < timeframe.in_seconds()
bool lowBelow = timeframe.in_seconds(lowTf) < timeframe.in_seconds()
string highReq = highBelow ? timeframe.period : highTf
string lowReq = lowBelow ? timeframe.period : lowTf
[vwH, bH] = f_anchor(highOn, highTime, highReq, not highSnap or highBelow, true, totVol, totSV, totSSV)
[vwL, bL] = f_anchor(lowOn, lowTime, lowReq, not lowSnap or lowBelow, false, totVol, totSV, totSSV)
float uH1 = vwH + bH * mult1
float lH1 = vwH - bH * mult1
float uH15 = vwH + bH * mult2
float lH15 = vwH - bH * mult2
float uH2 = vwH + bH * mult3
float lH2 = vwH - bH * mult3
float uL1 = vwL + bL * mult1
float lL1 = vwL - bL * mult1
float uL15 = vwL + bL * mult2
float lL15 = vwL - bL * mult2
float uL2 = vwL + bL * mult3
float lL2 = vwL - bL * mult3
//#endregion
//#region ---------------------------- Plots (colours and line styles are set in the Style tab)
plot(vwH, title = " high", color = #f48fb1, linestyle = plot.linestyle_dotted)
pHu1 = plot(showM1 ? uH1 : na, title = " highVAH", color = color.new(#f48fb1, 50), linestyle = plot.linestyle_dotted)
pHl1 = plot(showM1 ? lH1 : na, title = " highVAL", color = color.new(#f48fb1, 50), linestyle = plot.linestyle_dotted)
pHu15 = plot(showM2 ? uH15 : na, title = " high+1.5", color = color.new(#f48fb1, 50), linestyle = plot.linestyle_dotted)
pHl15 = plot(showM2 ? lH15 : na, title = " high-1.5", color = color.new(#f48fb1, 50), linestyle = plot.linestyle_dotted)
pHu2 = plot(showM3 ? uH2 : na, title = " high+2", color = color.new(#f48fb1, 50), linestyle = plot.linestyle_dotted)
pHl2 = plot(showM3 ? lH2 : na, title = " high-2", color = color.new(#f48fb1, 50), linestyle = plot.linestyle_dotted)
fill(pHu1, pHl1, title = " H fill 1", color = color.new(#f48fb1, 90), display = display.none)
fill(pHu15, pHl15, title = " H fill 1.5", color = color.new(#f48fb1, 90), display = display.none)
fill(pHu2, pHl2, title = " H fill 2", color = color.new(#f48fb1, 90), display = display.none)
plot(vwL, title = " low", color = #f48fb1, linestyle = plot.linestyle_dotted)
pLu1 = plot(showM1 ? uL1 : na, title = " lowVAH", color = color.new(#f48fb1, 50), linestyle = plot.linestyle_dotted)
pLl1 = plot(showM1 ? lL1 : na, title = " lowVAL", color = color.new(#f48fb1, 50), linestyle = plot.linestyle_dotted)
pLu15 = plot(showM2 ? uL15 : na, title = " low+1.5", color = color.new(#f48fb1, 50), linestyle = plot.linestyle_dotted)
pLl15 = plot(showM2 ? lL15 : na, title = " low-1.5", color = color.new(#f48fb1, 50), linestyle = plot.linestyle_dotted)
pLu2 = plot(showM3 ? uL2 : na, title = " low+2", color = color.new(#f48fb1, 50), linestyle = plot.linestyle_dotted)
pLl2 = plot(showM3 ? lL2 : na, title = " low-2", color = color.new(#f48fb1, 50), linestyle = plot.linestyle_dotted)
fill(pLu1, pLl1, title = " L fill 1", color = color.new(#f48fb1, 90), display = display.none)
fill(pLu15, pLl15, title = " L fill 1.5", color = color.new(#f48fb1, 90), display = display.none)
fill(pLu2, pLl2, title = " L fill 2", color = color.new(#f48fb1, 90), display = display.none)
//#endregion
//#region ---------------------------- Forward projection (visual only)
// Lines updated on the last bar only, coloured and styled from the inputs above.
// An invalid or hidden value clears its line, so no stale line is left behind.
f_newProj() =>
line.new(na, na, na, na, extend = extend.right)
f_lineStyle(string s) =>
s == "Dashed" ? line.style_dashed : s == "Dotted" ? line.style_dotted : line.style_solid
f_proj(line l, bool show, float y, color c) =>
if show and not na(y)
l.set_xy1(bar_index, y)
l.set_xy2(bar_index + 1, y)
l.set_color(c)
l.set_style(f_lineStyle(projStyleIn))
else
l.set_xy1(na, na)
l.set_xy2(na, na)
// Seven projection lines for one anchor: VWAP plus three band pairs.
f_projectAnchor(bool on, float vw, float u1, float l1, float u2, float l2, float u3, float l3) =>
var line lVwap = f_newProj()
var line lU1 = f_newProj()
var line lL1 = f_newProj()
var line lU2 = f_newProj()
var line lL2 = f_newProj()
var line lU3 = f_newProj()
var line lL3 = f_newProj()
if barstate.islast
f_proj(lVwap, on and projVwapOn, vw, projVwapColor)
f_proj(lU1, on and projBandOn and showM1, u1, projBandColor)
f_proj(lL1, on and projBandOn and showM1, l1, projBandColor)
f_proj(lU2, on and projBandOn and showM2, u2, projBandColor)
f_proj(lL2, on and projBandOn and showM2, l2, projBandColor)
f_proj(lU3, on and projBandOn and showM3, u3, projBandColor)
f_proj(lL3, on and projBandOn and showM3, l3, projBandColor)
f_projectAnchor(highOn, vwH, uH1, lH1, uH15, lH15, uH2, lH2)
f_projectAnchor(lowOn, vwL, uL1, lL1, uL15, lL15, uL2, lL2)
//#endregion