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 there a threadsafe way to access Account.Positions in an AddOn?

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

    Is there a threadsafe way to access Account.Positions in an AddOn?

    Same question for Account.Orders too.

    I definitely saw a case where accessing Account.Positions was not. It started with a count of 1, and by the time I referenced element 0 in the collection, it had no elements, which threw an exception.

    Are there synchronization primitives we are supposed to use here somehow?

    #2
    Hello carnitron,

    Thank you for your post.

    Prior to accessing the positions in the Account.Positions collection, you may lock Account.Positions as demonstrated in the example from the help guide here:


    You should not have to do the same for Account.Orders. Here is the help guide page for the Orders collection:


    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Oh geez, it's right there in an example. Apologies for overlooking that, I was hunting through the decompiled code. Thank you for the help!

      Comment


        #4
        It would be nice if there was clearer documentation on what collections need locking and what don't. Relying on reading sample code for the documentation for the item, or in some cases reading the included NinjaTrader code makes this much more challenging.

        Comment


          #5
          I agree with ntbone. The documentation is kind of hito r miss on questions like this, and I've seen multiple cases now where really good information is in the examples, but you don't really have a direct path there from the documentation search.

          Comment


            #6
            Hello ntbone and carnitron,

            Thank you for your feedback.

            For the Account.Positions page, there is a link to the Microsoft documentation about Collections:
            Property Value
            An Collection of Position objects
            That page does have a section about "Thread Safety" and the details about locking a collection to guarantee thread safety. This particular item is more of a general C# concept that is not specific to NinjaTrader, so that is likely why it is not specifically documented in the NinjaTrader help guide. I can certainly put in a suggestion to add a note/tip to the page about thread safety and the potential need to lock the Positions collection to ensure thread safety.

            Please don't hesitate to reach out with any additional NinjaTrader items we may assist you with.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              Thanks

              With regards to my comment
              1. Knowing which collections in the NinjaTrader are thread safe or not is not always clear. There are thread safe collections that can be used, and it is not always clear by the API that this is the case
              2. There are various ways to handle locking. The lock could use the collection itself, or it could use an object dedicated for dealing with the lock.
              3. The Orders object has the same documentation link as the Positions.
              Locking and thread safety are generic development concepts. Knowing that a particular collection needs a lock, and knowing which object to use for its lock is not clear with the NinjaTrader documentation and I have had to resort to looking at sample code to determine answers to this.

              It would be helpful if every collection in the documentation contained a specific line item that says "use a lock before accessing this collect" or "no lock is needed for this collection" or something similar.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by fx.practic, 10-15-2013, 12:53 AM
              5 responses
              5,404 views
              0 likes
              Last Post Bidder
              by Bidder
               
              Started by Shai Samuel, 07-02-2022, 02:46 PM
              4 responses
              95 views
              0 likes
              Last Post Bidder
              by Bidder
               
              Started by DJ888, Yesterday, 10:57 PM
              0 responses
              8 views
              0 likes
              Last Post DJ888
              by DJ888
               
              Started by MacDad, 02-25-2024, 11:48 PM
              7 responses
              159 views
              0 likes
              Last Post loganjarosz123  
              Started by Belfortbucks, Yesterday, 09:29 PM
              0 responses
              8 views
              0 likes
              Last Post Belfortbucks  
              Working...
              X