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

Is OnBarUpdate() always executed in the same thread for each strategy instance?

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

    Is OnBarUpdate() always executed in the same thread for each strategy instance?

    In NT8, is OnBarUpdate() always executed in the same thread for each strategy instance? Asking my question in another way, when configuring a strategy instance to calculate on each tick in realtime, do I need to worry about that OnBarUpdate() might be triggered by two ticks almost at the same time so that I have to use lock around most part of OnBarUpdate()? Same question applies to OnOrderUpdate() etc. For the same method in the same strategy instance, is OnOrderUpdate() always executed in the same thread so that NinjaTrader guarantee the sequential changes of state of the same order from strategy point of view? In an old topic thread it was stated that each NT6.5 strategy instance is one thread, therefore it seems my question is not an issue in NT6.5. But it seems this is no longer how NT8 implements indicator and strategies.

    #2
    I would like to clarify my questions a little bit. I am asking about whether a method such as OnBarUpdate() triggered by realtime ticks within the same strategy instance would always executed within the same thread (I don't know how many threads each strategy instance may own if there is no fancy code such as Dispatcher.InvokeAsync() is used. I tried to google some answers online, but most discussions found in search results are pretty old and applicable to older NT versions.

    In NT7 this would never happen (according to this topic thread https://ninjatrader.com/support/foru...ad.php?t=37982). But since NT8 has changed a lot compared to NT7, I am wondering whether it is still true.

    Actually I am looking for an explicit answer similar to the 4th post in this topic thread https://ww.ninjatrader.com/support/f...ad.php?t=52160, which unfortunately is applicable to NT7.

    Is there a complete introduction about what is the best practice in NT8 regarding to multi-threading, concurrency, lock, mutex etc. so as to avoid unnecessarily using locks in my methods such as OnBarUpdate(), OnOrderUpdate(), etc.
    Last edited by clearpicks; 12-24-2017, 08:01 AM.

    Comment


      #3
      Hello clearpicks,

      From my understanding in NinjaTrader 8 OnBarUpdate(), OnMarketData(), and OnMarketDepth within a single instance are all triggered from the same thread (the instrument thread).

      Script instances themselves may be run on different threads.

      It will never be necessary to lock Series<T> objects (data series), but it may be necessary to lock collections that are not data driven. (For example when looping through accounts or account objects)

      If it is necessary to use series data from a non-data-driven method such as OnRender or a Timer, you will need to use TriggerCustomEvent() to synchronize the series, or use .GetValueAt().

      Below are publicly available links to the help guide.

      Last edited by NinjaTrader_ChelseaB; 12-24-2017, 12:41 PM.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Hello Chelsea,

        Thanks for the response.

        How about those methods such as OnOrderUpdate(), OnExecutionUpdate()? Are they all executed within the same thread so that realtime events relevant to the same strategy instance would never trigger multiple concurrent OnOrderUpdate() calls (spread in different threads)?

        In your post you mentioned that "in NinjaTrader 8 OnBarUpdate(), OnMarketData(), and OnMarketDepth within a single instance are all triggered from the same thread (the instrument thread). Script instances themselves may be run on different threads." Is there a NinjaTrader document publicly available which provides architecture level information as that in your post. It seems those critical information related to multi-threading coding is only partially available and gets scattered in many different pages in NinjaScript reference.

        Thanks,
        Clearpicks

        Comment


          #5
          Hello clearpicks,

          OnOrderUpdate() and OnExecution() are triggered from separate threads within a single instance of a script and are triggered asynchronously as the order is updated.

          All methods in a script can trigger simultaneously as another instance of a script as script instances themselves may be run on different threads.

          It is possible that OnOrderUpdate() can trigger in multiple instances of scripts simultaneously.

          Within a single instance of a script, it is not necessary to lock an order object that is created by the script.

          However, when looping through the Account collection or orders from an account or positions from an account using the Addon class, it does become necessary to lock the collection.

          In the help guide, if a lock is necessary, this will generally be in the example.

          Below are a few publicly available links to the help guide.



          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Chelsea,

            I figured out the answers to my own questions by writing scripts to test what would happen in various scenarios. Thanks for your response anyway.

            --Clearpicks

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Mestor, 03-10-2023, 01:50 AM
            16 responses
            388 views
            0 likes
            Last Post z.franck  
            Started by rtwave, 04-12-2024, 09:30 AM
            4 responses
            31 views
            0 likes
            Last Post rtwave
            by rtwave
             
            Started by yertle, Yesterday, 08:38 AM
            7 responses
            29 views
            0 likes
            Last Post yertle
            by yertle
             
            Started by bmartz, 03-12-2024, 06:12 AM
            2 responses
            22 views
            0 likes
            Last Post bmartz
            by bmartz
             
            Started by funk10101, Today, 12:02 AM
            0 responses
            7 views
            0 likes
            Last Post funk10101  
            Working...
            X