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

Can OnBarUpdate & OnMarketData execute simultaneously?

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

    Can OnBarUpdate & OnMarketData execute simultaneously?

    Hi,

    I have logic in an indicator that initializes some variables on each new bar, as in

    int lastBar = -99;
    if (lastBar != CurrentBar)
    {
    // -- do something...

    I am processing bid/ask data, so I override both OnBarUpdate and OnMarketData. As I understand it, it's non-determinstic for any given incoming tick whether one or the other is called first. So my thinking is that code like the above needs to be in both. That should be fine as long as it's not possible for both to execute simultaneously on different threads. That would cause some problems and would require synchronization. Synchronization is expensive performance-wise, and I'd prefer not to do it. Also, while I don't believe it would happen here, it is possible to dead-lock when using synchronization.

    I'd appreciate it if someone from NT could respond as to whether or not OnBarUpdate and OnMarketData can execute simultaneously on different threads, and if so, share any tips on synchornization in a NT indicator.

    Regards,

    Gordon

    #2
    Hi Gordon,

    The NinjaScript methods are run in the same thread as the UI. The events are not guaranteed in an order (the events are not synchronous), however, they will not run simultaneously because they are run in the same thread.

    However, OnBarUpdate is only run when there is a tick of data which triggers OnMarketUpDate any way. So if you need something updated every tick, writing it solely into OnMarketUpDate would suffice.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hi Gordon,

      The NinjaScript methods are run in the same thread as the UI. The events are not guaranteed in an order (the events are not synchronous), however, they will not run simultaneously because they are run in the same thread.

      However, OnBarUpdate is only run when there is a tick of data which triggers OnMarketUpDate any way. So if you need something updated every tick, writing it solely into OnMarketUpDate would suffice.
      Hi Chelsea,

      Great - thanks for your answer. Just to be clear, are you saying that while in general there's no guaranteed order of execution of methods, OnMarketData does always precede OnBarUpdate?

      Also, can you tell me whether or not NinjaScript methods will run on the single thread of the UI in version 8, and, when you say single thread of the UI, is the UI a single chart (one thread per chart) or is the UI all of NinjaTrader?

      Thanks much. This information is very helpful.

      Regards,

      Gordon

      Comment


        #4
        Grose,

        This is not what Chelsea is saying.

        If a tick of information comes in either OnMarketData() OR OnBarUpdate() will get called first.
        There is synchronization between these two.

        NinjaTrader 8 will be multi-threaded for everything. With NinjaTrader 7, the UI, including charts, indicators, strategies and so forth, all run on 1 thread.
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Thanks Cal and Chelsea,

          One last question - when an incoming tick is the first tick of a new bar, is CurrentBar updated regardless of whether OnMarketData or OnBarUpdate is called, or is it only updated, or current, in the On BarUpdate method?

          Regards,

          Gordon

          Comment


            #6
            Grose,

            That is going to be processed in the background by the application. OnBarUpdate() is just a method that gets called from the data.
            Cal H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by pvincent, 06-23-2022, 12:53 PM
            14 responses
            238 views
            0 likes
            Last Post Nyman
            by Nyman
             
            Started by TraderG23, 12-08-2023, 07:56 AM
            9 responses
            382 views
            1 like
            Last Post Gavini
            by Gavini
             
            Started by oviejo, Today, 12:28 AM
            0 responses
            1 view
            0 likes
            Last Post oviejo
            by oviejo
             
            Started by pechtri, 06-22-2023, 02:31 AM
            10 responses
            125 views
            0 likes
            Last Post Leeroy_Jenkins  
            Started by judysamnt7, 03-13-2023, 09:11 AM
            4 responses
            59 views
            0 likes
            Last Post DynamicTest  
            Working...
            X