Tutorial

Pine Script VWMA: Volume Weighted Moving Average Guide

Learn how ta.vwma works in Pine Script v6, verify it by hand, compare it with an SMA, and build a selectable moving average input.

•11 min read

You have a moving average on your chart that has served you well, and then one afternoon a huge volume spike drags the price around while your line barely moves. Or the reverse happens: a thin, quiet bar prints an odd close and your average twitches as if it mattered. A simple moving average treats every bar in its window as equal, and that is exactly the property that makes it feel wrong in those two moments.

The volume weighted moving average, VWMA, is the small change that fixes this. It still averages the last N closes, but each close counts in proportion to the volume traded on its bar. In Pine Script v6 it is a single built-in call, ta.vwma, and this post covers what it computes, how to check it by hand, where it behaves differently from an SMA, and how to build it into a script you can actually reuse. If you want the wider family first, the moving average types post places SMA, EMA, RMA and WMA side by side.

What VWMA actually computes

An SMA adds up N closes and divides by N. A VWMA multiplies each close by that bar's volume, adds those products up, and divides by the total volume of the same N bars. In symbols, the value on the current bar is the sum of close times volume over the window, divided by the sum of volume over the window.

Two consequences follow directly from that formula. First, if every bar in the window has the same volume, the weights cancel and the VWMA equals the SMA exactly. Second, the more uneven the volume, the further the two lines separate, and the VWMA always leans toward the closes that came with heavy volume. Nothing about it is predictive. It is a different way of summarizing the same N bars, one that answers the question "what was the average price paid, weighted by how much traded", over a rolling window.

That is also why VWMA is easy to confuse with VWAP, and the two are not the same tool. VWAP accumulates from an anchor, usually the session start, and its window grows through the day. VWMA has a fixed length that slides forward one bar at a time. If you want the anchored kind, the VWAP in Pine Script post and the anchored VWAP post cover it.

The one-line version

The built-in function takes a source and a length. Here is the smallest useful script, with an SMA of the same length plotted beside it so you can see the difference immediately.

pine
//@version=6
indicator("VWMA vs SMA", overlay = true)

length = input.int(20, "Length", minval = 1)
src    = input.source(close, "Source")

vwmaValue = ta.vwma(src, length)
smaValue  = ta.sma(src, length)

plot(vwmaValue, "VWMA", color = color.orange, linewidth = 2)
plot(smaValue,  "SMA",  color = color.gray)

Load it on any liquid market and the two lines will hug each other through quiet stretches and pull apart around volume bursts. The gap is the whole story of this indicator, and we will come back to it later as something you can plot on its own.

Checking it by hand

Because a built-in can hide mistakes in your own understanding, it is worth reproducing the formula once. math.sum(source, length) returns the rolling sum of a series over the last length bars, so the definition above translates straight into code.

pine
//@version=6
indicator("VWMA by hand", overlay = true)

length = input.int(20, "Length", minval = 1)

manual  = math.sum(close * volume, length) / math.sum(volume, length)
builtIn = ta.vwma(close, length)

plot(manual,  "Manual",   color = color.blue, linewidth = 3)
plot(builtIn, "Built-in", color = color.orange)

// Should stay at zero on every bar that has a value.
plot(manual - builtIn, "Difference", color = color.red, display = display.data_window)

The two lines land on top of each other, and the difference series stays at zero in the Data Window apart from tiny floating point noise. Doing this once teaches you what the function needs. It needs a real volume series on the symbol, and it needs enough history to fill the window, so the first length - 1 bars have no value. That second point matters for anything you build on top of it, because a comparison against na is never true.

If a symbol has no volume data at all, the weights are all zero or missing and there is nothing meaningful to average. On those symbols the result comes back as na, and the right fix is a different tool, not a workaround in code.

Why the volume weighting changes the line

It helps to work through a small example with numbers. Take a three bar window with closes of 100, 101 and 102. If the volumes are 10, 10 and 10, the weights are equal and the VWMA is 101, the same as the SMA. Now change the volumes to 10, 10 and 80. The VWMA becomes (100 times 10 plus 101 times 10 plus 102 times 80) divided by 100, which is 101.7, while the SMA still says 101. Reverse the volumes to 80, 10 and 10 and the VWMA drops to 100.3.

So the line does not chase price. It chases the price at which most of the volume in the window changed hands. In practice that gives you two behaviors worth knowing.

