![]() |
|
|||||||
| Indicator Development Support for the development of custom indicators using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Member
Join Date: Apr 2007
Location: , ,
Posts: 82
Thanks: 1
Thanked 1 time in 1 post
|
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. |
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
|
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.
Ben
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Member
Join Date: Apr 2007
Location: , ,
Posts: 82
Thanks: 1
Thanked 1 time in 1 post
|
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. |
|
|
|
|
|
#4 |
|
NinjaTrader Customer Service
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
|
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.
Ben
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Member
Join Date: Apr 2007
Location: , ,
Posts: 82
Thanks: 1
Thanked 1 time in 1 post
|
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. |
|
|
|
|
|
#6 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
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.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#7 |
|
Member
Join Date: Apr 2007
Location: , ,
Posts: 82
Thanks: 1
Thanked 1 time in 1 post
|
Thanks, Josh. I will try that.
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |