Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Handling OrderState.Cancelled gracefully

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

    Handling OrderState.Cancelled gracefully

    In my strategies, I attempt to handle an order cancellation gracefully. I'm not really sure how an order can be cancelled if I didn't do it in the code, but inside OnExecutionUpdate(), I want to handle a order state == Cancelled correctly, especially in the case that it was partially filled.

    So I have this code at the top of the function:


    Code:
    /// Check the order state for both entry end exit
    if (entryOrder != null  &&  entryOrder == execution.Order)
    {
        string type = (execution.IsEntryStrategy) ? "        ENTRY " : "        EXIT ";
    
        /// If part filled, I do nothing, and hope that this method gets called again with order state == filled.
        /// If that never happens, not sure what is the "right" thing to do, or how to tell it never happened...
        if (execution.Order.OrderState == OrderState.PartFilled)
            Log(type + "ORDER ONLY PARTIALLY FILLED; execution.Order.Filled = " + execution.Order.Filled + ", Position.Quantity = " + Position.Quantity);
        
        /// If the order was somehow cancelled, then we need to just close the position if partially opened
    ​    else if (execution.Order.OrderState == OrderState.Cancelled)
        {
            Log(type + "ORDER CANCELLED; dir = " + marketPosition.ToString() + ", execution.Order.Filled = " + execution.Order.Filled);
            if (execution.Order.Filled > 0  &&  entryOrder != null)
            {
                Log("Order partially filled, now cancelled, so cancelling order");
                
                // I HAD THE CODE LIKE THIS, BUT IT DOES NOT SEEN RIGHT.  THE ORDER IS ALREADY
                // CANCELLED, SO I WANT TO CLOSE THE PARTIAL POSITION, AND CAN I DO IT FROM
                // HERE, OR DO I NEED TO SOMEHOW 'QUEUE' IT TO BE DONE AFTER THIS COMPLETES?
                CancelOrder(entryOrder);
            }
        }
        else if (execution.Order.OrderState == OrderState.Filled)
            Log(type + "ORDER FILLED (execution.Order.OrderState == OrderState.Filled); execution.Order.Filled = " + execution.Order.Filled + ", Position.Quantity = " + Position.Quantity);
    }
    else
        Log("entryOrder (" + entryOrder + ") null or not == execution.Order; execution.Order.OrderState = " + execution.Order.OrderState);
    What would be the "proper" way to handle this?

    Also, this is all assuming a market order, but what if I have placed a limit order? Does OnExecutionUpdate() get called when I place a pending (limit) order, and then again when filled? Because I do not seem to see my logging output for that function until it's filled.




    #2
    Hello DerkWehler,

    The order being cancelled means you can't try to cancel it again, its already a complete order. At that point you would have submit an exit order if you wanted to close the partial position that order had entered.

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello DerkWehler,

      The order being cancelled means you can't try to cancel it again, its already a complete order. At that point you would have submit an exit order if you wanted to close the partial position that order had entered.
      Yes, thank you. Perhaps I wasn't clear: to close that partial order, can I go ahead and SubmitOrderUnmanaged(), (or call function that does so), from there in OnExecutionUpdate(); will it just get put in a queue?

      Comment


        #4
        Hello DerkWehler,

        Yes you would use the order submission method for whichever approach you are using in that script. OnExecutionUpdate can be used to submit orders.

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          Hello DerkWehler,

          Yes you would use the order submission method for whichever approach you are using in that script. OnExecutionUpdate can be used to submit orders.
          Many thanks Jesse, happy holidays!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          553 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
          100 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          543 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          546 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X