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

How to get thread safe collection of orders?

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

    How to get thread safe collection of orders?

    I use the code to enumerate orders of Account:

    Code:
    foreach (var o in account.Orders.Where(.....)) 
    {
    }
    Sometimes this code throws exeption: "Collection was modified; enumeration operation may not execute"

    It's because while I'm enumerating orders, some of them was added/removed.
    I need to get thread safe copy of order collection, but I don't know how.

    #2
    Hello rfsettling,

    You may need to use the lock() statement to prevent changes to the collection while it is being looped through.

    For example:
    Code:
    lock(account.Orders)
    {
    foreach (var o in account.Orders.Where(.....)) 
    {
    }
    }
    Below is a publicly available link to the help guide.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello, Chelsea

      Thank you for your replay.

      lock(account.Orders) statement will work only if opposite side (who changes the collection, in other words, this is NT internals) also uses this statement. Is it true?

      Comment


        #4
        Hello rfsettling,

        I am not quite sure I am understanding what you are asking.

        The lock() statement ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread tries to enter a locked code, it will wait, block, until the object is released.

        Below is a publicly available link to the Microsoft Documentation on lock.
        Use the C# lock statement to ensure that only a single thread exclusively reads or writes a shared resource, blocking all other threads until it completes.


        In other words, the lock will prevent the collection from being modified while you are looping through it.

        May I confirm you have tried this and it did not correct the error?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          In order to make lock() construction working all code which accesses to some variable must use lock() statement.

          In other words, somewhere inside ninja must be code
          lock(Account.Orders)
          {
          Add/Remove items
          }

          Following the link to msdn you provided: "The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section."

          If Ninja does not use lock(), it is useless to me to use lock().

          Comment


            #6
            Hello rfsettling,

            That is correct. In the thread running your script you are looping through a collection that is actively being modified by NinjaTrader in another thread. The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section and would prevent modifications until your loop is done.

            There are places where NinjaTrader Development uses lock() and suggests using lock().

            Below is a publicly available link to the help guide on Multi-Threading Consideration for NinjaScript. Please read under the heading Access Violation Exception.


            Also below are publicly available links to other sections where lock() is used.




            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello, Chelsea

              Thank you for the links provided. Now I see that it's a common practice in Ninja to lock collections. Now I'm sure it will be working

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by ursavent, Today, 12:54 PM
              0 responses
              2 views
              0 likes
              Last Post ursavent  
              Started by Mizzouman1, Today, 07:35 AM
              3 responses
              17 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by RubenCazorla, Today, 09:07 AM
              2 responses
              13 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by i019945nj, 12-14-2023, 06:41 AM
              7 responses
              82 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by timmbbo, 07-05-2023, 10:21 PM
              4 responses
              158 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Working...
              X