A spike in price on thin volume moves a VWMA less than it moves an SMA, because that close carries little weight. This is the quiet-bar case from the opening, and it is the main reason people reach for VWMA on intraday charts where overnight or lunchtime bars are thin.

A move on heavy volume pulls the VWMA toward it faster than the SMA of the same length. That can make the line look more responsive at the exact moments the market is busiest, but it is not the same thing as a shorter length. The responsiveness depends on where the volume sits in the window, so it is uneven, and the line can also hold its position for a long time after a volume spike leaves the window, then jump when that bar drops out the back. A rolling window drops old bars all at once, and with volume weighting a single dominant bar leaving the window can move the line noticeably in one step. If you see a sudden shift with no new activity on the chart, check what just fell out of the window.

Reading VWMA against SMA

Plotting both lines is useful, but the comparison is easier to read as its own series. The spread between VWMA and SMA is positive when the heavy-volume bars in the window closed above the window's simple average, and negative when they closed below it. Here is a version that plots that spread as columns in its own pane and colors it by sign and by whether it is growing.

pine
//@version=6
indicator("VWMA minus SMA spread", overlay = false)

length = input.int(20, "Length", minval = 1)
src    = input.source(close, "Source")

spread = ta.vwma(src, length) - ta.sma(src, length)

growing = spread > nz(spread[1])
col = spread >= 0 ? (growing ? color.new(color.teal, 0) : color.new(color.teal, 60))
                  : (growing ? color.new(color.red, 60) : color.new(color.red, 0))

plot(spread, "Spread", style = plot.style_columns, color = col)
hline(0, "Zero", color = color.gray)

The spread is in price units, so its size depends on the symbol. If you want something comparable across markets, divide by the SMA to express it as a fraction of price. The sign is the part that carries meaning here: it tells you whether volume has been concentrated on the high side or the low side of the window's range. It describes the recent past and nothing else, so treat it as a way to read what already happened, not as an instruction to do anything.

A VWMA crossover without the usual traps

The classic use is a crossover, and it inherits every crossover problem you already know. A cross of price over the VWMA, or of a fast VWMA over a slow one, is a trigger condition like any other. The moving average crossover guide covers the pattern in general. The VWMA-specific points are these.

Use ta.crossover and ta.crossunder rather than a hand-rolled comparison, so the condition fires on the single bar where the cross happens and not on every bar that follows. Let the condition be evaluated on the confirmed bar. A cross that exists in the middle of a live bar can vanish by the close, which is how alerts end up firing on bars that later look like nothing happened, a problem the barstate.isconfirmed post walks through. And make sure the volume source is not the thing repainting: on a live bar, volume is still accumulating, so the VWMA on the last bar changes as ticks arrive even though earlier bars stay fixed.

pine
//@version=6
indicator("VWMA fast/slow cross", overlay = true)

fastLen = input.int(10, "Fast length", minval = 1)
slowLen = input.int(30, "Slow length", minval = 2)

fast = ta.vwma(close, fastLen)
slow = ta.vwma(close, slowLen)

crossUp   = ta.crossover(fast, slow)
crossDown = ta.crossunder(fast, slow)

confirmedUp   = crossUp   and barstate.isconfirmed
confirmedDown = crossDown and barstate.isconfirmed

plot(fast, "Fast VWMA", color = color.orange)
plot(slow, "Slow VWMA", color = color.blue)

plotshape(confirmedUp,   "Cross up",   style = shape.triangleup,   location = location.belowbar, color = color.teal, size = size.tiny)
plotshape(confirmedDown, "Cross down", style = shape.triangledown, location = location.abovebar, color = color.red,  size = size.tiny)

alertcondition(confirmedUp,   "VWMA cross up",   "Fast VWMA crossed above slow VWMA")
alertcondition(confirmedDown, "VWMA cross down", "Fast VWMA crossed below slow VWMA")

Notice that the fast length input has minval = 1 and the slow one minval = 2. Nothing enforces fast being smaller than slow, so it is worth a line in your own checklist when you tune the inputs: swap them and the markers swap meaning.

Letting the user pick the average type

A common next step is a script where the moving average type is an input, so you can flip between SMA, EMA and VWMA on the same chart without editing code. The trap is putting the ta.* calls inside a switch or if. Functions that keep history need to run on every bar, and calling them conditionally gives inconsistent results and a compiler warning. Compute every candidate each bar and choose which one to use afterwards.

