Guide

Anchored VWAP in Pine Script: Swings, Session, Bands

A VWAP is only as meaningful as the bar it starts from. Five anchors, one set of bands, and a line that does not move once the swing is confirmed.

•12 min read

The volume-weighted average price is a simple object. From some starting bar to now, add up price times volume, divide by volume, and you have the average price of everything traded since that start. The hard part is the start. A session VWAP answers one question. It says nothing about the average since the weekly open, since the last swing low, or since the last bar that traded several times normal volume. Anchored VWAP Engine keeps those averages on the chart at the same time, and it refuses to draw a swing average until the swing is actually confirmed.

Short answer

Anchored VWAP Engine plots the volume-weighted average from the UTC session, from the week, from the last confirmed swing high, from the last confirmed swing low, and, if you turn it on, from the last volume climax. One of those anchors carries standard-deviation bands. A swing average appears only after ta.pivothigh or ta.pivotlow confirms, it is computed from the swing bar itself, and it does not move once drawn. Triggers fire on closed bars: a reclaim of the swing-low average, a loss of the swing-high average, and a rejection at an outer band. The label on the last bar counts how many of the active averages price is above.

Key facts

  • The default source is hlc3, not close. Every anchor uses that same source.
  • Session VWAP resets on the UTC day and is plotted only on intraday charts. On a daily chart every bar would be its own session, so the session average is left blank.
  • The weekly VWAP resets with the TradingView week and is plotted on every timeframe.
  • Swing averages use a length of 10 by default. They appear 10 bars after the swing, because that is when the pivot exists.
  • A volume climax is a bar whose volume is at least 3 standard deviations above a 100-bar average. That anchor is off until you enable it.
  • Bands default to the weekly anchor, at 1 and 2 standard deviations, and they wait until the anchor is at least 5 bars old.
  • On a daily chart or higher, asking for session bands falls back to the week.
  • A reclaim or a loss needs the default of 3 closes on the other side of the swing average before the cross counts.
  • A band rejection is the first bar of a run that tags the outer band and closes back inside the inner band.
  • The last-bar label reads "Above N of M VWAPs". All of them is broad strength on this definition. None is broad weakness. It is a count, not a forecast.
AnchorWhen it startsColorOn by default
SessionUTC day change, intraday charts onlyBlueYes
WeekWeek change, every timeframeOrangeYes
Last swing high10 bars after the high, from that highRedYes
Last swing low10 bars after the low, from that lowGreenYes
Volume climaxThe bar after a 3-standard-deviation volume barPurpleNo

Anchoring is a subtraction, not a new kind of average

ta.vwap already knows how to anchor to a session or to a time change, and the VWAP guide covers that function. The library indicator does the arithmetic itself, because it needs several anchors at once, including two that start in the past on a bar that was not special until later. The identity is the same either way. Keep a running total of price times volume and a running total of volume. An anchor is the pair of totals as they stood just before the starting bar. Today's VWAP is the difference of those totals, divided.

Doing it with running totals means a new anchor does not restart a loop over old bars. When a swing confirms, the script remembers the totals from the bar before the swing and every later bar is one subtraction. The same stores also feed the standard deviation: a third running total, price squared times volume, gives the second moment, and the deviation is the square root of that moment minus the square of the mean. Bands are that deviation times the multiples you set, default 1 and 2.

If the volume since the anchor is zero, the average is na rather than a division by zero. The first bar of a new anchor is also plotted as a break, so the new line does not join the old one with a vertical stroke. You see a gap at each session open, each week open, and each new swing, which is what you want. A continuous line across a reset would be a drawing error, not a price.

pine
//@version=6
indicator("Anchored VWAP, one anchor", overlay = true)

src = input.source(hlc3, "Source")
float v = nz(volume)
float cumV = ta.cum(v)
float cumPV = ta.cum(src * v)

// True on the first bar of the window you care about.
anchor = ta.change(time("D")) != 0
var float v0 = na
var float pv0 = na
if anchor
    v0 := nz(cumV[1])
    pv0 := nz(cumPV[1])

float dv = cumV - nz(v0)
float vwap = na(v0) or dv <= 0 ? na : (cumPV - pv0) / dv
plot(vwap, "Session VWAP", color.blue, 2)

The library script keeps five copies of that state, as a small user-defined type with the three totals and the bar the anchor began. Session and week copy the totals from the previous bar when the day or the week changes. The swing copies are the interesting ones, because the anchor bar and the bar that discovers it are not the same bar.

A swing VWAP starts late and then stays put

A swing high is only a swing high once enough bars have printed to its right. The default is 10. On the bar the pivot confirms, the script looks back to the totals as they stood at the end of the bar before the swing, and it stores those as the anchor. Every later value of that line is the average of trading from the swing bar forward. The confirmation bar itself is left blank so the new line does not connect backward to the previous swing's line.

This is the property worth checking on a chart. Scroll to a swing that confirmed a while ago and note the line. Come back after more bars have printed. The older part of the line should be in the same place. New bars extend it. They do not rewrite it. That is possible because the anchor totals are fixed at confirmation. A script that recomputes "the last swing" using the current bar's right-hand neighbors will draw a different average every time the swing set changes, and on historical bars that redraw is invisible.

The swing length is also the delay. At 10, you do not have a swing-low VWAP until 10 bars after the low. Shortening it gives you an earlier line and more, smaller swings. Lengthening it does the opposite. The line is still computed from the swing bar, not from the confirmation bar, so the delay changes when the line appears. It does not change which volume is included once the line exists.

Session, week, and the climax you have to turn on

