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 it thread-safe: Account.Orders[i]; Account.Strategies[i].Instrument;

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

    is it thread-safe: Account.Orders[i]; Account.Strategies[i].Instrument;

    Hello.
    I want to check on the strategy start are there any working orders placed on the instrument and are any other strategies running on the instrument.
    How to do it in a thread safe way?
    Can I just do it like this:

    PHP Code:
                        #region check is no working orders on instrument
                        
    List<Orderalive_orders = new List<Order>();                        
                        for( 
    int i 0Account.Orders.Counti++ )
                        {                                                
                            var 
    order Account.Orders[i];

                            if( !
    Order.IsTerminalStateorder.OrderState ) && order.Instrument == Instrument )
                            {
                                
    alive_orders.Addorder );                            
                            }                        
                        } 
    Or should I use Dispatcher.InvokeAsync()?
    Or should I do it another way?
    fx.practic
    NinjaTrader Ecosystem Vendor - fx.practic

    #2
    Hello fx.practic,

    Thanks for your post.

    It would be safe to access the Account.Orders collection from the strategy. If you are worried about an order or strategy being added/removed at the same time the loop is being processed, you could consider copying the collection and looping through your copy to prevent errors. Or you could consider using a try/catch to identify when the collection has been modified and to check the collection again.

    Copying the collection:
    Code:
                else if (State == State.DataLoaded)
                {
                    System.Collections.ObjectModel.Collection<Order> FoundOrders = new System.Collections.ObjectModel.Collection<Order>(Account.Orders);
                    foreach(Order order in FoundOrders)
                    {
                        Print(order.ToString());
                    }
    
                    System.Collections.ObjectModel.Collection<StrategyBase> FoundStrategies = new System.Collections.ObjectModel.Collection<StrategyBase>(Account.Strategies);
                    foreach (StrategyBase strategy in FoundStrategies)
                    {
                        Print(strategy.Name);
                    }
                }
    This will create a copy of the collection, so you would be looping through a snapshot and not the active Orders and Strategies collections. If the collection gets modified inflight, you will not receive an error.

    Using a try catch to trap when the collection is modified inflight:

    Another approach could be consider to use a try/catch and to loop through the collection again in the event of an error. This would allow you to loop through the collection again after it has been modified to get the most recent changes to it. The SampleStreamReader example uses a try/catch to trap when a file is in use. A similar approach could be taken to have the the catch block set a variable that tells the script it should check the collection again.



    I look forward to being of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      Perfect, thank you, Jim!
      fx.practic
      NinjaTrader Ecosystem Vendor - fx.practic

      Comment


        #4
        If account contains hundreds of orders, and all of them are in terminal state - I will takes too much time to enumerate all of them. Maybe, list of live only orders exists somewhere?
        fx.practic
        NinjaTrader Ecosystem Vendor - fx.practic

        Comment


          #5
          Hello fx.practic,

          The Account.Orders collection would be the accessible collection to check. There is not a separate collection for active orders that we could advise to use instead. Looping through the Orders collection would be how you can identify active orders, and we could not advise a faster way to do this.

          Let us know if you have any questions.
          JimNinjaTrader Customer Service

          Comment


            #6
            Clear, thank you.
            1. Can I be sure that orders in Account.Orders are sorted by placing (or accepting) time?
            2. Can I be sure that orders are not removing from Account.Orders during the strategy life time (or during strategy State.RealTime) ?
            Maybe there are other hints to optimize work with this collection?
            Last edited by fx.practic; 01-19-2020, 09:16 AM.
            fx.practic
            NinjaTrader Ecosystem Vendor - fx.practic

            Comment


              #7
              Hello fx.practic,

              RE 1: Orders from the Account.Orders collection will be listed chronologically (last updated) fashion following your approach. You can observe this by testing the snippet below and observing the output in comparison to the Orders tab of the Control Center. If you move an order it will be updated and will appear at the top of this list.

              Code:
              else if (State == State.DataLoaded)
              {
                  for( int i = 0; i < Account.Orders.Count; i++ )
                  {
                      Print(Account.Orders[i].ToString());
                  }
              }
              RE 2: I would not expect a situation where an order will be removed from the collection unless we have rolled into the next trading day when the Orders tab is cleared. Active orders will remain present when this happens.
              JimNinjaTrader Customer Service

              Comment


                #8
                Thank you, Jim, this is very helpful.
                I think I can store the index of the latest order is in the terminal state and next time start enumeration from that index, not from the beginning of collection.
                fx.practic
                NinjaTrader Ecosystem Vendor - fx.practic

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by sidlercom80, 10-28-2023, 08:49 AM
                168 responses
                2,262 views
                0 likes
                Last Post sidlercom80  
                Started by Barry Milan, Yesterday, 10:35 PM
                3 responses
                10 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by WeyldFalcon, 12-10-2020, 06:48 PM
                14 responses
                1,429 views
                0 likes
                Last Post Handclap0241  
                Started by DJ888, 04-16-2024, 06:09 PM
                2 responses
                9 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by jeronymite, 04-12-2024, 04:26 PM
                3 responses
                41 views
                0 likes
                Last Post jeronymite  
                Working...
                X