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):
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:
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:
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:
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: (more…)