NexusFi: Find Your Edge


Home Menu

 





Intrabar VWAP


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Eubie with 2 posts (1 thanks)
    2. looks_two Fat Tails with 2 posts (2 thanks)
    3. looks_3 ejtrader with 2 posts (0 thanks)
    4. looks_4 Quick Summary with 1 posts (0 thanks)
    1. trending_up 4,391 views
    2. thumb_up 3 thanks given
    3. group 3 followers
    1. forum 6 posts
    2. attach_file 2 attachments




 
Search this Thread

Intrabar VWAP

  #1 (permalink)
 Eubie 
Prague, Czech republic
 
Experience: Beginner
Platform: NinjaTrader/ZF
Trading: ES, TF
Posts: 131 since Jul 2010
Thanks Given: 267
Thanks Received: 74

Hello everybody,

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:

 
Code
	
private double SumPrices;
private double SumVolume;

...

protected override void OnMarketData(MarketDataEventArgs e)
{
if ( FirstTickOfBar ){
	SumPrices = 0;
	SumVolume = 0;
	}
	
SumPrices += e.Price;
SumVolume += e.Volume;

DrawDot( "IntrabarVWAP" + CurrentBar, false, 0, SumPrices / SumVolume, Color.Black);
}
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.
Regards,
Daniel.

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.

Attached Thumbnails
Click image for larger version

Name:	Intrabar_VWAP_error.PNG
Views:	280
Size:	20.3 KB
ID:	52214  
Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
ZombieSqueeze
Platforms and Indicators
NexusFi Journal Challenge - April 2024
Feedback and Announcements
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
Request for MACD with option to use different MAs for fa …
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Retail Trading As An Industry
58 thanks
Battlestations: Show us your trading desks!
55 thanks
NexusFi site changelog and issues/problem reporting
48 thanks
What percentage per day is possible? [Poll]
31 thanks
GFIs1 1 DAX trade per day journal
29 thanks

  #3 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102



Eubie View Post
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.
Regards,
Daniel.

There are several problems with your code.

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

 
Code
sumPriceVolume  +=  e.Price * e.Volume;
sumVolume += e.Volume;
or in OnBarUpdate() simply

 
Code
if (FirstTickOfBar)
{
      sumPriceVolume = Close[0] * Volume[0];
      sumVolume = Volume[0];
}
else
{
      sumPriceVolume += Close[0] * (Volume[0] - sumVolume);
      sumVolume = Volume[0];
}
if (sumVolume> 0)
   DrawDot ("IntrabarVWAP" + CurrentBar, false, 0, sumPriceVolume / sumVolume, Color.Black);
Not tested.

Reply With Quote
The following 2 users say Thank You to Fat Tails for this post:
  #4 (permalink)
 Eubie 
Prague, Czech republic
 
Experience: Beginner
Platform: NinjaTrader/ZF
Trading: ES, TF
Posts: 131 since Jul 2010
Thanks Given: 267
Thanks Received: 74

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.

Started this thread Reply With Quote
The following user says Thank You to Eubie for this post:
  #5 (permalink)
 ejtrader 
Portland, OR
 
Experience: Intermediate
Platform: Sierra Chart
Trading: ES
Posts: 269 since Jan 2011
Thanks Given: 327
Thanks Received: 180

Eubie/Harry - I tried to use this exact code - and I get the dots plotted on the close of the bar - instead of calculated VWAP for the bar.

Anything wrong with the code or should I be doing this with Gomi framework?

Thanks.

 
Code
        protected override void OnBarUpdate()
        {
			if (FirstTickOfBar)
			{
      			sumPriceVolume = Close[0] * Volume[0];
      			sumVolume = Volume[0];
			}
			else
			{
      			sumPriceVolume += Close[0] * (Volume[0] - sumVolume);
      			sumVolume = Volume[0];
			}

			if (sumVolume> 0)
   			DrawDot ("IntrabarVWAP" + CurrentBar, false, 0, sumPriceVolume / sumVolume, Color.Black);
        }

Attached Thumbnails
Click image for larger version

Name:	NT_Dots.jpg
Views:	186
Size:	26.7 KB
ID:	61492  
Reply With Quote
  #6 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102


ejtrader View Post
Eubie/Harry - I tried to use this exact code - and I get the dots plotted on the close of the bar - instead of calculated VWAP for the bar.

Anything wrong with the code or should I be doing this with Gomi framework?

Thanks.

 
Code
        protected override void OnBarUpdate()
        {
            if (FirstTickOfBar)
            {
                  sumPriceVolume = Close[0] * Volume[0];
                  sumVolume = Volume[0];
            }
            else
            {
                  sumPriceVolume += Close[0] * (Volume[0] - sumVolume);
                  sumVolume = Volume[0];
            }

            if (sumVolume> 0)
               DrawDot ("IntrabarVWAP" + CurrentBar, false, 0, sumPriceVolume / sumVolume, Color.Black);
        }

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.

Reply With Quote
  #7 (permalink)
 ejtrader 
Portland, OR
 
Experience: Intermediate
Platform: Sierra Chart
Trading: ES
Posts: 269 since Jan 2011
Thanks Given: 327
Thanks Received: 180

Thanks Harry.

I would use the O+C+H+L instead of close instead.

Reply With Quote





Last Updated on February 3, 2012


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts