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

The OnMarketData() method is guaranteed to always be called before OnBarUpdate()

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

    The OnMarketData() method is guaranteed to always be called before OnBarUpdate()

    Is this really true?

    I've put a Print statement inside each method and onBarUpdate is executed the first every time I reload the ninjascript in a chart using tick replay.

    #2
    Hello johnrocargas, and thank you for your question.

    While incoming market data is reacted to before bars complete, for historical data processing, if you would like to guarantee an exact sequence of stored events are replayed for both the OnBarUpdate and OnMarketData events, you will need the Tick Replay feature enabled. You can learn more about this feature here,



    If any tick replay or Ninja questions come up please don't hesitate to reach out.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      It's not that I want to guarantee the order. I've just done a simple test.

      1. I've created and empty Indicator script with Calculate on each tick enabled.
      2. I've put a Print statement inside the onBarUpdate and another Print statement inside the onMarketData 3. I've cleared the Ninjascript Output window.
      4. I've attached the indicator to a chart with some bars.

      The very first line of the Ninjascript Output is the one coming from the Print statement inside the onBarUpdate, not from the onMarketData Print statement.

      Comment


        #4
        Thank you for this additional information. Without Tick Replay enabled for historical data, to answer your question, there is no guaranteed ordering.

        If you filter out non-live data the necessity for Tick Replay to getting OnMarketData events to precede OnBarUpdate will make itself apparent. You can do so with code such as the following at the beginning of OnBarUpdate or OnMarketData :

        Code:
        [FONT=Courier New]if (State < State.Realtime)
        {
          return;
        }[/FONT]
        If you then leave your Print statements in code you will see many OnMarketData events precede every OnBarUpdate with that change.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Tick Replay is always enabled. I have Tick Replay enabled in the chart I'm doing the test. I don't want to have Tick Replay disabled. I'm just trying to understand the execution flow of the two methods while the Tick Replay mode is on and Calculate is on each tick. It's not clear from the docs how they are supposed to be used together. In addition, if NT8 is multithreaded and those methods are executed in different threads (which again is not clear from the docs) that can cause other problems that are not addressed in the docs.
          Last edited by joanrocagas; 01-04-2017, 12:40 PM.

          Comment


            #6
            I've done another test.
            For every new T & S line I see in the Ninjascript Output

            onBarUpdate
            onMarketData


            so, Why am I seeing the Print statement from the onBarUpdate BEFORE the Print from the onMarketData?

            Comment


              #7
              Thank you for helping clear this up, I believe I understand a little better what is occurring. Just to make sure I am completely on the same page, when you say OnBarUpdate is executing before OnMarketData - which should not be occurring - are you referring to the order that Print statements show up in the Output Window? If so, NinjaTrader is a multithreaded, asynchronous, event driven application, and as such Output Window ordering is not a guarantee of execution order.

              I would like to recommend the following :

              • Replace your Print("message") calls by Log("message", LogLevel.Information)
              • If the message you are printing does not include the timestamp, I also recommend adding Time[0].ToString("HH:mm:ss.fff") to any messages you send out

              If the ordering in your Log (your logs are in (My) Documents\NinjaTrader 8\logs) still does not match the ordering on the charts here,





              Please let us know so we can investigate further. OnMarketData calls should precede OnBarUpdate.
              Jessica P.NinjaTrader Customer Service

              Comment


                #8
                It occurs the same. Besides, even though the multithreaded environment, why the Print Statements would always end up executed in reverse order?

                See the pictures of the log file, and of the indicator source code
                Attached Files

                Comment


                  #9
                  I appreciate your patience. It looks like your copy of NinjaTrader and the ones available to me on the machines available to me are behaving differently. We must therefore discover what is occurring local to your system.

                  Could you run the script I have attached on your system, and let me know if the output you receive is different from the output I have attached in terms of OBU / OMD order?

                  If you get the same order I do, could you modify my script so it produces OBU events before OMD events, and send it my way? Please attach any returned code sample as a text document.

                  When you run this test, please ensure all the following. These are likely steps you already took and once again I appreciate your patience, but please work through this checklist as it will ensure our systems have the same configuration :

                  • Tools -> Options -> Market Data -> Show Tick Replay is checked
                  • Right-click on your chart -> Data Series -> Tick Replay is checked
                  • You are using a 2 second chart

                  Thank you once again, if we can discover what is happening on your system it may benefit other customers.
                  Attached Files
                  Jessica P.NinjaTrader Customer Service

                  Comment


                    #10
                    I've done the tests.

                    The first OBUPrinter - the file you sent me - is calculated on each bar close. I've attached the output. It is exactly like yours.

                    After that, I've modified the OBUPrinter to calculate OnEachTick. See the attached .cs file.
                    I've attached also the output. You can see there that the OBU method is always called before the OMD method.
                    Attached Files
                    Last edited by joanrocagas; 01-04-2017, 03:20 PM.

                    Comment


                      #11
                      Thank you for following up. I had misunderstood what was meant. This is the correct operation of Ninja with Calculate.ForEachTick.
                      Jessica P.NinjaTrader Customer Service

                      Comment


                        #12
                        What?
                        So are you saying that NT8 calls OMD before OBU except on Calculate.OnEachTick? This behaviour is not consistent.

                        If I'm adding up buy/sell volume at ask like in the BuySellVolume indicator

                        Code:
                        protected override void OnMarketData(MarketDataEventArgs e)
                        		{			
                        			if(e.MarketDataType == MarketDataType.Last)
                        			{				
                        				if(e.Price >= e.Ask)
                        				{
                        					buys += e.Volume;
                        				}
                        				else if (e.Price <= e.Bid)
                        				{
                        					sells += e.Volume;
                        				}
                        			}	
                        		}
                        		
                        		protected override void OnBarUpdate()
                        		{
                        			if (CurrentBar < activeBar || CurrentBar <= BarsRequiredToPlot)
                        				return;
                        
                        			// New Bar has been formed
                        			// - Assign last volume counted to the prior bar
                        			// - Reset volume count for new bar
                        			if (CurrentBar != activeBar)
                        			{
                        				Sells[1] = sells;
                        				Buys[1] = buys + sells;
                        				buys = 0;
                        				sells = 0;
                        				activeBar = CurrentBar;
                        			}
                        
                        			Sells[0] = sells;
                        			Buys[0] = buys + sells;
                        		}
                        the onBarUpdate is going to lag one tick behind the onMarketData.


                        Also why do say in the docs
                        The OnMarketData() method is guaranteed to always be called before OnBarUpdate()


                        Can you guys check why this is implemented this way, please?
                        Last edited by joanrocagas; 01-04-2017, 03:51 PM.

                        Comment


                          #13
                          in this old thread seem to be talking about the same problem


                          onBarUpdate lags 1 trade tick because is called BEFORE onMarketData!! it should be the other way!
                          Could some NinjaTrader dev share some light into this, please?

                          Comment


                            #14
                            I have more carefully reviewed the documentation, and while NinjaScript is truly event driven and asynchronous, I was mistaken earlier in saying this is the correct behavior. You are correct, there are strong guarantees in the documentation against this. For reference I am providing a quote from OnMarketData.

                            Originally posted by http://ninjatrader.com/support/helpGuides/nt8/en-us/onmarketdata.htm
                            The OnMarketData() method is guaranteed to always be called before OnBarUpdate()
                            In light of the documentation and Ninja's behavior being inconsistent, I will treat this as a bug.

                            This behavior was confirmed on our end. We will be investigating this further. Please keep an eye on the NinjaTrader 8 Release Notes page for updates and bugfixes.





                            Tracking ID: NTEIGHT-10990

                            A link to this forums post has been included in the bug report, and the developers will be aware of any posts in this thread.
                            Jessica P.NinjaTrader Customer Service

                            Comment


                              #15
                              Thanks Jessica.

                              Is there a public bug tracking site or similar to follow the progress on open bugs and such things?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by BarzTrading, Today, 07:25 AM
                              2 responses
                              25 views
                              1 like
                              Last Post BarzTrading  
                              Started by devatechnologies, 04-14-2024, 02:58 PM
                              3 responses
                              20 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by tkaboris, Today, 08:01 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post tkaboris  
                              Started by EB Worx, 04-04-2023, 02:34 AM
                              7 responses
                              163 views
                              0 likes
                              Last Post VFI26
                              by VFI26
                               
                              Started by Mizzouman1, Today, 07:35 AM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X