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
Useta.vwap()on your preferred price source to monitor volume-weighted trends, build dynamic bands, or trigger signals.
At a Glance
Section titled “At a Glance”| Difficulty | Beginner |
| Time to implement | 10-15 min |
| Category | Strategy Basics |
Quick Actions
Section titled “Quick Actions”Quick Start
Section titled “Quick Start”//@version=5indicator("Session VWAP", overlay=true)
vwapValue = ta.vwap(hlc3)plot(vwapValue, "VWAP", color=color.orange, linewidth=2)Why It Matters
Section titled “Why It Matters”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.
What You’ll Learn
Section titled “What You’ll Learn”- 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
Quick Reference
Section titled “Quick Reference”| Call | Purpose |
|---|---|
ta.vwap(source) | Returns intraday VWAP for the provided source series. |
hlc3 | Common VWAP source (high + low + close) / 3. |
input.source / input.float | Parameterise the VWAP source and band offset. |
plot() | Display VWAP and its bands. |
barcolor() | Highlight bars when price is outside VWAP bands. |
Implementation Blueprint
Section titled “Implementation Blueprint”-
Pick the source to average
Usehlc3,close, or any series you want VWAP to track.sourceInput = input.source(close, "VWAP source")vwapValue = ta.vwap(sourceInput) -
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) -
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)
Example Playbook
Section titled “Example Playbook”//@version=5indicator("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)- 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.
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”- 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().
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”Why is VWAP flat on some bars?
Section titled “Why is VWAP flat on some bars?”If volume is zero, that bar contributes nothing—VWAP remains unchanged. Check the data feed or switch resolutions that aggregate more volume.
Can I reset VWAP at a custom time?
Section titled “Can I reset VWAP at a custom time?”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.
Key Takeaways
Section titled “Key Takeaways”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.
Keep Going
Section titled “Keep Going”- Open AI Editor to apply the pattern with guided assistance
- Browse PineScript Examples for ready-to-run templates
- Connect to Live Trading when you are ready to automate
Adapted from tradingcode.net, optimised for Algo Trade Analytics users.