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

Cancellation rejected by the Order Management System - Order is complete

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

    Cancellation rejected by the Order Management System - Order is complete

    Hello,

    When I execute a strategy, sometimes the following error occurs:

    Cancellation rejected by the Order Management System - Order is complete

    The provider is Rithmic for APEX.

    I have read the documentation and forums on this matter and have followed the guidelines. I have even placed code in the OnOrderUpdate event to capture the error, and this is what it receives:

    ErrCode: UnableToCancelOrder native: Cancellation rejected by the Order Management System - Order is complete

    I have gathered more data about the error, and in this case, the following happens:

    1. When an entry is executed, additional limit orders are placed. For example, we enter long, and 2 Buy Limit orders are placed at 2 different prices below.

    2. Each time a Long order is executed, a Target and a Stop are also placed.

    3. When the first Long (named Long1) is executed, its corresponding Target1 and Stop1 are set.

    4. The price drops a bit, and the second and third Long orders are executed. Now we have Long2 and Long3, with their corresponding Target2, Target3, Stop2, and Stop3.

    5. After some time, the strategy decides to exit the trade. ExitLong is sent for Long1, Long2, and Long3, and OnOrderUpdate marks them as Submitted.

    6. The price moves rapidly.

    7. OnOrderUpdate marks Stop3 Filled, Stop2 Filled, Stop3 Filled...

    8. After that, the ERROR occurs.

    9. The strategy is disabled.

    10. Now there are 3 SHORT positions without a Stop Loss or anything. I deduce that NT opened 3 positions in the opposite direction to what it was before the error.

    The same happens if, for example, in point 4 mentioned earlier, only 2 Long orders are executed, and then the strategy sends the Exit, i.e., ExitLong, and cancels the Long3 order. However, as the price moves very fast, OnOrderUpdate marks it as Submitted but then marks Long3 Filled. And the error occurs.

    I hope the explanation is clear.

    Could you help me find a solution to this error?

    I think what happens is that the Exit or Cancellation order is sent but not yet filled, and in the meantime, the Filled of the Stop order or Long3 order that has already been canceled is executed, causing the error.

    Thank you.​

    #2
    Hello ervin,

    It sounds like you are trying to use multiple exit orders for the same position entry.

    We highly recommend that you first cancel any working stop or limit, and then when the order updates as cancelled in OnOrderUpdate(), send the replacement exit market order.
    This will prevent issues where both orders can fill and reverse the position.

    When sending the cancellation first, if the order is filled before the order can be cancelled, you can choose to use RealtimeErrorHandling.IgnoreAllErrors. If the order is unable to cancel and is filled, then do not send a replacement market order.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thak you for your reply.
      It sounds like you are trying to use multiple exit orders for the same position entry.
      I don't try to use multiple exits. I set, in the example, 3 Long at different prices, and for Each one put diferent Targets and Stops. But because price moves very fast, tha cancellation occourrs and in the middle the Stops occours.

      We highly recommend that you first cancel any working stop or limit, and then when the order updates as cancelled in OnOrderUpdate(), send the replacement exit market order.
      This will prevent issues where both orders can fill and reverse the position.​
      When I send the ExitLong(Long1) ExitLong(2) ExitLong(3) I expected that their associated Targets and Stops cancell automatically.

      But again, in the middle of the execution the Stops is filled. And the error comes.

      I have tried to Cancell All pending orders, sending Cancell for all of them, but does not work either because when the cancell order is sent but is not executed yet, in the middle appears the Stop Filled.

      The error case I'm trying to describe is that when the cancell order was sent in the middle appers other order that muct be cancelled but is filled instead, and after that the cancellation is rejected.

      When sending the cancellation first, if the order is filled before the order can be cancelled, you can choose to use RealtimeErrorHandling.IgnoreAllErrors. If the order is unable to cancel and is filled, then do not send a replacement market order.
      This is the case, I guess.

      - But if I go for IgnoreAllErrors, if other kind of error happend the strategy couldn't realize that this errors ocourrs that, right? how can I handlle that??
      - Other Question, When ExitLong Happend, NT send a Market order (SELL) to close Long, right? but if fails, give the error and send other SELL to make sure the close is executed right? . If I set IgnoreAllErrors, NT will not send the seccond SELL?

      - I have tried with REalTimeErrorHandling.Close IgnoreRejects, but seems does not work either. There is a way to make it work this option? because IgnoreAllErrors seems to me so radical at this point.



      Comment


        #4
        Hello ervin,

        If you are submitting a stop order and then a market order for the same position, this would be multiple exit orders for the same position entry.

        "The error case I'm trying to describe is that when the cancell order was sent in the middle appers other order that muct be cancelled but is filled instead, and after that the cancellation is rejected."

        If the order is filled before the cancellation can be received, then the order is filled. You can choose to ignore the failed cancellation error and just let the order be filled and not submit a market order.

        "But if I go for IgnoreAllErrors, if other kind of error happend the strategy couldn't realize that this errors ocourrs that, right? how can I handlle that??"

        For other order errors, you can chose to call CloseStrategy() to allow the strategy to be disabled on other error types.

        "When ExitLong Happend, NT send a Market order (SELL) to close Long, right? but if fails, give the error and send other SELL to make sure the close is executed right? . If I set IgnoreAllErrors, NT will not send the seccond SELL?"

        This depends on your logic. If you submit a sell market order and the order is rejected, your code would have to detect this to send another market order. However, if a market order is rejected, sending another is not likely to fill and would likely cause an endless loop of the order being submitted, rejected, submitted, rejected, etc.

        "If I set IgnoreAllErrors, NT will not send the seccond SELL?"

        Ignoring all errors, would not cause NinjaTrader to submit or not submit orders automatically, your code logic would have to do that.

        "REalTimeErrorHandling.Close IgnoreRejects"

        A unable to cancel order error is not a rejected order. No, this would not apply to orders that cannot be cancelled.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          If you are submitting a stop order and then a market order for the same position, this would be multiple exit orders for the same position entry.
          Do you mean that if I have an Stop Loss for an Long Position, and then at some point the strategy send EXITLONG, at that point there are multiple exit orders? When the strategy send EXITLONG, NinjaTrader is who send a market position (SELL) to exit the existing long Right?

          If the order is filled before the cancellation can be received, then the order is filled. You can choose to ignore the failed cancellation error and just let the order be filled and not submit a market order.
          When Cancellation Fails, NinjaTRader Send another SELL to make sure the long is Closed. I just send EXITONG, and NinjaTrader manages that. I don't send ever Maket Sell Specifically in the strategy.

          And I can't to let the order filled, because at that point the strategy it's expecting that all the Longs are Closed and the position be FLAT. But instead Chart shows 3 Short, and the Position. MarketPosition have FLAT value. So The strategy think that it is on FLAT but the chart shows 3 Shorts.

          For other order errors, you can chose to call CloseStrategy() to allow the strategy to be disabled on other error types.
          Ok, It could be done. But how? and I will need to Log or something that an specific Error happend before CloseStrategy. And if Close strategy, in previous case, if the strategy close the 3 Shorts Will remain??? because the strategy can not to leave with orders to the trader, it could make to loose money without control.

          Thank you

          Comment


            #6
            Hello ervin,

            "Do you mean that if I have an Stop Loss for an Long Position, and then at some point the strategy send EXITLONG, at that point there are multiple exit orders?"

            Yes, if you have an exit stop loss working and send an exit market order, those are two exit orders which means multiple exit orders.

            "When the strategy send EXITLONG, NinjaTrader is who send a market position (SELL) to exit the existing long Right?"

            Yes, ExitLong() submits a sell market order to exit a long position.

            "When Cancellation Fails, NinjaTRader Send another SELL to make sure the long is Closed."

            You would have to code that into the script. If the cancellation of stop order fails, because the stop order filled, I would not recommend submitting an additional sell order as this would reverse the position.

            " I just send EXITONG, and NinjaTrader manages that. I don't send ever Maket Sell Specifically in the strategy."

            ExitLong() is the order method that sends a sell market order.

            "And I can't to let the order filled, because at that point the strategy it's expecting that all the Longs are Closed and the position be FLAT."

            If you can't allow the stop order to be filled, then why are you submitting a stop order? If you can't let that be filled, then don't send that order.
            If the stop order fills, that closes the position for that entry.

            " But instead Chart shows 3 Short, and the Position."

            The position was likely reversed from long to short because you have multiple sell exit orders fill for the same position. If you are long 1, and send a sell order for 1 and a sell order for 1, the position is now short 1, not flat.

            "Ok, It could be done. But how?"

            In OnOrderUpdate() you can check the ErrorCode error and string comment to see why the order had an error.


            If the error is not an unable to cancel order error, then call CloseStrategy().


            " and I will need to Log or something that an specific Error happend before CloseStrategy. "

            All order errors are already logged and will show on the Log tab of the Control Center.

            ". And if Close strategy, in previous case, if the strategy close the 3 Shorts Will remain?"

            Calling CloseStrategy() will "Cancels all working orders, closes any existing positions, and finally disables the strategy. "
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              "When Cancellation Fails, NinjaTRader Send another SELL to make sure the long is Closed."

              You would have to code that into the script. If the cancellation of stop order fails, because the stop order filled, I would not recommend submitting an additional sell order as this would reverse the position.​
              I don't code submitt an additional order. I just send EXITLONG and Ninjatrader manages the exit and is NinjaTrader who send the first market SELL to close the Long, and when it fails ninjatrader is who send the seccond SELL. I realize that, and in other forums say that too.

              If you can't allow the stop order to be filled, then why are you submitting a stop order? If you can't let that be filled, then don't send that order.
              If the stop order fills, that closes the position for that entry.​
              The sequence is kind of follow:
              - EnteLong(Long1)
              - OnExecutionUpdate, send Target1 and Stop1 Limit and Stop Orders
              - Price goes up.
              - Prcie start to goes down.
              - MA cross happend, so the strategy want to close the long. So send EXITLONG(Long1). when Long1 will be closed, the Target1 and Stop1 will be cancelled automatically by ninjatrader, because are created related to Long1.
              - On Order Update
              ExitLongSubmitted (I don't remeber exacly but it is for the example), and the other states mut be accomplisshed until FILLED, but wait.
              Stop1 Filled occours.
              this events occur at same time ex. 10:30:00 all of them.
              - Cancellation Reject occours.

              ". And if Close strategy, in previous case, if the strategy close the 3 Shorts Will remain?"

              Calling CloseStrategy() will "Cancels all working orders, closes any existing positions, and finally disables the strategy. "​
              Even if the short are not from the strategy? because the print for Position.MarketPosition at that point is FLAT. Ninjatrader will Close the Shorts anyway?

              Comment


                #8
                Hello ervin,

                Calling ExitLong() when you have already called ExitLongStopMarket() or SetStopLoss() is coding the strategy to send an additional exit order.

                Calling the order method, triggers the order to be submitted. You are coding the strategy to submit a second order when you call ExitLong().

                The exception to this, is if the strategy is disabled due to an order error and the strategy submits an order to close the position.

                "is NinjaTrader who send the first market SELL to close the Long"

                Calling the order method is what submits the order. NinjaTrader is submitting orders because you are calling order methods in the script.

                ", and when it fails ninjatrader is who send the seccond SELL"

                NinjaTrader would only submit an order if an order method is called. If you don't call the order method, the order does not get submitted.

                "The sequence is kind of follow:
                - EnteLong(Long1)
                - OnExecutionUpdate, send Target1 and Stop1 Limit and Stop Orders"

                If you cannot allow the stop order to be filled, do not submit the stop order.​

                "Even if the short are not from the strategy? because the print for Position.MarketPosition at that point is FLAT. Ninjatrader will Close the Shorts anyway?"

                I'm not certain I am understand what you are asking.

                Calling CloseStrategy() will only close a position if there is an open position. If you call CloseStrategy() and the position is flat, no order will be submitted to close any position as there is no position open.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  ChelseaB, thank you for trying to help me, but I'm not shoure if I am explain me in the rigth way.

                  Calling ExitLong() when you have already called ExitLongStopMarket() or SetStopLoss() is coding the strategy to send an additional exit order.
                  Calling the order method, triggers the order to be submitted. You are coding the strategy to submit a second order when you call ExitLong().

                  Calling the order method is what submits the order. NinjaTrader is submitting orders because you are calling order methods in the script.​
                  Ok, I understand that if I have opened 1 Long, and I put Stop and Target, I have two exits for that Long, one the stop and other the target. When Stop is reached NinjaTrader send a SELL MARKET order to close the Long, same thing for the Target. Until here, is correct?
                  But when the price is up or down or what ever, the strategy decides to close the 1 Long, then send an ExitLong. This is the third way to close the position, right?. So now the strategy have 3 posible exit orders to be filled, am I right?

                  Now In the scenario I have described. At the sime time, hour, minute, second, etc. the price moves so fast, and the exit long is submitted, but not filled yet. Meanwile the Stop is filled. At this ponit, for you explanation, the long are closed. Then comes the exitlong and is filled so now there are a short because two SELL orders are sent. until here it is correct?

                  I hope I'm explaining correctly.

                  But how can I prevent this situation?

                  The two sell orders are sent at the same time.

                  It's only happen when price move to fast that the orders are submitted and filled at the same time. The strategy works ok if one order is submmited, ExitLong, because when is filled the stop and target are cancelled correctly.

                  Thak you again.

                  Comment


                    #10
                    Hello ,

                    "When Stop is reached NinjaTrader send a SELL MARKET order to close the Long, same thing for the Target. Until here, is correct"

                    A stop loss is a Stop Market order. The order is submitted to the brokerage and on to the exchange and works on the exchange servers. Once touched, this fills at market price.
                    The Stop loss and Profit target from set methods SetStopLoss() and SetProfitTarget() use OCO ids (one-cancels-others). If one of the orders fills, the other is cancelled.
                    As these use OCO, it's fine for these two orders to be working at the same time, as they both cannot fill.

                    Submitting a new order with ExitLong(), however, will not be using that OCO. The order would close the long position, but would not cancel the stop and target automatically with OCO. It will be possible for the stop loss to fill and be unable to be cancelled. At this point both the exit long order and stop loss order will have both filled. This causes the position to become short.

                    If the stop were to not fill at the same time, after exit long market order fills, this will transmit back from the exchange to NinjaTrader that the order has executed, which will change the position, and then NinjaTrader will send a cancellation request for the stop loss. As long as the stop loss has not filled, it will be cancelled, which will also cancel the profit target due to OCO. (This is where your error is occurring as the stop did fill and could not be cancelled)


                    "But when the price is up or down or what ever, the strategy decides to close the 1 Long, then send an ExitLong. This is the third way to close the position, right?. So now the strategy have 3 posible exit orders to be filled, am I right?"

                    Yes, this is correct. There are three exit orders. It's possible for two of those exit orders to fill at the same time, and reverse the position instead of exiting the position.


                    "Now In the scenario I have described. At the sime time, hour, minute, second, etc. the price moves so fast, and the exit long is submitted, but not filled yet. Meanwile the Stop is filled. At this ponit, for you explanation, the long are closed. Then comes the exitlong and is filled so now there are a short because two SELL orders are sent. until here it is correct?"

                    Yes, I think you are understanding.


                    "But how can I prevent this situation?"

                    This is what I was suggesting in post # 2.
                    Hello, When I execute a strategy, sometimes the following error occurs: Cancellation rejected by the Order Management System - Order is complete The provider is Rithmic for APEX. I have read the documentation and forums on this matter and have followed the guidelines. I have even placed code in the OnOrderUpdate event to



                    If the stop loss and target are submitted with exit methods like ExitLongStopMarket() / ExitLongLimit() / SubmitOrderUnmanaged(), when you want to close with a market order, submit the stop to the CancelOrder(method).
                    In OnOrderUpdate() detect the order.OrderState is OrderState.Cancelled. If true, call ExitLong() to submit the market order.
                    If the ErrorCode error is ErrorCode.UnableToCancelOrder then do nothing and allow the stop loss to fill and close the position.
                    If the error code is not ErrorCode.NoError and is not ErrorCode.UnableToCancelOrder, then call CloseStrategy().​

                    I have examples of using exit methods and the unmanaged approach you may find helpful.



                    Set methods, however, are not meant to be cancelled. Attempting to cancel a set method will see it just re-appear. Attempting to modify the price of a set method will see it revert back to the original price.

                    If the stop loss and profit target are submitted with set methods like SetStopLoss() / SetTrailStop() / SetProfitTarget(0, and you want to close the position immediately, it may be wiser to move the profit target behind the market price so it fills immediately, which will cause the stop loss to cancel with OCO. This would be instead of submitting a sell market order with ExitLong().

                    Call SetProfitTarget(CalculationMode.Price, GetCurrentBid() - 5 * TickSize) and this would move the profit target to behind the market price, and it should fill at the market price.
                    (A limit fills at the specified price or better, and will fill at the market price if the market price is a better price.)

                    Note, if you do this, you also need to reset the profit target to a number of ticks before submitting a new entry method.

                    SetProfitTarget(CalculationMode.Ticks, 20);
                    EnterLong();
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Thank you very much, I think I'm starting to understand.
                      If the stop loss and target are submitted with exit methods like ExitLongStopMarket() / ExitLongLimit() / SubmitOrderUnmanaged(), when you want to close with a market order, submit the stop to the CancelOrder(method).
                      In OnOrderUpdate() detect the order.OrderState is OrderState.Cancelled. If true, call ExitLong() to submit the market order.
                      If the ErrorCode error is ErrorCode.UnableToCancelOrder then do nothing and allow the stop loss to fill and close the position.
                      If the error code is not ErrorCode.NoError and is not ErrorCode.UnableToCancelOrder, then call CloseStrategy().​​
                      Yes, I'm using ExitLongStopMarket() / ExitLongLimit() to set the Stop and Target for the LONG. If I send CancelOrder(StopOrder) to cancel the stop, like you sugest, is it necesary to send CancelOrder(TargetOrder) to cancel the target too? or target is cancelled automatically?

                      You mentioned that if i use set methods could move SetProfitTarget to execute inmediatly, but since I'm not using the set method, this approach could work in ExitLongStopMarket() / ExitLongLimit()?
                      I mean, use ChangeOrder(OrderExitLongLimit) and change the price to a price under the actual price so that order could exectue imendiatly, it could work?. In this case the Order ExitLongStopMarket and the order Long will cancel automatically, right?

                      Thak you for you patient.

                      Comment


                        #12
                        Hello ervin,

                        "Yes, I'm using ExitLongStopMarket() / ExitLongLimit() to set the Stop and Target for the LONG. If I send CancelOrder(StopOrder) to cancel the stop, like you sugest, is it necesary to send CancelOrder(TargetOrder) to cancel the target too? or target is cancelled automatically?"

                        With ExitLongStopMarket() / ExitLongLimit() you should check if they are in a working or accepted state and if they are try and cancel them. After the position closes, NinjaTrader will try and cancel them, but that gives more time for the orders to fill. These orders are not using OCO, so you should try and cancel them if they are working.

                        "You mentioned that if i use set methods could move SetProfitTarget to execute inmediatly, but since I'm not using the set method, this approach could work in ExitLongStopMarket() / ExitLongLimit()?"

                        Changing the limit so it is filled would also work with exit methods like ExitLongLimit().

                        "In this case the Order ExitLongStopMarket and the order Long will cancel automatically, right?"

                        Long is a position which can be opened / closed, ExitLongStopMarket() would submit an order which would be submitted, accepted, working, filled, cancelled, rejected, etc.

                        If the exit limit order fills, this would close the position. The stop order ninjatrader would try and cancel the opposing order after the position updates, but could fill during that time. The examples I've provided you, cancel the opposing orders if they are still working.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          I have write this code on OnOrderUpate to get the error:

                          if(!nativeError.IsNullOrEmpty() || error != ErrorCode.NoError)
                          {
                          Print("ERROR " + " Name: " + o.Name + " ErrCode: " + (error == null ? " null " : error.ToString()) + " native: " + (nativeError == null ? " null " : nativeError) );
                          }

                          and get some like that

                          Name: Exit Long 2 ErrCode: UnableToCancelOrder native: Cancellation rejected by the Order Management System - Order is complete.

                          and in the Log, is the same error, so it's ok.

                          But. I have another case, similar of course, in wich we were enter in Short (with their Stop and Target), and put one Short Limit order (Short2) if price goes up. At some point we want to close short so send Cancel(Short2) and then ExitShort(Short1). In the middle the Short2 is filled. And error raises.
                          The OnOrderUpdate event does not register the error, but in the LOG does, the error in Log was (something like that):

                          Stop2 Filled=1 Fill price=xxxx10.75 Error='Unable to cancel order' Native error='Cancellation rejected by the Order Management System - Order is complete.'

                          and then the strategy was disabled
                          and then the strategy was enabled.

                          It is the same error, but the first appears into OnOrderUpdate, but the seccond don't.

                          So, Why this error does not goes through OnOrderUpdate?
                          What Errors goes to OnOrderUpdate and What Error does not goes to OnOrderUpdate?
                          The aproach to see in OnOnderUpdate the errors does not seem very reliable if not all errors go through OnOrderUpdate, am I Right?

                          Comment


                            #14
                            Hello ervin,

                            It may be the strategy is being disabled before the order can update.

                            May I confirm you are using RealtimeErrorHandling.IgnoreAllErrors as suggested in post # 2?
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              May I confirm you are using RealtimeErrorHandling.IgnoreAllErrors as suggested in post # 2?
                              No, this data was collected before the IgnoreAllErrors. But I have to ask.

                              Thak you for your help.
                              I'm going to implement all we were talking about and Let you know.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by leonardomocci, Today, 08:28 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post leonardomocci  
                              Started by geotrades1, Today, 07:54 AM
                              4 responses
                              9 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by prisonbreaker82, 02-24-2024, 11:16 AM
                              3 responses
                              36 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by bill2023, Today, 08:21 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post bill2023  
                              Started by KonAdams, Yesterday, 10:53 PM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X