I am trying to create an indicator that takes all the ticks within a bar and makes a VWAP out of them. The purpose is to have a dot in the middle of the bar where the VWAP sits.
As a first attempt, I coded it form gomCD, separating Buy and Sell volume. It worked but there was too little difference between buy and sell VWAP for the amount of messing up of charts. Hence, I would like to replace it with a version that does not differentiate between buy/sell volume.
So far, my code is the following:
However, like this I sometimes get 3M bars with non-zero volume that have no VWAP (dot displayed). At the same time I sometimes get a VWAP that lies out of the High-Low range. NT Support kindly told me they cannot "debug custom indicators". It's obvious I am doing something wrong but can't figure out what. Including the screen of what is happening.
Any help is greatly appreciated.
EDIT: After writing a new version which stored the Last price and volume from OnMarketData() and calculating the VWAP in OnBarUpdate(), I found out that OnMarketData() does not fire with each tick of the replay. I knew that OnMarketDepth() does not fire on each update of the book on replay, but I had no idea that OnMarketData() is unrealiable as well. NT know about this stuff, I sent them several emails regarding this anomaly, without response. So in the end, I hacked the good ol' gomCD. Gomi, you are King. Without gomCD, ninja is dead.
Last edited by Eubie; October 17th, 2011 at 09:09 AM.
First, if you use OnMarketData(), the outcome depends on the data stream of you data provider. I personally use Interactive Brokers, and they use the fields e.price and e.Volume to transport other information. Often I just get a zero value for price and a huge value for volume, probably representing some sort of cumulated volume.
There is no need to use OnMarketData(), you can all do in OnBarUpdate(). Also I am not sure that OnMarketData() will recognize FirstTickOfBar, as this again is a value related to OnBarUpdate().
Third, your VWAP formula is false. You cannot simply up the price, but you need to add up the weighted price, so you sumSeries should include
or in OnBarUpdate() simply
The following 2 users say Thank You to Fat Tails for this post:
The e.Price * e.Volume was obviously a mistake out of rushing too much. The indicator is running nicely (the same as hacked gomi) but I guess faster. Thank you very much Fat Tails. I learned a lot from those few lines of code.
The following user says Thank You to Eubie for this post:
You would need to set your indicator to CalculateOnBarClose = false. The VWAP will only be displayed on real-time data but not on historical data. If you also want to display an intraday VWAP on historical data, this would would require to load a secondary bar series of 1-tick bars and then perform the calculations by using the secondary bar series.
You can use high + low + open + close as a proxy for the VWAP. It is easier to calculate and does not make much of a difference.