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

CancelOrder() Not working

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

    CancelOrder() Not working

    Hello,
    I have tried to Cancel a working stop Loss order and it is not working.
    Have tried a ton of different ways and will attach code below.

    I am essentially trying to cancel a stop loss order that is working/accepted so that it does not fill overnight.
    If there is an easier way to do this let me know, but ninja is filling stop loss orders during Trading Hours that I would not like to trade.
    I realize it is just a working order so it is just doing what it is told, just giving you some background.
    Would all be remedied if this order was able to be canceled and I could resend it on the first bar of the next trading day.

    Let me know what the issue is please!

    This is in my OnOrderUpdate()
    if (order.Name == "Stop loss"){
    myEntryOrder = order;
    MyOrdersList.Add(order);
    }


    if (myEntryOrder != null && myEntryOrder == order){
    // Check if myEntryOrder is cancelled.
    if (myEntryOrder.OrderState == OrderState.Cancelled){
    // Reset myEntryOrder back to null
    myEntryOrder = null;
    }
    }

    This is in my OnBarUpdate()
    if(Time[0].Minute == 16){
    foreach(Order order in MyOrdersList){
    if(order.OrderState == OrderState.Working || order.OrderState == OrderState.Accepted){
    CancelOrder(order);
    }
    }
    Print("Should Cancel");
    }

    Also just in case, of course I have initialized the list

    #2
    Hello Vikuno1,

    Welcome to the NinjaTrader forums!

    You cannot cancel a Set method order. Use ExitLongStopMarket()/ExitShortStopMarket/SubmitOrderUnmanaged() if you want to be able to cancel the order later.

    With you code:
    if (order.Name == "Stop loss"){
    myEntryOrder = order;

    This doesn't make sense for the variable names. You are assigning the stop loss order to a variable with the name myEntryOrder. This would be a exit order, not an entry order, which can't be cancelled anyway.

    Below is a link to an example that has code to cancel orders.
    https://ninjatrader.com/support/foru...269#post802269
    Last edited by NinjaTrader_ChelseaB; 08-16-2022, 09:46 AM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello Vikuno1,

      Welcome to the NinjaTrader forums!

      You cannot cannot a Set method order. Use ExitLongStopMarket()/ExitShortStopMarket/SubmitOrderUnmanaged() if you want to be able to cancel the order later.

      With you code:
      if (order.Name == "Stop loss"){
      myEntryOrder = order;

      This doesn't make sense for the variable names. You are assigning the stop loss order to a variable with the name myEntryOrder. This would be a exit order, not an entry order, which can't be cancelled anyway.

      Below is a link to an example that has code to cancel orders.
      https://ninjatrader.com/support/foru...269#post802269
      Hi Chelsea,

      Thank you for your response!
      I realize that the variable name does not make sense but that does not matter in this context (was just trying to duplicate the example code from the language reference).

      Are you saying that Stop loss orders cannot be canceled?

      Comment


        #4
        Hello Vikuno1,

        An order submitted with SetStopLoss() cannot be cancelled.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello Vikuno1,

          An order submitted with SetStopLoss() cannot be cancelled.
          Alright, What about ChangeOrder() ?

          The Language reference uses this code
          // Raise stop loss to breakeven when you are at least 4 ticks in profit
          if(stopOrder!=null&&stopOrder.StopPrice<Position.AveragePrice&&Cl ose[0]>=Position.AveragePrice+4*TickSize)
          ChangeOrder(stopOrder,stopOrder.Quantity,0,Position.AveragePrice);


          I have replicated to this

          if(Time[0].Minute == 4 && Position.MarketPosition == MarketPosition.Long){
          ChangeOrder(stopOrder,stopOrder.Quantity,0,1000.0) ;
          Print("Should change to: " + (Position.AveragePrice - 1000));
          }

          And am getting no change in order. Can Stop loss orders be changed like in the reference?

          Thanks so much for you help btw!

          Comment


            #6
            Hello Vikuno1,

            Orders placed with set methods are not intended to be modified or cancelled. These are set methods, as in set-and-forget, NinjaTrader will manage them.

            If you want to modify an order price from a set method, call the set method again with a new price.

            If you want to control an order directly with CancelOrder() or ChangeOrder() use an order method and not a set method as advised in post #2.
            Last edited by NinjaTrader_ChelseaB; 08-16-2022, 11:46 AM.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Awesome, Understood. Thank you for your help!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Christopher_R, Today, 12:29 AM
              0 responses
              6 views
              0 likes
              Last Post Christopher_R  
              Started by sidlercom80, 10-28-2023, 08:49 AM
              166 responses
              2,235 views
              0 likes
              Last Post sidlercom80  
              Started by thread, Yesterday, 11:58 PM
              0 responses
              3 views
              0 likes
              Last Post thread
              by thread
               
              Started by jclose, Yesterday, 09:37 PM
              0 responses
              7 views
              0 likes
              Last Post jclose
              by jclose
               
              Started by WeyldFalcon, 08-07-2020, 06:13 AM
              10 responses
              1,415 views
              0 likes
              Last Post Traderontheroad  
              Working...
              X