Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Orders missed using foreach(Order order in account.Orders)

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

    Orders missed using foreach(Order order in account.Orders)

    issue closed...
    Last edited by defa0009; 09-11-2024, 03:17 PM.

    #2
    Hello TIA,

    Are you printing the execution.ToString() at the top of OnExecutionUpdate() without any conditions?

    To confirm, you are waiting for the order to fill and have an execution, is this correct?

    If you are wanting to see the order become working before it fills, print the order.ToString() from OnOrderUpdate().
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello defa0009,

      Looping through a collection while it is being modified will typically cause an error and disable the script.

      I recommend looping over the <Account>.Orders.ToList() to copy the list first to avoid errors.

      Once the loop starts, it will not contain any new elements and you would have to loop through again.

      However, if you are looking for new orders or updated orders, this would update in the <Account>.OrderUpdate event.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Hello defa0009,

        Looping through the account orders would not be correct.

        The strategy is only able to see orders that are submitted by the strategy. Orders submitted to the account will update update OnExecutionUpdate() as they would not be orders made by the strategy.

        If you want to handle orders submitted to the account, you would need to add event handlers to the account.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello defa0009,

          Using <Account>.Orders.ToList() would avoid periodically getting a 'Collection was modified; enumeration operation may not execute' error.

          The new orders would update the <Account>.OrderUpdate event where you can process them.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Hello defa0009,

            I think understand. You've named your custom event handler OnExecutionUpdate() and this is not the same as the NinjaScript Strategy OnExecutionUpdate() override method. Apologies for the confusion.

            <Account>.ExecutionUpdate runs when the execution is processed, <Account>.OrderUpdate runs when the order is processed, the <Account>.Orders collection updates after the order is processed.

            Try moving that code to the OrderUpdate event.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello defa0009,

              The new order would be updating with the <Account>.OrderUpdate event. Is this where you are running the code?

              Regarding the example, may I have a link to the example or help guide page you are referring to so I can get that updated?
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Hello defa0009,

                The video is not showing what event this is being run from.

                The new order would be updating with the <Account>.OrderUpdate event. Is this where you are running the code?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hello defa0009,

                  "OnExecutionUpdate() occurs after OnOrderUpdate() completes"

                  This is not always the case and should not be relied upon.

                  Execution objects should be accessed from the <Account>.ExecutionUpdate event. Order objects should be accessed from the <Account>.OrderUpdate event.

                  What example are you referring to?
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Hello defa0009,

                    It matters what event this is running from.

                    "The order count should not change?"

                    The Orders collection count will change when the collection updates after the order updates.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello defa0009,

                      <Account>.ExecutionUpdate runs when the execution is processed, <Account>.OrderUpdate runs when the order is processed, the <Account>.Orders collection updates after the order is processed.

                      Try moving that code to the <Account>.OrderUpdate event.​
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Hello ​defa0009,

                        When the execution event occurs, the stop and target have not been submitted.

                        Once these are submitted and become active in the <Account>.OrderUpdate() event these will show as accepted and / or working.

                        No, from the <Account>.ExecutionUpdate event of the entry these orders should not yet exist.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Hello defa0009,

                          An order will be OrderState.Filled, OrderState.Cancelled, or OrderState.Rejected when the order stops updating.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Hello defa0009,

                            Below is a video showing no orders are being removed from the collection.


                            Attached is the test script.
                            AccountOrdersCollectionTest_NT8.zip

                            The orders are added when they update the <Account>.OrderUpdate event.

                            The orders remain after they have been added to the collection.


                            "I need to be able to tell when all OrderUpdate events have completed"

                            Your previous inquiry was to know how an order has finished it's last update. An order will last update when the order is filled, rejected, or cancelled.
                            A stop and target will not yet be submitted and become working at the exact time the entry fills. It takes time for those orders to be submitted and become working.
                            When the stop is created and submitted, it will be available in the <Account>.Orders collection and will update the <Account>.OrderUpdate collection.

                            Chelsea B.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            558 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            324 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by Mindset, 02-09-2026, 11:44 AM
                            0 responses
                            101 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                            0 responses
                            545 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by RFrosty, 01-28-2026, 06:49 PM
                            0 responses
                            547 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X