NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 12-12-2007, 01:12 PM   #1
RedDuke
Senior Member
 
Join Date: Apr 2007
Location: , ,
Posts: 105
Thanks: 1
Thanked 0 times in 0 posts
Default OnBarUpdate and OnMarketData used in one indicator?

Hello,

If I have an indicator that uses both OnBarUpdate and OnMarketData, and let’s say there is a sell and the price ticks down. This means that both of these methods will be triggered. I assume that OnMarketData will be triggered first and then the logic will jump to OnBarUpdate. Is this the case?

Thanks,
redduke
RedDuke is offline  
Reply With Quote
Old 12-12-2007, 01:43 PM   #2
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
Default

OnBarUpdate() only triggers off of last price. OnMarketData() triggers off of all market data events. OnBarUpdate() will trigger for every last price market data event fired. Although I suspect OnBarUpdate() is called second in most cases, you can not guarantee that.
NinjaTrader_Ray is offline  
Reply With Quote
Old 12-12-2007, 02:07 PM   #3
RedDuke
Senior Member
 
Join Date: Apr 2007
Location: , ,
Posts: 105
Thanks: 1
Thanked 0 times in 0 posts
Default

Hi Ray,

Thanks for the reply. I took a look at one of Ninja 6.5 indicators – BuySellVolume. It uses both of these methods. OnMarketData is used to calculate buy and sell volume and then OnBarUpdate plots it into display, so it is important for one to follow the other, which I assume it does based on your reply, it least in most cases.

Regards,
redduke
RedDuke is offline  
Reply With Quote
Old 12-13-2007, 07:42 AM   #4
RedDuke
Senior Member
 
Join Date: Apr 2007
Location: , ,
Posts: 105
Thanks: 1
Thanked 0 times in 0 posts
Default

I have experienced a weird behavior today, when I tried to see how OnMarketData performs. Below is the code that I used:

protected override void OnBarUpdate()
{
Print(LastVolume);
}

protected override void OnMarketData(MarketDataEventArgs e)
{
if (e.MarketDataType == MarketDataType.Last)
LastVolume = e.Volume;
}

When I opened Output window and placed it next to Time and Sales, I have notices that there was about 1-2 seconds delay of the LastVolume appearing there. In some cases it appeared at the same time as volume in T&S, but most of the time there was a small delay.

Any idea why there is a lag?

Thanks,
redduke
RedDuke is offline  
Reply With Quote
Old 12-13-2007, 07:50 AM   #5
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
Default

Is the Time and Sales set to refresh based on a timer? If yes, this would account for the delay.
NinjaTrader_Ray is offline  
Reply With Quote
Old 12-13-2007, 07:53 AM   #6
RedDuke
Senior Member
 
Join Date: Apr 2007
Location: , ,
Posts: 105
Thanks: 1
Thanked 0 times in 0 posts
Default

No, T&S is set to be refreshed with every tick, and it is the indicator with the logic below that lags. I thought that they should behave identically, am i wrong?

Thanks,
redduke
RedDuke is offline  
Reply With Quote
Old 12-13-2007, 08:08 AM   #7
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,164
Thanks: 6
Thanked 46 times in 32 posts
Default

Thanks for clarification. Looks like OnBarUpdate() may trigger first before OnMarketData thus the event that is printing LastVolume is actually the next incoming tick.
NinjaTrader_Ray is offline  
Reply With Quote
Old 12-13-2007, 08:17 AM   #8
RedDuke
Senior Member
 
Join Date: Apr 2007
Location: , ,
Posts: 105
Thanks: 1
Thanked 0 times in 0 posts
Default

Ray,

Thanks for this info. This would explain another anomaly that I have seen. I have put BuySellVolume indicator on constant volume charts (200 volumes per bar), so I should have never seen BuySellVolume greater than 200 for each bar. However, this is exactly what happened. BuySellVolume showed volumes like 230, 220 and so on for some of the bars.

Now since, OnMarketData is triggered after OnBarUpdate, it just grabbed the volume that was supposed to stay in the previous bar and added to the current bar as soon as current bar is created.

Regards,
redduke
RedDuke is offline  
Reply With Quote
Old 12-14-2007, 03:25 PM   #9
rt-trader
Senior Member
 
Join Date: Jan 2007
Location: , ,
Posts: 250
Thanks: 1
Thanked 2 times in 1 post
Default

Hi Ray & RedDuke,

I have noticed the same issues with the interaction between OnMarketData and OnBarUpdate.

If you add e.Time to your short example and then include the timestamp in the Print statement you will see that the delay you notice is actually a 1 trade lag.

Not sure why this happens - any ideas Ray?
rt-trader is offline  
Reply With Quote
Old 12-14-2007, 06:54 PM   #10
RedDuke
Senior Member
 
Join Date: Apr 2007
Location: , ,
Posts: 105
Thanks: 1
Thanked 0 times in 0 posts
Default

Hi Rt-trader,

OnMarkerData will be called whenever somethig changes on level1. OnBarupdate will be called only when there is a trade, in which case it will be called before OnMarketData. The solution that I found to be working is migrate most my indicator logic to OnMarketData. I am very excited since I can now capture time and sales in indicator without some not so perfect coding in OnBarUpdate.

Regards,
redduke
RedDuke is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
OnMarketData And Chart Update question rt-trader Historical NinjaTrader 6.5 Beta Threads 2 11-29-2007 05:18 PM
OnMarketData() not working Json General Programming 4 11-08-2007 04:09 AM
Drawing indicator outside of OnBarUpdate() nvladik Indicator Development 1 06-21-2007 12:01 AM
Stateful OnBarUpdate kgillis23 General Programming 2 05-30-2007 11:01 AM
Events Other Than OnBarUpdate californiaruby Automated Trading 5 02-10-2007 08:30 AM


All times are GMT -6. The time now is 02:11 PM.