VWAPALOOZA

UPDATE 23 Feb 2009: Fixed an error in the formatting. Added “Volume Increment”. Changed Incremental and Rolling VWAP’s to calculate for all days on chart, not just current day.

I asked for some indicator requests on Twitter. The most common response was for VWAP, or Volume Weighted Average Price. Think or Swim has a built-in VWAP indicator, but it is a black box. I like my boxes to have source code. Honestly, I’m not quite sure what the built-in “VWAP” indicator is even plotting. In my testing I couldn’t figure it out. It seems to do different things when plotted on daily vs. intraday charts. There is another built-in indicator called “CumulativeVWAP” that is more useful that I’ll discuss below.

There are many ways to define a VWAP. The “standard” way is to multiply each trade by the corresponding trade volume, sum them all, then divide by the total volume traded, like so (from the wikipedia article):

VWAP Formula

This is typically done over a 1 day timeframe.

To remain true to this definition, we would have to calculate VWAP from the tape itself, using time and sales data. Think or Swim does not (yet) support this. The smallest duration we have to work with is the 133 tick chart. So if you define Pj = (bar high + bar low)/2 and use the formula above for a given day, you should get a plot of the VWAP as it was calculated during the day at each corresponding time. As the day starts out, VWAP responds quickly to price changes, but by the end of the day the volume of each bar becomes small compared to total volume. It’s basically an oscillation that gets more damped as the day goes on (barring extreme changes in price or volume). The final value of VWAP on this tick chart at the close should be near to the actual value as calculated from the individual trades. When I plot this VWAP value (in yellow) along with the value of the built-in “CumulativeVWAP” indicator (in black) with timeframe set to “DAY” (hiding the “upper band” and “lower band”), the two curves lie on top of each other:

vwapvscumvwap

Looks good. However, when you move to a larger timeframe, say 30 minute bars, then my Thinkscript (now gray line) loses resolution compared to the “CumulativeVWAP” indicator (now white dots). The “CumulativeVWAP” still shows the same values as in the fine scale 133 tick chart:

vwapvscumvwap_30mins

This leads me to believe that the “CumulativeVWAP” indicator is calculated from time and sales data server-side at ToS. So if you just want a standard VWAP indicator, use “CumulativeVWAP” instead of calculating it yourself.

Now, I’ve seen others (including Richard at Move the Markets) use a type of rolling VWAP. To me, the idea would be that intraday traders near the end of the day can be far removed from the action at the open. They probably have already closed positions from the early morning and are likely to be trading from a different perspective. Calculating a rolling VWAP would let you find a more recent volume-weighted consensus of value. So I wrote a simple “Rolling VWAP” indicator that looks back N number of bars. In the limit as N goes to the total number of bars so far in a day, my “Rolling VWAP” value approaches the “CumulativeVWAP” value (remembering that my calculation loses resolution as your chart timeframe gets larger).

BUT THAT’S NOT ALL!! Order now and you’ll receive an “Incremental VWAP” calculation at no extra charge! The “Incremental VWAP” calculates VWAP over a given period (say N bars). It then waits for N more bars to go by, and then calculates a new VWAP for that most recent N bar period. Here’s all three of my VWAPs– Full Day (cyan dash), Rolling (solid yellow) and Incremental (purple dash)–along with the built-in “CumulativeVWAP” (solid gray) on a 1 min chart of ES from today:

vwap_pro

UPDATE 23 Feb 2009: I also added the option to specify the number of shares as an increment for VWAP rather than just number of bars.

There are many different ways to use VWAP, and hopefully one of these will help you in your trading. The “Incremental VWAP” contains code that uses a counter variable that goes from 1 to N and then resets to 1, so look at the source if that interests you.

Here is the code:

