Skip to content

Get the Volume-Weighted Average Price (VWAP) in Pine Script

Get the Volume-Weighted Average Price (VWAP) in Pine Script

Section titled “Get the Volume-Weighted Average Price (VWAP) in Pine Script”

TL;DR
Use ta.vwap() on your preferred price source to monitor volume-weighted trends, build dynamic bands, or trigger signals.

DifficultyBeginner
Time to implement10-15 min
CategoryStrategy Basics
//@version=5
indicator("Session VWAP", overlay=true)
vwapValue = ta.vwap(hlc3)
plot(vwapValue, "VWAP", color=color.orange, linewidth=2)
Heads-up. VWAP resets each trading day. On assets without clear sessions (crypto), the reset happens at midnight exchange time.

VWAP is a popular benchmark for intraday trading—it reveals the average price weighed by traded volume. Traders use it to judge whether the current price is expensive or cheap relative to the day’s flow, or to anchor breakout bands. Pine Script’s ta.vwap() keeps you aligned with the same tool used by desks and algorithms.

  • Calculating VWAP from different price sources or custom data
  • Building upper/lower percentage bands around VWAP
  • Reacting when price leaves the VWAP zone (colour bars, alerts, etc.)
  • Handling instruments with 24/7 trading sessions
CallPurpose
ta.vwap(source)Returns intraday VWAP for the provided source series.
hlc3Common VWAP source (high + low + close) / 3.
input.source / input.floatParameterise the VWAP source and band offset.
plot()Display VWAP and its bands.
barcolor()Highlight bars when price is outside VWAP bands.
  1. Pick the source to average
    Use hlc3, close, or any series you want VWAP to track.

    sourceInput = input.source(close, "VWAP source")
    vwapValue = ta.vwap(sourceInput)
  2. Add bands or derived metrics
    Build offsets or compare VWAP against other signals.

    bandPct = input.float(0.1, "Band %", step=0.05)
    upperBand = vwapValue * (1 + bandPct / 100)
    lowerBand = vwapValue * (1 - bandPct / 100)
  3. React visually or with alerts
    Colour bars, plot lines, or emit notifications when price leaves the VWAP zone.

    barcolor(close > upperBand ? color.green :
    close < lowerBand ? color.red : na)
//@version=5
indicator("VWAP bands", overlay=true)
src = input.source(hlc3, "VWAP source")
bandPct = input.float(0.25, "Band offset (%)", step=0.05, minval=0)
vwapValue = ta.vwap(src)
upperBand = vwapValue * (1 + bandPct / 100)
lowerBand = vwapValue * (1 - bandPct / 100)
plot(vwapValue, "VWAP", color=color.orange, linewidth=2)
plot(upperBand, "Upper band", color=color.new(color.blue, 0))
plot(lowerBand, "Lower band", color=color.new(color.blue, 0))
bgcolor(close > upperBand ? color.new(color.green, 85) :
close < lowerBand ? color.new(color.red, 85) : na)
Why this works.
  • Using `hlc3` smooths out single-price noise by averaging high/low/close.
  • Bands emphasise how far price has drifted from the session VWAP.
  • Colour cues bring attention to potential overbought (above band) or oversold (below band) states.
  • VWAP ignores post-session data. On assets with overnight sessions, consider alternative anchors or custom reset logic.
  • Volume gaps (e.g., weekends on futures) can flatten VWAP temporarily—pair with a moving average if it bothers you visually.
  • VWAP requires volume; for instruments without volume (indices from some feeds), the function returns na.
  • For multi-day VWAP, accumulate volume and price manually instead of relying on ta.vwap().

If volume is zero, that bar contributes nothing—VWAP remains unchanged. Check the data feed or switch resolutions that aggregate more volume.

ta.vwap() resets at session boundaries. To reset at another time, accumulate price × volume yourself and divide by cumulative volume, resetting whenever your condition is met.

  • ta.vwap() gives you the session’s volume-weighted average price for any source series.
  • Offsetting VWAP with percentage bands creates simple overbought/oversold zones.
  • Combine visual cues (plots, bar colours) and alerts to capitalise on VWAP deviations.
  • Watch for volume-less instruments or custom session requirements when using VWAP.

Adapted from tradingcode.net, optimised for Algo Trade Analytics users.