Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Getting Pending Order Cancel From Strategy

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

    Getting Pending Order Cancel From Strategy

    I have a strategy that runs in strategy analyzer, but when running in a live environment, I keep getting pending order cancel errors. My on bar update is as follows:
    if (ToTime(Time[0]) <= EndTime && ToTime(Time[0]) >= StartTime) {
    if (Short) {
    if (Close[0] < Close[1]) {
    //CancelOrder(myEntryOrder);
    EnterShortLimit(1, Close[0] + gap*TickSize, "Enter Short");
    }​

    #2
    Hello samish18,

    I see you are not using the overload with the isLiveUntilCancelled bool as true.
    This means any order not filled when the bar closes will automatically be cancelled.

    EnterShortLimit(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double limitPrice, string signalName)
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I want orders not filled within the current bar to be automatically cancelled.

      Comment


        #4
        Is there a way to set isLiveUntilCancelled to true and then to cancel if a certain number of bars pass?

        Comment


          #5
          Hello samish18,

          You can cancel an order at any time by supplying the order object to CancelOrder().
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            I am using the CancelOrder() function and am still getting the error message

            Comment


              #7
              Hello samish18,

              What is the full error message you are getting?

              Are you saving the order object to a variable from OnOrderupdate() that is being supplied to CancelOrder()?
              Are you ensuring the <order>.OrderState is OrderState.Accepted or OrderState.Working before attempting to cancel the order?

              The example 'ProfitChaseStopTrailExitOrdersExample' below shows calling CancelOrder() on lines 267 to 289.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                The error occurs less frequently, but it is the "pending order cancel" error. My code is as follows:
                protected override void OnBarUpdate()
                {
                if (BarsInProgress != 0)
                return;

                if (CurrentBars[0] < (20))
                return;

                CancelOrder(myEntryOrder);
                if (ToTime(Time[0]) <= EndTime && ToTime(Time[0]) >= StartTime) {
                if (Short) {
                if (Close[0] < Close[1]) {
                EnterShortLimit(0, true, 1, Close[0] + gap*TickSize, "Enter Short");
                }
                }
                else {if (Close[0] > Close[1]) EnterLongLimit(0, true, 1, Close[0] - gap*TickSize, "Enter Long");
                }
                }
                }
                protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError) {
                if (order.Name == "Enter Short" || order.Name == "Enter Long") myEntryOrder = order;
                if (myEntryOrder != null && myEntryOrder == order) {
                if (myEntryOrder.OrderState == OrderState.Cancelled) myEntryOrder = null;
                }
                }​

                Comment


                  #9
                  Hello samish18,

                  Please copy and paste the full error message from the Log tab of the Control Center.

                  Pending order cancel just sounds like the order was attempting to be cancelled which is what you want.

                  However, there is no check here in this code that myEntryOrder is not null and has a working or accepted orderstate.

                  The very first thing this script will do on the first bar past 20, is try and cancel this order.
                  This order will not exist because the order has never been submitted yet.
                  That will fail because there is no order to cancel and the variable is null.

                  Put the call to CancelOrder() in a condition that checks myEntryOrder is not null and is in a working or accepted state.



                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    The return statement is not the issue, the strategy runs without issue for a while and execute some trades, then it spits out the error and I have to restart ninjatrader

                    Comment


                      #11
                      Hello samish18,

                      I've not stated the return is the issue.

                      The issue is that CancelOrder() is being called for every bar update and there is no check that there is any order to cancel or if the variable being supplied is not full.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        I added a if(myEntryOrder != null) before the CancelOrder(). I am still getting the issue

                        Comment


                          #13
                          Hello samish18,

                          Please also add a check the order's OrderState is OrderState.Working or OrderState.Accepted.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            I added OrderState.Accepted to the if statement and am still getting the same issue - the strategy runs for a bit (maybe 100 bars) and then has trouble cancelling an order

                            Comment


                              #15
                              Hello samish18,

                              May I see the script to confirm everything?

                              To export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
                              1. Click Tools -> Export -> NinjaScript Add-on...
                              2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
                              3. Click the 'Export' button
                              4. Enter the script name in the value for 'File name:'
                              5. Choose a save location -> click Save
                              6. Click OK to clear the export location message
                              By default your exported file will be in the following location:
                              • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
                              Below is a link to the help guide on Exporting NinjaScripts.


                              Once exported, please attach the file as an attachment to your reply.​
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              54 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              130 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              72 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              44 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              49 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X