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

Unable to cancel previous open orders

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

    Unable to cancel previous open orders

    Hello,

    I need support for the correct code to cancel previous open orders after new ones be filled when the price jump more ticks that expected.

    In the sample below same that I as trying, when the price jump first to the "exit_Buy", the Sell order isn't being canceled (maybe the execution being made before the previous order be in fully submitted state?)

    Code:
    private Order Buy, Sell;
    private double Price;
    
    
    private void AssignOrderToVariable(ref Order order)
    {
         if (order.Name == "Buy" && Buy != order)
                    Buy = order;
         if (order.Name == "Sell" && Sell != order)
                    Sell = order;        
     }​
    
    
    protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
                Cbi.MarketPosition marketPosition, string orderId, DateTime time)
    {​
        if (Sell != null && Sell.OrderState == OrderState.Submitted && execution.Name == "exit_Buy")
        {
               CancelOrder(Sell);
        }​
    
    }
    
    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
    {
          if (marketDataUpdate.Price == Price)
          {  
                Buy = EnterLongLimit(1, Price, "Buy");
          }
    
          if (marketDataUpdate.Price == Price - (4 * TickSize))
          {
                Sell = EnterShortLimit(1, (Price - (4 * TickSize)), "Sell");
          }​
    
          if (marketDataUpdate.Price < Price - (4 * TickSize))
          {
                ExitLong(@"exit_Buy", @"Buy"); ​
          }
    }

    #2
    Hello diorfo,

    I would suggest to reverse what you are doing here and cancel the order first, then based on that cancellation you can submit the new order. That ensures the existing order does not fill before submitting the exit. You will also need to use Prints to make sure your conditions are happening as you expect.

    Code:
    if (marketDataUpdate.Price < Price - (4 * TickSize) && Sell != null)
    {
        Print("cancelling order");
        CancelOrder(Sell);
    }
    
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
    {
        Print(order.ToString());​
        if (Sell!= null && Sell== order && order.OrderState == OrderState.Cancelled)
        {
            ExitLong(@"exit_Buy", @"Buy"); ​
        }​
    }​​
    Last edited by NinjaTrader_Jesse; 01-10-2023, 08:37 AM.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      Is there any way to do the cancelling just after "ExitLong" order been filled?

      Because I may lost 1 tick canceling the sell order first before open "exitlong" order because even if the price hits the stop price, I may get filled sometimes first in the "Sell" 1 tick above with better price.

      Comment


        #4
        Hello diorfo,

        I would not suggest to do that because that opens the possibility of having both orders filled at once. Your pending short entry order may fill and also the exit for the long. The exit would bring you to flat and then the short would enter to become in a short position. Its best to make sure orders are canceled before doing any other position changes to make sure that no additional orders fill.

        If you wanted to do that you can use the order events in whatever ways you want so long as what you are doing makes sense. You can use OnOrderUpdate to detect order state changes like cancelled and use OnExecutionUpdate to detect filled executions.
        Last edited by NinjaTrader_Jesse; 01-10-2023, 08:44 AM.
        JesseNinjaTrader Customer Service

        Comment


          #5
          I would like to insist in place both orders and cancel only after one of both be filled.

          The problem I'm having is that the OnExecutionUpdate seems not track correctly the order state from below code as should do.

          Code:
          if (Sell != null && Sell.OrderState == OrderState.Submitted && execution.Name == "exit_Buy")
          {
              CancelOrder(Sell);
          }​
          ​
          Even adding "Sell.OrderState == OrderState.Working" and "Sell.OrderState == OrderState.Accepted" to the code, the "if" sentence never was able to be true.

          I tried to put in OnOrderUpdate event but didn't work too.

          Is there any other event I may use to track more accurately the fills and order states?

          Comment


            #6
            Hello diorfo,

            If your logic is not working correctly you would need to use a print to see what the values are for the variables you used. That would let you know what needs to be changed.

            JesseNinjaTrader Customer Service

            Comment


              #7
              Jesse,

              I decided to do similar you have written, first exiting long position, but then entering the short reverting positing, but now I'm having the issue to not be able to cancel the short order when the price jumps it and moves some ticks far from short order. I need to cancel it about 6 ticks below when the price not fills in descend move. I need to cancel to avoid get short while price is moving back again to upside.

              I tried to do as I you have written:
              Code:
              if (marketDataUpdate.Price < Price - (6 * TickSize) && Sell != null)
              {
              Print("cancelling order");
              CancelOrder(Sell);
              }
              But I received the error below:

              "Error on calling 'OnMarketData' method on bar 2511: Object reference not set to an instance of an object."

              How can I cancel the sell order when the price jumps it and move far away?

              thanks for supporting.​

              Comment


                #8
                Hello diorfo,

                The error means that some object you used was null. From the given code I don't see what that would be, you would need to isolate which line of code is causing that to happen to better understand what is null. You can start by commenting out the cancel order line and this condition to make sure that its not some other code you may have in OnMarketData. The error notes that the problem happened in OnMarketData.

                If the Sell object is null then you need to use the AssignOrderToVariable that you have shown from OnOrderUpdate to assign your order object to the variable once it has started working or has been accepted.

                JesseNinjaTrader Customer Service

                Comment


                  #9
                  I was able to see where was the problem.

                  I was annulling the order value (Sell = null) in previous field.

                  Now is happening another weird thing.

                  I was able to make true the "if" below and printed the order was cancelling but the order doesn't cancel!!!

                  Code:
                  if (marketDataUpdate.Price < Price - (6 * TickSize) && Sell != null && Sell.OrderState == OrderState.Working)
                  {
                  Print("cancelling order");
                  CancelOrder(Sell);
                  }


                  How can be it possible?

                  I have assigned the order as below to avoid mismatch but the code seems not find the order to cancel it.

                  Code:
                  private void AssignOrderToVariable(ref Order order)
                  {
                  if (order.Name == "Buy" && Buy != order)
                  Buy = order;
                  if (order.Name == "Sell" && Sell != order)
                  Sell = order;
                  }​
                  Am I still missing something?​

                  Comment


                    #10
                    Hello diorfo,

                    Is the condition to submit the order still true at that time? if you are still submitting the order you won't be able to cancel it.

                    The method AssignOrderToVariable is assigning the variables but you never call that method in the code you provided. You would need to call that inside the OnOrderUpdate method.

                    Code:
                    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
                    {​
                        AssignOrderToVariable( ref order); 
                    
                    
                    }
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_Jesse View Post
                      Hello diorfo,

                      Is the condition to submit the order still true at that time? if you are still submitting the order you won't be able to cancel it.
                      The condition to place the order it's not getting true afterwards because it's a limit order generated when the market hits the specific entry price far away from the cancel region.

                      Code:
                      if (marketDataUpdate.Price == Price - (4 * TickSize))
                      {
                      Sell = EnterShortLimit(1, (Price - (4 * TickSize)), "Sell");
                      }​​
                      Looks like the CancelOrder command it's not find the Sell order to cancel it

                      Comment


                        #12
                        Originally posted by NinjaTrader_Jesse View Post

                        The method AssignOrderToVariable is assigning the variables but you never call that method in the code you provided. You would need to call that inside the OnOrderUpdate method.

                        Code:
                        protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
                        {​
                        AssignOrderToVariable( ref order);
                        
                        
                        }
                        I didn't understand what you meant.
                        Could you explain with for the sample I have put above?

                        Comment


                          #13
                          Hello diorfo,

                          The condition to place the order it's not getting true afterwards because it's a limit order generated when the market hits the specific entry price far away from the cancel region.
                          Code:
                          if (marketDataUpdate.Price == Price - (4 * TickSize))

                          This entry condition could become true multiple times as the price moves up and down. If the price hit this amount so the condition was true, then moved up 1 tick and then back down 1 tick the condition will have become true two times at that point. Your entry order may be getting called many times as the incoming data keeps checking the condition.



                          I didn't understand what you meant.
                          Could you explain with for the sample I have put above?​
                          I provided a sample of what you needed to add already. If you make a method you need to call it like the sample I provided. The sample you provided in post 1 is not complete, you never call the method AssignOrderToVariable in your code. If you are not sure how to use methods you should just remove that method and put that code directly inside OnOrderUpdate:

                          Code:
                          protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
                          {​
                              if (order.Name == "Buy" && Buy != order)
                                  Buy = order;
                              if (order.Name == "Sell" && Sell != order)
                                  Sell = order;​
                          }​
                          JesseNinjaTrader Customer Service

                          Comment


                            #14
                            The condition to place new order it's not getting true after the cancelling (see attached picture).

                            I just put 1 print to order placed and 1 print to order canceled to confirm that.

                            I proceeded as you described above and the order still not getting canceled.

                            Code:
                            protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
                            {​
                            if (order.Name == "Buy" && Buy != order)
                            Buy = order;
                            if (order.Name == "Sell" && Sell != order)
                            Sell = order;​
                            }​

                            Is there another way to guarantee that the order submited is the same I was canceling in OnMarketData event?

                            Comment


                              #15
                              Hello diorfo,

                              You can see a working example of cancelling an order here:
                              JesseNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by stafe, 04-15-2024, 08:34 PM
                              11 responses
                              57 views
                              0 likes
                              Last Post stafe
                              by stafe
                               
                              Started by pechtri, 06-22-2023, 02:31 AM
                              5 responses
                              115 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by Tim-c, Today, 03:54 AM
                              3 responses
                              13 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by FAQtrader, Today, 12:00 PM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by gnocchi, Today, 11:56 AM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Zachary  
                              Working...
                              X