The session anchor follows the UTC day, using a change in time("D"). It is plotted only when the chart is intraday. On a daily chart or higher the session average would reset every bar and tell you nothing, so it is na. The weekly anchor has no such guard. It resets on time("W") and is drawn on every timeframe the indicator is applied to.

Both time tests are computed on every bar before they are combined with anything else. Pine Script v6 stops evaluating an or expression at the first true operand, and a ta.* call that is skipped on some bars loses the history it needs. Writing the day change and the week change into their own variables, every bar, is what keeps the resets stable. The lazy-evaluation guide is the longer version of that bug. It is an easy one to reintroduce if you inline the conditions later.

The climax anchor is the one that is off by default. A climax is volume at least 3 standard deviations above its 100-bar mean. When that bar prints, the anchor is set to the totals from the bar before it, so the climax bar is included in the average. Three standard deviations is a rare bar by construction. If you lower the threshold you will re-anchor often, and the purple line will jump. Leave it off until you have a reason to ask where the average has been since the last unusually heavy bar.

Bands, reclaims, and the stack on the last bar

One anchor wears the bands. The default is the week. You can move them to the session, the last swing high, or the last swing low, or turn them off. If you ask for session bands on a daily chart, the script uses the week instead, for the same reason the session line itself is blank there. Bands also wait. Until the chosen anchor is at least five bars old, a deviation is mostly noise, and the bands stay na. The inner and outer zones are filled, red above and green below, so the stretch is visible without four extra lines competing with price.

Two events are defined tightly enough to plot. A reclaim of the swing-low VWAP is a confirmed bar that closes above it, after the previous bar was at or below it, and after at least three closes had already been on the other side. The default of three is the input "Reclaim needs closes on the other side". Losing the swing-high VWAP is the mirror. A one-bar dip through the line does not qualify, because the count of closes on the other side never reached the threshold. That is a filter against chatter, and it will hide crosses you might have wanted. Change the number if you want those crosses. Do not read the absence of a triangle as a statement that the cross was unimportant.

A rejection is narrower still. The bar's high has to reach the outer upper band, the previous bar must not have reached it, and the close has to finish back inside the inner band. The lower band mirrors that. Only the first reach of a run counts, so a grind along the band marks once. Circles are those bars. They are not a claim that the band will hold.

The remaining object is a single label, moved on the last bar rather than created again. It counts the anchors you have enabled, ignores any that are still na, and reports how many of them the close is above. The label is green when price is above all of them, red when it is above none, and grey in between. "Above 4 of 4 VWAPs" means the close is on one side of four averages. It does not mean the move will continue.

Where this sits in the library

Anchored VWAP Engine opens for any signed-in account, alongside Liquidity Sweeps. Volume Pulse, Delta Flow, and Volume Profile Suite are included in every paid plan. Mini is $9.99 a month, Pro is $19.99, Ultra is $39.99. Plans differ in credits, context, and how many live alerts you can connect. They do not differ in which paid indicators you can open.

Inside PineScripter the script runs on crypto spot data: 308 markets from Binance public data, 14 timeframes from 1 minute to 1 week, up to 5,000 bars. There is no monthly timeframe in the app. Paste the script into TradingView when you want a market, or a timeframe, the app does not carry. The alertcondition calls cover the reclaim, the loss, and both band rejections, and they use the same confirmed-bar conditions as the shapes.

If you already have a session VWAP from ta.vwap and you want the swing anchors added, say that in the editor instead of starting over. The running-total form and the lazy-evaluation split are the two pieces a general chat assistant most often gets wrong, usually by calling ta.vwap once and describing the other anchors in a comment. The library script is there so you can read a version that does all five, then change the swing length or the band anchor and see the diff.

The editor answers from the Pine v6 manual, which is where ta.vwap and the time anchors are defined

Frequently asked questions

How is an anchored VWAP different from a session VWAP?

The math is the same. The anchor is different. A session VWAP starts at the session open. An anchored VWAP starts at a bar you chose: a week, a confirmed swing, or a volume climax. Anchored VWAP Engine draws several of those at once.

Why does the swing VWAP show up late?

The swing is a pivot with a default of 10 bars on each side. The average cannot start until that pivot exists, so the line appears 10 bars after the high or low. It is still computed from the swing bar, and the older part of the line does not move as new bars arrive.

Which anchor do the bands use?

The weekly VWAP, unless you change the "Bands on" input. They are 1 and 2 standard deviations by default, and they stay off until that anchor is at least five bars old. On a daily chart, a request for session bands is drawn on the week instead.

What does the label "Above N of M VWAPs" mean?

On the last bar, M is how many anchors are enabled and have a value, and N is how many of those the close is above. It is a count of current location. It is not a prediction.

Does a triangle mean I should enter?

No. A triangle marks a confirmed close back through a swing VWAP after enough closes on the other side, or you are looking at a circle, which marks the first rejection at an outer band. Both describe the bar that just closed.

The practical takeaway

Pick the anchor before you argue about the line. A session average, a weekly average, and an average since the last confirmed swing are three different questions, and this indicator prints them as separate lines so you can see which question you asked. The swing line waits until the swing is real, and then it stays where it was drawn. Bands belong to one anchor, reclaims require a run of closes on the other side, and the last-bar label is a count of how many averages price is above. None of those objects is a forecast. They are the average price since a bar you can name.

Anchored VWAP Engine is in the indicator library. The running totals, the five anchors, and the band switch are easier to read than to reconstruct, and PineScripter opens the script in the editor so a change to the swing length or the band anchor is a diff you can review.

Sources

Related reading: the session VWAP built with ta.vwap, liquidity pools on the same swings, why v6 or skips later calls, volume profile and the point of control.

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.