top of page

Pine Script Library

Public·1 member

VWAP with Anchored Std Dev Bands — Volume Drop


# VWAP with Anchored Std Dev Bands — Volume Drop


Anchored VWAP tells you where the volume-weighted crowd is positioned; standard deviation bands around it tell you how dispersed that positioning is. Together you get a fair-value rail with statistical context for how far is "too far" — without forcing an arbitrary lookback window.


## What it does


- Builds a volume-weighted average price anchored to the start of each Session, Week, or Month (your choice).

- Computes a volume-weighted standard deviation from the same anchored data set using the closed-form `Var = E[P²] − (E[P])²`.

- Plots ±1σ, ±2σ, and ±3σ envelopes around the VWAP — they widen as participation disperses and tighten as the auction balances.

- Shades the 2σ–3σ zones as "volatility extremes" so reversion setups jump off the chart.

- Live label shows current VWAP, 1σ, and the full 2σ bandwidth.


## Recommended settings


- **Anchor Period:** `Session` for intraday, `Week` for swing, `Month` for position work.

- **Source:** `hlc3` (typical price) — the canonical volume-weighting input.

- **Band 1 / 2 / 3:** `1.0 / 2.0 / 3.0` — change only if you trade a chronically high- or low-vol instrument.

- **Show ±3σ Band:** `true` — it's where reversion trades actually live.

- **Shade 2σ–3σ Volatility Extreme:** `true` — visual cue for the extreme zone.


## Ideal timeframe


1m–5m for `Session` anchor on US equity index futures; 15m–1h for `Week`; daily for `Month`.


## How to use


Treat the anchored VWAP as fair value and the ±1σ band as the "normal" trading envelope. Touches of ±2σ during regular auction (no news, no halt) are statistical extremes — most fade back to the mean. Touches of ±3σ are rarer and usually signal a regime change rather than a fade. Use VWAP reclaims/losses as bias flips, and 2σ extensions as either profit targets (if you entered near the VWAP) or fade entries with a stop beyond the 3σ band.


## Pine Script v5 code