pine
//@version=6
indicator("Selectable moving average", overlay = true)

maType = input.string("VWMA", "Type", options = ["SMA", "EMA", "WMA", "VWMA"])
length = input.int(20, "Length", minval = 1)
src    = input.source(close, "Source")

smaV  = ta.sma(src, length)
emaV  = ta.ema(src, length)
wmaV  = ta.wma(src, length)
vwmaV = ta.vwma(src, length)

ma = switch maType
    "SMA"  => smaV
    "EMA"  => emaV
    "WMA"  => wmaV
    => vwmaV

plot(ma, "Moving average", color = color.orange, linewidth = 2)

Every average is calculated on every bar, and the switch only picks a value that already exists. The cost of computing four averages is small next to the clarity you gain. The same pattern is how you would retrofit VWMA into an existing script that hard-codes ta.sma: add the input, compute the alternatives, and route the one variable that the rest of the script reads. The input types post covers input.string with options in more detail.

Common mistakes

The most frequent mistake is using VWMA on a symbol where volume is unreliable or absent. The calculation is only as meaningful as the volume series underneath it. On markets where the reported volume covers only one venue or is a rough proxy for activity, the weights reflect that quirk, and the VWMA inherits it.

The second is expecting VWMA to be faster than an SMA of the same length. It is not faster in any consistent sense. It is differently weighted, and on a market with steady volume it is nearly identical.

The third is comparing a VWMA of one length against an SMA of another and reading the gap as volume information. If the lengths differ, the gap mixes two effects, window size and weighting, and you cannot separate them. Keep the lengths equal when the spread is the point.

The fourth is forgetting the warmup. A 200 bar VWMA has no value on the first 199 bars, and a condition built on it simply never fires there. If a script seems to ignore the left edge of the chart, this is usually why. The chart also only holds so much history, so a very long length on a short intraday window can leave you with few usable bars.

Testing a change from SMA to VWMA

Once the script runs, the natural question is whether swapping the average changed how a strategy behaves. VWMA is a plain function swap, so the experiment is clean: change one call, keep everything else the same, and compare the two runs in TradingView's Strategy Tester. Keep the same inputs, the same date range and the same cost settings on both runs, otherwise you are comparing more than one change at once. The slippage and commission post explains why the cost settings matter more than most people expect, and a result from one symbol and one window says little by itself.

If you would rather not edit every ta.sma call by hand across a long script, this is a task suited to a describe-and-review workflow. In the PineScripter editor you can ask for the swap in plain English, for example "replace the SMA with a VWMA of the same length and keep it as an input", and the Code Changes view shows the affected lines so you can confirm that only the averaging calls moved. The code is checked for compile errors before you paste it into TradingView, which removes the copy-the-error-back-and-forth loop for something as mechanical as this.

What to take from this

VWMA is a rolling average of the last N closes, weighted by volume, and ta.vwma(source, length) is all it takes in v6. It matches the SMA when volume is flat and departs from it when volume is uneven, which makes the spread between the two a compact way to see where recent volume sat inside the window. It needs real volume data, it has a warmup of length - 1 bars, and on a live bar it still moves because volume is still arriving. Build it with the selectable pattern above and you can try it in any script you already have.

Disclaimer: PineScripter is a coding tool for Pine Script development. It does not provide financial advice and does not guarantee trading profits. Always backtest strategies thoroughly and understand the risks before live trading.

Frequently asked questions

What is the Pine Script function for VWMA?

ta.vwma(source, length) returns the volume weighted moving average of the source over the last length bars.

Is VWMA the same as VWAP?

No. VWAP accumulates from an anchor such as the session start, so its window grows. VWMA uses a fixed length that slides forward one bar at a time.

When does VWMA equal the SMA?

When every bar in the window has the same volume, the weights cancel and the two lines match exactly.

Why is my VWMA na?

The first length minus 1 bars have no value because the window is not full yet, and a symbol with no volume data gives nothing to weight by.

Can I check ta.vwma by hand?

Yes. math.sum(close * volume, length) / math.sum(volume, length) reproduces it.

Describe your indicator in plain English and get Pine Script that compiles →


Disclaimer: PineScripter is a coding tool for Pine Script development. It does not provide financial advice and does not guarantee trading profits. Always backtest strategies thoroughly and understand the risks before live trading.