NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Indicator Development

Indicator Development Support for the development of custom indicators using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 12-21-2008, 10:06 PM   #1
svrz
Member
 
Join Date: Apr 2007
Location: , ,
Posts: 82
Thanks: 1
Thanked 1 time in 1 post
Default OnMarketData and OnBarUpdate

Greetings

I've created an indicator where the calculations are being done in OnMarketData and the inserted into the indicator array in OnBarUpdate. However it seems data coming into OnMarketData at the begining of the bar is not being updated in OnBarUpdate.

Here is the code snippet:

protected override void OnMarketData(MarketDataEventArgs e)
{
if (e.MarketDataType == MarketDataType.Ask) m_LastAskPrice = e.Price;
else if (e.MarketDataType == MarketDataType.Bid) m_LastBidPrice = e.Price;

else if (e.MarketDataType == MarketDataType.Last)
{
if (m_LastAskPrice <= e.Price) AskVol = e.Volume;
else if (m_LastBidPrice >= e.Price) BidVol = e.Volume;
}
}

protected override void OnBarUpdate()
{
//keep updating currentbar
if (m_ActiveBar == CurrentBar)
{
Ask.Set(AskVol );
Bid.Set(BidVol );
}
//new bar: reset
else
{

Ask.Set(0);
Bid.Set(0);

m_ActiveBar = CurrentBar;
}

}

The output from OnMarketData matches T&S perfectly. The only problem is that some values are not being inserted into the data array.


Is the basic approach correct? Is there a better way to synch OnBarUpdate and OnMarketData?

Please advise. Thanks in advance.
svrz is offline  
Reply With Quote
Old 12-22-2008, 08:34 AM   #2
NinjaTrader_Ben
NinjaTrader Customer Service
 
NinjaTrader_Ben's Avatar
 
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
Default

Hello,


OnBarUpdate and OnMarketData are not sync-able. These are different threads and can execute at completely different times. This may be the source of your issue.

Also, your m_ActiveBar variable seems like it may be involved as it looks like you will not get information from the first tick of the first bar because m_ActiveBar = CurrentBar does not happen until AFTER the first tick of the new bar. (Assuming you have CalculateOnBarClose set to false.) I suggest using if(FirstTickOfBar). You will need to change your logic a bit but I think you will be better off.
NinjaTrader_Ben is offline  
Reply With Quote
Old 12-22-2008, 08:47 PM   #3
svrz
Member
 
Join Date: Apr 2007
Location: , ,
Posts: 82
Thanks: 1
Thanked 1 time in 1 post
Default

Hello Ben

Thanks for reply. FirstTickOfBar solved one of the problems, thanks. However one remains which is: when bids and asks are updated in OnMarketData but there is no corresponding tick for OnBarUpdate, naturally the indicators do not get updated. I've only been testing this indicator during the after market hours for symbol ES where ticks can be scarce at times. I will have to see if this is a problem during the RTH.

Is there a way around this problem?

Thanks in advance.
svrz is offline  
Reply With Quote
Old 12-23-2008, 08:25 AM   #4
NinjaTrader_Ben
NinjaTrader Customer Service
 
NinjaTrader_Ben's Avatar
 
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
Default

Hello,


I am not sure what you are try to accomplish exactly but you may want to put your plot.Set(...) in the OnMarketData() block of code.
NinjaTrader_Ben is offline  
Reply With Quote
Old 12-23-2008, 08:53 AM   #5
svrz
Member
 
Join Date: Apr 2007
Location: , ,
Posts: 82
Thanks: 1
Thanked 1 time in 1 post
Default

Hi Ben

Thanks for the reply.

The indicator is just a cummulation of ask and bid sizes for a given bar. In other words, it is an attempt to take all the ask and all the bid sizes in T&S from start of a bar to its end and then sum them up respectively.

I've tried placing the plot.set(...) method in OnMarketData but the results were the same. I will make another try.

Regrds.
svrz is offline  
Reply With Quote
Old 12-23-2008, 08:59 AM   #6
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

Drive all your logic from OnMarketData() and just reset your aggregate variables in OnBarUpdate(). Run with CalculateOnBarClose = true so you know a bar has closed when you get an OnBarUpdate() event.
NinjaTrader_Josh is offline  
Reply With Quote
Old 12-23-2008, 09:17 AM   #7
svrz
Member
 
Join Date: Apr 2007
Location: , ,
Posts: 82
Thanks: 1
Thanked 1 time in 1 post
Default

Thanks, Josh. I will try that.
svrz 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
OnBarUpdate() / OnMarketData() Priority mrlogik General Programming 11 09-28-2008 05:00 PM
OnMarketData() vs. OnBarUpdate() Tight Face General Programming 6 09-10-2008 02:20 PM
OnMarketData SystemTrading General Programming 2 09-09-2008 04:51 PM
Is there no way to backtest with OnMarketData() ? tradefaz General Programming 6 08-24-2008 08:55 PM
OnBarUpdate and OnMarketData used in one indicator? RedDuke General Programming 9 12-14-2007 06:54 PM


All times are GMT -6. The time now is 10:26 AM.