# VWAPALOOZA
# By Prospectus @ http://readtheprospectus.worspress.com
#
# This Thinkscript calculates four different types of VWAP:
# OneDay (standard type), Rolling (N bar lookback), Bar
# Incremental (discrete N bar periods) and Volume Incremental
# (discrete N-shares-traded periods).
#
declare upper;
#
# Check if we are on the current day:
#
def istoday = if getDay() == getLastDay() then 1 else 0;
#
# Input Rolling / Incremental period, VWAP type, and
# Averaging method ( (H+L)/2 or Close):
#
input Period = 20;
def n = Period;
input VolIncrement=20000;
def v=VolIncrement;
input VWAPType = {Rolling, BarIncremental, VolIncremental, default OneDay};
def type;
switch (VWAPType){
case Rolling:
type = 1;
case BarIncremental:
type = 0;
case OneDay:
type = 2;
case VolIncremental:
type=3;
}
input AverageMethod = {CLOSE, default H_L};
def num;
switch (AverageMethod){
case CLOSE:
num = close;
case H_L:
num = (high + low) / 2;
}
#
# Define volume for today only (other days zero):
#
def todayvol = if istoday then volume else 0;
#
# Calculate the P*V term for VWAP:
#
def pv = num * todayvol;
#
# Code for BarIncremental counter:
#
rec k = if k[1] == n then 1 else k[1] + 1;
#
# Code for VolIncremental volume sum:
#
rec volsum=if volsum[1]>v then volume else volsum[1]+volume;
#
# Code for VolIncremental price*volume:
#
rec pvsum =if volsum[1]>v then num*volume else pvsum[1] + num*volume;
#
# Now define the VolIncremental to display:
#
rec volinc=if volsum[1]==0 then num else if volsum[1]>v then pvsum[1]/volsum[1] else volinc[1];
#
# Now the final VWAP calculations:
#
rec calcVWAP = if type == 2 then TotalSum(pv) / TotalSum(todayvol) else if type == 1 then sum(num*volume, n) / sum(volume, n) else if type==3 then volinc else if type==0 and k == n then sum(num*volume, n) / sum(volume, n) else calcvwap[1];
#
# Define the plot:
#
plot VWAPALOOZA = calcvwap;
#
# Formatting:
#
VWAPALOOZA.setdefaultcolor(color.white);
VWAPALOOZA.setstyle(curve.long_dash);
VWAPALOOZA.setlineweight(3);

Tags: , , , ,

10 Responses to “VWAPALOOZA”

  1. ava Says:

    Is it possible to add someting like 5 day vwap or weekly vwap?

  2. Prospectus Says:

    Yes, it is. I’ll put that on my pile of things to do. I need a few clones of myself X_X

  3. qew Says:

    Can you add Standard deviations bands (1st, 2nd, 3rd) to this rolling vwap ?

  4. Prospectus Says:

    I can, but just flat don’t have the time right now. I need a deputy thinkscript coder X_X

  5. qew Says:

    I tried to do something on my own it works but I have trouble calculating the Vi/V for the variance so my SD are wrong …. Vi being the volume at a particular price and V total volume for the lookback period …. now how to calculate Vi ?

  6. Prospectus Says:

    Volume at price is a big problem. Thinkscripter made a big script that does a reasonable job at it, but we don’t have an accurate server-side definition, and without them streaming us tick-by-tick data we can’t truly recreate it locally.

    The only suggestion I’d have is to look at the volume of the current bar vs total lookback volume, but that’s not really what you want to do, I know…

  7. Dan Says:

    I am new to trading and just getting started with a small amount of money I have saved prior to losing my job. I downloaded your DDE Template and I see that VWAP drop down does not work. Do I need to add specific fields or is VWAP no longer supported in TOS?

  8. Prospectus Says:

    I don’t know, I’ll have to look at it

  9. Sean Smith Says:

    Is it possible to modify the rolling VWAP code to have the rolling VWAP anchored to the day open, like the regular VWAP is? I’m looking at constructing a VWAP that is more responsive to intraday price fluctuations, i.e. a shorter-time frame VWAP (30 min, 60 min, etc.) but I would like it to begin calculating at the day’s open so it only reflects volume traded during the current session.

    Thanks for your work!!!

  10. Lucian Trif Says:

    Any way to add a week vwap or a year vwap ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: