Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
OnMarketData and range bars problems
Collapse
X
-
OnMarketData and range bars problems
Hi, when using e.g. replay connection. or during faster market moves, I sometimes get OnMarketData tick in batches. Unfortunately, ticks in some of the batches are in fact drawn over 2 (or more, depending on the range settings) bars. Is there any way how to correctly decide which tick belongs to which bar? Using Time data is not very helpful, during reports several range bars with the same time can be created. Since I cannot rely on any order of OnMarketData and OnBarUpdate call, I am a bit lost in this case. Thanks for any help.Tags: None
-
Do you have a specific time/timezone on market replay I can test this with?
What period of range bar is being used?
What instrument and session template?
Additionally if you have a sample script you're using that replicates the error please attach that as well.
Located in (MY)Documents\NinjaTrader 7\bin\Custom (indicator or strategy folder)LanceNinjaTrader Customer Service
Comment
-
Hi Lance, I use replay data, default Range 4 chart on TF12-12, 13.12.2012. At 16:02:05 there is a batch of three 829.8 and six 829.9 prices. OnMarketData is triggered for all ticks before the OnBarUpdate is called and CurrentBar is the same in all OnMarketData calls. 829.9 however belong to a new bar. As for sample, I am sorry, but I have quite a complicated logic in the indicator and cannot easily prepare a sample script.
Comment
-
Hello,
When dealing dealing with range bars a new bar will not be drawn until price exceeds the bounds of the current bar. The boundary is first exceeded when receiving data on the current bar. OnMarketData() is called with the current bar value and the value outside of the boundary. OnBarUpdate() is then called and updates the bar.
If you needed this last value set to be shifted to the new bar you would need to manually update your tracking variables for the last call.
The following is a simple script that would allow you to better see how the values are being printed.
Code:protected override void OnBarUpdate() { Print(Time[0]+" "+CurrentBar); } protected override void OnMarketData(MarketDataEventArgs e) { if (e.MarketDataType == MarketDataType.Last) { Print("last: "+CurrentBar); Print("Last = " + e.Price + " " + e.Volume); } }
LanceNinjaTrader Customer Service
Comment
-
Hi Lance, it looks I managed to handle the Range bars behavior. However, there is another problem, this time with Volume bars. My algorithm is this: in OnMarketData I store incoming ticks to a queue. In OnBarUpdate, where I expect to have correct CurrentBar value, I dequeue the ticks and process them with correct CurrentBar, which may not be correct on OnMarketData. With Rangebars, every call of OnMarketData corresponds to one call of OnBarUpdate, at least everything seems to work with this premise. Can you confirm this? Unfortunately, with Volume bars this is not true.
See this log, TF12-12, Volume 100 chart:
OnMarketData 13.12.2012 16:18:19: Price=831, Volume=1, CurrentBar=2933
OnBarUpdate 13.12.2012 16:18:19: Price=831, TickQueue.Count=5, CurrentBar=2933
Dequeue: TickVolume=1, Volume[0]=94
OnMarketData 13.12.2012 16:18:19: Price=831, Volume=1, CurrentBar=2933
OnMarketData 13.12.2012 16:18:19: Price=831, Volume=1, CurrentBar=2933
OnMarketData 13.12.2012 16:18:19: Price=831, Volume=1, CurrentBar=2933
OnMarketData 13.12.2012 16:18:19: Price=831, Volume=1, CurrentBar=2933
OnMarketData 13.12.2012 16:18:19: Price=831, Volume=2, CurrentBar=2933
OnMarketData 13.12.2012 16:18:19: Price=831, Volume=1, CurrentBar=2933
OnMarketData 13.12.2012 16:18:19: Price=831, Volume=1, CurrentBar=2933
OnMarketData 13.12.2012 16:18:19: Price=831, Volume=1, CurrentBar=2933
OnMarketData 13.12.2012 16:18:19: Price=831, Volume=1, CurrentBar=2933
OnBarUpdate 13.12.2012 16:18:19: Price=831, TickQueue.Count=13, CurrentBar=2933
Dequeue: TickVolume=1, Volume[0]=100
OnBarUpdate 13.12.2012 16:18:19: Price=831, TickQueue.Count=12, CurrentBar=2934
Dequeue: TickVolume=1, Volume[0]=4
There is many OnMarketData calls with Volume=1 or 2, but in OnBarUpdate the Volume[0] increases by 6 and 4 - the ticks are obviously aggregated and OnBarUpdate calls do not correspond to OnMarketData calls at all.
My question is obvious - how do I handle this using Ninjatrader API? How do I correctly match OnMarketData call to corresponding CurrentBar value? Thank you for your help.
Comment
-
What you see in the log are values from the MarketDataEventArgs structure. I put the structure into a Queue and Dequeue it in OnBarUpdate(). No processing on my side. You can easily modify your script like this, you will get same results as I do - many calls with small volumes in OnMarketData but fewer calls and large volume increments in OnBarUpdate.
Code:protected override void OnBarUpdate() { Print(Time[0]+" "+Volume[0] + " " + CurrentBar); } protected override void OnMarketData(MarketDataEventArgs e) { if (e.MarketDataType == MarketDataType.Last) { Print("last: "+CurrentBar); Print("Last = " + e.Price + " " + e.Volume); } }
Comment
-
Unfortunately there isn't going to be a way to guarantee the sequence of event calls.
OnMarketData may occur before or after OnBarUpdate which means you may get a few ticks in the next bar if counting manually yourself.
For example, the following would return close to the same results but there is no guarantee because of the ordering that they would always be the same.
Code:protected override void OnBarUpdate() { Print(thisBarsVol); thisBarsVol = 0; Print(Time[0]+" "+Volume[0] ); } protected override void OnMarketData(MarketDataEventArgs e) { if (e.MarketDataType == MarketDataType.Last) { //Print("last: "+CurrentBar); thisBarsVol += e.Volume; } }
LanceNinjaTrader Customer Service
Comment
-
OK, I understand current NinjaTrader architecture does not allow what I need. Is there any chance in version 8 the architecture and object model is changed or enhanced so that it allows precise tracking of changes per bar? Something like introduce events Chart.NewBarCreated, Chart.BarChanged, Bar.TickAdded, Bar.Changed or similar? I am sure your developers know exactly how the architecture should look like to allow monitoring all events a user could need, and I'm sure they use something like that in the charting module anyway. If it's possible, can you add to your feature request list request to publish these events? Thank you.
Comment
-
Hello,
Yes I will have this added to the list of feature requests. It has been requested in the past and as such we will add this request to the list of tallies for this feature. The more requests for a feature the more likely to see it implemented.
Let me know if I can be of further assistance.LanceNinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by bertochi, 04-16-2020, 12:15 PM
|
30 responses
1,058 views
2 likes
|
Last Post Today, 09:36 PM | ||
Started by Lumbeezl, 01-11-2022, 06:50 PM
|
28 responses
755 views
1 like
|
Last Post Today, 09:23 PM | ||
Started by jourdale, 03-16-2024, 06:33 AM
|
3 responses
42 views
0 likes
|
Last Post Today, 06:44 PM | ||
Started by henryd333, 03-22-2021, 08:18 AM
|
18 responses
930 views
0 likes
|
Last Post Today, 06:07 PM | ||
Started by PaulMohn, Today, 05:51 PM
|
0 responses
4 views
0 likes
|
Last Post
by PaulMohn
Today, 05:51 PM
|
Comment