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

Cancel All Orders NinjaTrader 8 Strategy

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

    Cancel All Orders NinjaTrader 8 Strategy

    Hello, would someone please be able to tell me what the NT8 syntax would be to cancel all orders OnBarUpdate()? Does CancelAllOrders(bool entries, bool exits); not work for NT8?

    #2
    Hello RobotSyndicate, thanks for your post.

    I moved this to a new thread because the original thread was 4 years old.

    To cancel all orders from the strategy you can loop through the orders in the account and cancel them, or keep a list of only the orders that the strategy has created and cancel all that are in State.Working or State.Accepted e.g.

    foreach (Order order in Account.Orders) //this will cancel all orders on the account.
    {
    if(order.OrderState == OrderState.Working || order.OrderState == OrderState.Accepted)
    {
    CancelOrder(order);
    }
    }

    We have an official example of using CancelOrder here:
    https://ninjatrader.com/support/help...thod_to_ca.htm

    Please let me know if I can provide any further information.

    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris, thank you for your help! So how would I do what you mentioned and alter that code above so that the strategy just cancels the working and accepted orders that it created, not all of the orders on the account?

      Thanks again

      Comment


        #4
        Hello RobotSyndicate, thanks for your reply.

        Your strategy first needs to hold a list of orders in a List<Order> data structure. Whenever you create an order, add that order to the list. When the time comes to cancel all orders do

        foreach(Order order in MyOrdersList)
        {
        if(order.OrderState == OrderState.Working || order.OrderState == OrderState.Accepted)
        {
        CancelOrder(order);
        }
        }

        Here is a publicly available link to the List<T> documentation:

        https://docs.microsoft.com/en-us/dot...1?view=net-5.0

        Please let me know if I can provide any further information.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Thank you Chris.

          So would I create a list like this?

          private List<Order> MyOrdersList = new List<Order>();


          Then when I am adding an order to the list, I assume in OnOrderUpdate(), would I use something like this?

          if (order.Name == "longOrder")
          {
          longOrder = order;
          MyOrdersList.Add(new Order() {ID = Order, OrderState = Order.OrderState}); //<----- All I want to keep track of is the OrderState. Or I guess I wouldn't keep order state in the list, because orderstate is dynamic? So I would just keep a list of the Orders and then as long as I can call up the Orders I want then I can evaluate their OrderState that NT is keep track of? In that case would I just use: MyOrdersList.Add(Order); ?

          // Reset the entryOrder object to null if order was cancelled without any fill
          if (order.OrderState == OrderState.Cancelled && order.Filled == 0)

          {
          longOrder = null;
          }

          }

          Then where I want to in OnBarUpdate(), I would use this?

          foreach(Order order in MyOrdersList)
          {
          if(order.OrderState == OrderState.Working || order.OrderState == OrderState.Accepted)
          {
          CancelOrder(order);
          }
          }

          Also, would I have to have something to check that the Order isn't already in the list?


          When I try compiling with that above, I get errors that "NinjaTrader.Cbi.Order dows not contain a definition for ID", even though I have defined ID as a private string. I also get the error "NinjaTrader.Cbi.Order is a type but is being used like a variable".


          Thanks again so much for your help! I greatly appreciate it!


          UPDATE:

          I might be closer to figuring it out, but I kept the original stuff up top in case its helpful to someone later. So now I am using this in OnOrderUpdate()

          if (order.Name == "longOrder")
          {
          longOrder = order;
          MyOrdersList.Add(order);

          // Reset the entryOrder object to null if order was cancelled without any fill
          if (order.OrderState == OrderState.Cancelled && order.Filled == 0)

          {
          longOrder = null;
          }

          }

          And then I am using this in OnBarUpdate()

          foreach(Order order in MyOrdersList)
          {
          if(order.OrderState == OrderState.Working || order.OrderState == OrderState.Accepted)
          {
          CancelOrder(order);
          }
          }

          It seems to be working with a simulation account on live data, but I am still wondering if I have to check to make sure I am not adding duplicates to the list. It looks like I am, and I'm wondering if when I run this live will I get a barrage or errors about canceling orders that have already been canceled. Thoughts?
          Last edited by RobotSyndicate; 12-11-2020, 07:56 AM.

          Comment


            #6
            Hello RobotSyndicate, thanks for your reply.

            Each order should have a unique, pre-defined signal name so they can be identified as duplicates. The name will be stored within order.Name as a string.

            The list will need to be updated with List.Remove(Order)(publicly available link). Once an order in the list is in a terminal state (Filled, Canceled, Rejected), remove it from the list.

            Please let me know if there is anything else I can assist with.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Perfect, thanks Chris!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by The_Sec, Today, 03:37 PM
              0 responses
              3 views
              0 likes
              Last Post The_Sec
              by The_Sec
               
              Started by GwFutures1988, Today, 02:48 PM
              1 response
              5 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Started by ScottWalsh, 04-16-2024, 04:29 PM
              6 responses
              32 views
              0 likes
              Last Post ScottWalsh  
              Started by frankthearm, Today, 09:08 AM
              10 responses
              36 views
              0 likes
              Last Post frankthearm  
              Started by mmenigma, Today, 02:22 PM
              1 response
              4 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Working...
              X