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!
See more
See less

Partner 728x90

Collapse

OnMarketData and range bars problems

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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.

    #2
    Hello,

    You could use a variable and store CurrentBar in it to find the bar location of the call to OMD()



    Let me know if I can be of further assistance.
    LanceNinjaTrader Customer Service

    Comment


      #3
      Hi Lance, of course this is the first thing I tried, unfortunately it does not work. CurrentBar is sometimes not updated yet when ticks for the new bar are already coming.

      Comment


        #4
        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


          #5
          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


            #6
            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


              #7
              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


                #8
                So that I may further assist, can you provide me with the snippet you're using in OnMarketData() for volume tracking?

                Also what is your Calculate on bar close setting?
                LanceNinjaTrader Customer Service

                Comment


                  #9
                  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


                    #10
                    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;
                    			}			
                                      }
                    If tracking volume in OnBarUpdate isn't sufficient, you would likely get better results if you pull the last tick of data from the prior bar. I recall there being a user who has coded this but unfortunately I don't remember where it was located. You may also want to look into the GOMI indicators.
                    LanceNinjaTrader Customer Service

                    Comment


                      #11
                      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


                        #12
                        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 alifarahani, Today, 09:40 AM
                        6 responses
                        30 views
                        0 likes
                        Last Post alifarahani  
                        Started by Waxavi, Today, 02:10 AM
                        1 response
                        17 views
                        0 likes
                        Last Post NinjaTrader_LuisH  
                        Started by Kaledus, Today, 01:29 PM
                        5 responses
                        13 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Started by Waxavi, Today, 02:00 AM
                        1 response
                        12 views
                        0 likes
                        Last Post NinjaTrader_LuisH  
                        Started by gentlebenthebear, Today, 01:30 AM
                        3 responses
                        17 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Working...
                        X