Anchored VWAP
An anchored Volume Weighted Average Price (VWAP) calculates the weighted average price from a manually selected bar forward, rather than from the start of a trading session or a fixed time interval. Traders use this to establish a dynamic reference level from chart-identified pivots, breakouts, or swing points. This is especially useful when the session start or standard VWAP anchor point does not coincide with a meaningful structural level [1].
//@version=6
indicator("Anchored VWAP", overlay=true, max_bars_back=5000)
// Inputs
anchorBarNum = input.int(1, minval=1, title="Bars Back to Anchor Point",
tooltip="Number of bars back from current bar to use as anchor (1 = current bar)")
lineColor = input.color(color.blue, title="VWAP Line Color")
lineWidth = input.int(2, minval=1, maxval=5, title="Line Width")
showBands = input.bool(false, title="Show ±1 Std Dev Bands")
bandColor = input.color(color.gray, title="Band Color")
// Identify anchor bar
anchorIdx = bar_index - (anchorBarNum - 1)
isAnchorBar = bar_index == anchorIdx
barsSinceAnchor = bar_index - anchorIdx
// Calculate cumulative typical price * volume from anchor
tp = (high + low + close) / 3.0
cumulTP = 0.0
cumulVol = 0.0
stdDevSum = 0.0
if isAnchorBar
cumulTP := tp * volume
cumulVol := volume
else if barsSinceAnchor > 0
cumulTP := nz(cumulTP[1], 0) + tp * volume
cumulVol := nz(cumulVol[1], 0) + volume
// Calculate VWAP
vwap = cumulVol > 0 ? cumulTP / cumulVol : na
// Calculate standard deviation for bands
if barsSinceAnchor >= 0
devFromVwap = (tp - vwap) * (tp - vwap)
stdDevSum := nz(stdDevSum[1], 0) + devFromVwap
barsN = barsSinceAnchor + 1
variance = barsN > 1 ? stdDevSum / (barsN - 1) : 0
stdDev = math.sqrt(variance)
upperBand = vwap + stdDev
lowerBand = vwap - stdDev
// Plot
plot(vwap, title="VWAP", color=lineColor, linewidth=lineWidth)
plot(showBands ? upperBand : na, title="Upper Band +1σ",
color=color.new(bandColor, 50), linewidth=1, style=plot.style_dashed)
plot(showBands ? lowerBand : na, title="Lower Band -1σ",
color=color.new(bandColor, 50), linewidth=1, style=plot.style_dashed)
plot(isAnchorBar ? close : na, title="Anchor Point",
color=color.red, style=plot.style_circles, linewidth=3)
How the code works
The script uses bar_index to identify bars relative to the current chart position. The anchor is calculated as bar_index, (anchorBarNum, 1): if the user selects "1 bar back," that places the anchor at the most recent closed bar; "2 bars back" anchors to the bar before that, and so on. The variable barsSinceAnchor tracks how many bars have passed since the anchor point.
From the anchor bar forward, the code accumulates two running totals: cumulTP (cumulative typical price multiplied by volume) and cumulVol (cumulative volume). The typical price is (high + low + close) / 3, a standard mid-point estimate used in many volume-weighted metrics [2]. On each bar following the anchor, these totals grow by adding the current bar's contribution. VWAP is then the ratio: cumulTP / cumulVol.
For the optional bands, the script calculates standard deviation of price deviations from the VWAP. This requires tracking the sum of squared differences from VWAP across all bars from anchor onward, then dividing by (n-1) to estimate sample variance. A single standard deviation is plotted above and below the VWAP line in dashed gray.
The anchor bar itself is marked with a red circle, making the reference point visually explicit on the chart.
Reading it on a chart
Traders typically anchor VWAP at a swing high or low, a breakout level, or the bar when a significant news event occurred. The resulting line represents the volume-weighted average price from that moment forward. Price trading above anchored VWAP suggests buying pressure has dominated since the anchor; price below suggests selling pressure. A bounce off the VWAP from above (or below) can signal support or resistance.
The bands extend one standard deviation in each direction. Closes near or beyond the bands indicate price has moved significantly away from its weighted average; this can flag overextension or a potential mean-reversion setup, though neither guarantees reversal.
Anchored VWAP is particularly useful in volatile markets where intraday or multiday trends distort the traditional session-based VWAP. For example, a trader who identifies a daily market pivot can anchor from that bar and use the resulting VWAP as a dynamic reference for the following trading sessions, without waiting for the next session to begin.
Limitations
This indicator is free of lookahead: it calculates VWAP using only bars from the anchor point to the current bar, with no future data. However, the anchor bar itself is selectable by the user and appears on all subsequent bars, which is a UX choice, not a repainting flaw.
VWAP is a weighted average and is therefore lag-prone when volatility or volume structure changes sharply. A sudden spike in volume can move VWAP significantly, and once that bar is historical, the line becomes static on that bar. Traders should avoid treating anchored VWAP as a precise support or resistance level; it is a trend reference, not a support/resistance floor.
Standard deviation bands assume a normal distribution of price deviations, which is not always true in real markets; fat tails and gaps mean price can exceed the bands more often than normal theory predicts. The bands provide context but not firm reversal signals.
Anchoring at a point that is not structurally significant (e.g., a random bar during a consolidation) produces a VWAP line that is misleading. The quality of the indicator depends entirely on the choice of anchor point. No automated anchor selection is provided; this is by design, to keep the tool under trader control.
The indicator stores cumulative sums in variables; on very long charts (5000+ bars history), the cumulative totals can grow large, though Pine Script's floating-point arithmetic can handle typical volume and price ranges.
Key definitions
VWAP: Volume Weighted Average Price; the cumulative average of prices weighted by volume transacted at each price, calculated as the sum of (typical price × volume) divided by the sum of volume.
Typical Price: The average of the high, low, and close for a bar, used as a representative price for the bar's trading range.
Anchored VWAP: A VWAP calculation that begins from a user-selected bar, rather than from a fixed time interval or session start.
Standard Deviation: A measure of how far prices deviate from their weighted average; the square root of the variance of deviations from VWAP.
Volume-Weighted: A calculation method that weights each price by the volume traded at that price, giving higher volume bars more influence on the average.
Mean Reversion: The tendency of prices to return toward an average level after deviating significantly from it.
References
- Investopedia, "Volume Weighted Average Price (VWAP)", Investopedia, https://www.investopedia.com/terms/v/vwap.asp
- TradingView, "Pine Script Documentation", TradingView, https://www.tradingview.com/pine-script-docs/
- CME Group, "Basics of Volume", CME Group Education, https://www.cmegroup.com/education/courses/basics-of-volume.html
- Wikipedia, "Standard Deviation", Wikipedia, https://en.wikipedia.org/wiki/Standard_deviation
Educational research on historical data only. Not investment advice, not a signal, and never a performance promise. Past results do not predict future performance. Every reference is link-verified before publication and every paper is re-audited weekly against the library's editorial standard.
Last reviewed by the PropLedger research pipeline: 2026-08-30. Educational research on historical data, not financial advice.
Keep reading
Inside & Outside Bar Highlighter
Free open-source Pine Script indicator: inside bar and outside bar highlighter. Full code and a plain-English walkthrough.
Swing Structure & Break of Structure Marker
Free open-source Pine Script indicator: swing high/low structure marker (BOS and CHoCH labels). Full code and a plain-English walkthrough.
Relative Volume vs N-Day Average
Free open-source Pine Script indicator: relative volume vs N-day average. Full code and a plain-English walkthrough.