Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Order can't be submitted: order status is CancelPending.

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

    Order can't be submitted: order status is CancelPending.

    Hello,

    I need some advice regarding a part of my automated strategy. Here’s the scenario:
    • At 7:00 AM, I enter a long trade.
    • By 7:05 AM, the trade is in a negative position, but the stop loss hasn’t been triggered.
    • At this point, the method ReversePosition() is called. This closes the current ATM strategy and attempts to open a trade in the opposite direction.

    However, instead of successfully opening the new position, I encounter an error.

    I’ve tried implementing logic to prevent this issue (see code below) but haven’t been able to resolve it. Specifically, I’m looking for:
    1. A better way to retrieve the order status after canceling an order.
    2. Guidance on how to wait until the order is no longer in the CancelPending state before submitting the new order.

    Thank you for your help!


    Error log:
    Order 'fb09e1e1506e403fbd64aacfec319c95' can't be submitted: order status is CancelPending. affected Order: Sell 5 Market
    Order='fb09e1e1506e403fbd64aacfec319c95/APEX-41035-71' Name='Close' New state='Rejected' Instrument='ES 12-24' Action='Sell' Limit price=0 Stop price=0 Quantity=5 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='Unable to submit order' Native error='Order 'fb09e1e1506e403fbd64aacfec319c95' can't be submitted: order status is CancelPending.'



    Code:
            #region Reverse Position
            private void ReversePosition()
            {
                // Reverse the position logic
                if (GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Long)
                {
                    AtmStrategyClose(atmStrategyId); // Close the long position
    
                    // Wait until the cancel operation completes
                    while (true)
                    {
                        string[] orderStatus = GetAtmStrategyEntryOrderStatus(atmStrategyOrderId);
    
                        // Check if the returned array contains valid order information
                        if (orderStatus.Length > 0)
                        {
                            string currentState = orderStatus[2]; // The third element represents the order state
                            if (currentState != "CancelPending")
                                break; // Exit the loop once the state is no longer CancelPending
    
                            Print("LONG position is being cancelled...");
                        }
                        else
                        {
                            Print("No order information found. Exiting loop.");
                            break;
                        }
    
                        System.Threading.Thread.Sleep(50); // Pause for 50 milliseconds
                    }
    
                    atmStrategyId = GetAtmStrategyUniqueId();
                    atmStrategyOrderId = GetAtmStrategyUniqueId();
    
                    double bidPrice = GetCurrentBid(); // Get the current bid price
                    Print("Current Bid is: " + bidPrice);
    
                    AtmStrategyCreate(OrderAction.Sell, OrderType.Limit, bidPrice, 0, TimeInForce.Day,
                        atmStrategyOrderId, "ATM T-Stop 5 Lot", atmStrategyId, (atmCallbackErrorCode, atmCallbackId) => {
                            if (atmCallbackId == atmStrategyId && atmCallbackErrorCode == Cbi.ErrorCode.NoError)
                            {
                                isAtmStrategyCreated = true;
                                Print("Reversing the losing LONG Position!");
                            }
                        });
                }​

    #2
    Hello devatechnologies,

    Generally you would need to use OnBarUpdate events to poll for changes before doing an action, using a while loop won't let the script properly update and continue. Do you still see this happen if you don't specifically try to create a new order and just call Close on the ATM?
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello Jesse,

      I do have some logic that will just close the trade, and not reverse it, if the position is more than a few points negative. In these cases the ATM strategy will close as expected with no errors.

      Actually I think when I was trading 3 contracts there wasn't an issue with closing and reversing the ATM order. Now that I'm trading 5 contracts it seems to happen. What if just called Sleep for a few seconds, giving all the orders(targets and stops) time to cancel?


      Code:
      System.Threading.Thread.Sleep(50); // Pause for 50 milliseconds

      Comment


        #4
        Hello devatechnologies,

        I would suggest removing the while loop and also do not use Thread.Sleep, those items prevent your script from working correctly. In NinjaScript there should never be a point where you are making the script wait, that prevents the platform from proceeding and changing variables in your script and could cause issues. Do you see problems if you do not do any kind of waiting? You should be polling for changes with each new OnBarUpdate instead of trying to stop the scripts execution.
        JesseNinjaTrader Customer Service

        Comment


          #5
          You're right. I'll get rid of the while loop and not use Thread.Sleep. If I'm going to reverse the position I'll close the ATM strategy, then on my tick data series check to see if the MarketPosition is flat, then when it is enter the reversal order.

          Thanks for your help!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by mitchdavo, Today, 05:30 PM
          0 responses
          7 views
          0 likes
          Last Post mitchdavo  
          Started by MikePari, 01-13-2025, 10:36 PM
          2 responses
          17 views
          0 likes
          Last Post MikePari  
          Started by Pa_Baz, 12-14-2024, 09:40 PM
          21 responses
          313 views
          1 like
          Last Post NinjaTrader_LuisH  
          Started by reynoldsn, 01-06-2025, 06:21 PM
          7 responses
          53 views
          0 likes
          Last Post reynoldsn  
          Started by 37s29nala, 05-26-2022, 05:44 PM
          9 responses
          1,000 views
          0 likes
          Last Post rmhollon  
          Working...
          X