```

//@version=5

// ─────────────────────────────────────────────

// │ MarketFragments.com | DNA & Market │

// │ info@marketfragments.com │

// │ www.marketfragments.com │

// ─────────────────────────────────────────────

// Time →

// │

// █ █ █│ █

// █ █ █ │ █ █

// █ █ █ │ █ █ █ ╭─╮

// █ █ █ │ █ █ █ █ ╭─╯ ╰─╮

// █ █ █ │ █ █ █ █ █ █ ╭─╯ ╰─╮

// █ █ █ │ █ █ █ █ █ █ █ █ ╭─╯ ╰─╮

// █ █ █ │ █ █ █ █ █ █ █ █ █ █╭─╯ ╰─╮

// █ █ █ │ █ █ █ █ █ █ █ █ ╰─╮ ╭─╯

// █ █ █ │ █ █ █ █ █ █ ╰─╮ ╭─╯

// █ █ █ │ █ █ █ █ ╰─╮ ╭─╯

// █ █ █ │ █ █ ╰─╮ ╭─╯

// █ █ █ │ █ ╰─────╯

// ──────┴──────────────────────────────────────────────────────────────

// T1 T2 T3 T4 T5 T6

//

// Indicator: VWAP with Anchored Std Dev Bands

// Category : Volume

// Free for non-commercial use. Attribution appreciated.

//

// What it does:

// Plots a volume-weighted average price anchored to the start of each

// Session / Week / Month, surrounded by ±1σ, ±2σ, and ±3σ standard

// deviation envelopes computed from the same anchored data set. The

// bands describe where price has actually traded around fair value

// so far in the anchor period — they widen as activity disperses and

// tighten when the auction balances.

// ─────────────────────────────────────────────


indicator("MarketFragments — VWAP w/ Anchored Std Dev Bands", shorttitle="MF VWAP Bands", overlay=true)


// ───── Inputs ─────

anchorOpt = input.string("Session", "Anchor Period", options=["Session", "Week", "Month"])

src = input.source(hlc3, "Source")

devMult1 = input.float(1.0, "Band 1 Std Dev", minval=0.1, step=0.1)

devMult2 = input.float(2.0, "Band 2 Std Dev", minval=0.1, step=0.1)

devMult3 = input.float(3.0, "Band 3 Std Dev", minval=0.1, step=0.1)

showBand3 = input.bool(true, "Show ±3σ Band")

showCloud = input.bool(true, "Shade 2σ–3σ Volatility Extreme")

showLabel = input.bool(true, "Show VWAP Label")


// ───── Anchor detection ─────

isNew = switch anchorOpt

"Session" => timeframe.change("D")

"Week" => timeframe.change("W")

"Month" => timeframe.change("M")


// ───── Running sums ─────

var float sumPV = 0.0

var float sumV = 0.0

var float sumP2V = 0.0


if isNew

sumPV := src * volume

sumV := volume

sumP2V := src * src * volume

else

sumPV := sumPV + src * volume

sumV := sumV + volume

sumP2V := sumP2V + src * src * volume


// ───── VWAP and volume-weighted std dev ─────

vwap = sumV > 0 ? sumPV / sumV : na

variance = sumV > 0 ? sumP2V / sumV - vwap * vwap : 0.0

stdev = math.sqrt(math.max(variance, 0))


upper1 = vwap + devMult1 * stdev

lower1 = vwap - devMult1 * stdev

upper2 = vwap + devMult2 * stdev

lower2 = vwap - devMult2 * stdev

upper3 = showBand3 ? vwap + devMult3 * stdev : na

lower3 = showBand3 ? vwap - devMult3 * stdev : na


// ───── Plots ─────

vwapPlot = plot(vwap, "VWAP", color=color.yellow, linewidth=2)

u1Plot = plot(upper1, "Upper 1σ", color=color.new(color.green, 30))

l1Plot = plot(lower1, "Lower 1σ", color=color.new(color.red, 30))

u2Plot = plot(upper2, "Upper 2σ", color=color.green, linewidth=2)

l2Plot = plot(lower2, "Lower 2σ", color=color.red, linewidth=2)

u3Plot = plot(upper3, "Upper 3σ", color=color.new(color.green, 60))

l3Plot = plot(lower3, "Lower 3σ", color=color.new(color.red, 60))


// Volatility-extreme clouds between 2σ and 3σ

fill(u2Plot, u3Plot, color=showCloud ? color.new(color.green, 85) : na, title="Upper 2σ–3σ Cloud")

fill(l3Plot, l2Plot, color=showCloud ? color.new(color.red, 85) : na, title="Lower 2σ–3σ Cloud")


// ───── Live label ─────

var label lbl = na

if showLabel and barstate.islast

label.delete(lbl)

lbl := label.new(bar_index, vwap,

"VWAP " + str.tostring(vwap, "#.##") +

" 1σ: " + str.tostring(stdev, "#.##") +

" bandwidth(2σ): " + str.tostring(2 * devMult2 * stdev, "#.##"),

style=label.style_label_left,

color=color.new(color.yellow, 60),

textcolor=color.black,

size=size.small)


// ───── Alerts ─────

alertcondition(ta.crossover(close, upper2), "Cross Above Upper 2σ", "Close crossed ABOVE Upper 2σ band")

alertcondition(ta.crossunder(close, lower2), "Cross Below Lower 2σ", "Close crossed BELOW Lower 2σ band")

alertcondition(ta.crossover(close, vwap), "Reclaim VWAP", "Close reclaimed the anchored VWAP")

alertcondition(ta.crossunder(close, vwap), "Lose VWAP", "Close lost the anchored VWAP")

```


Drop your tweaks, screenshots, and questions below — that's how the library gets better.


2 Views
Brain with financial data analysis.

Inquiries at :

Important Risk Notice: Trading involves substantial risk of loss. This is educational content only—not advice. Full details here  ------------>  

Proceed only if you're prepared.

tel#: (843) 321-8514

bottom of page