Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Is there a method in NT8 similar to NT7's CancelAllOrders()?

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

    Is there a method in NT8 similar to NT7's CancelAllOrders()?

    Hello

    Is there a method in NT8 to cancel all existing EntryOrders without creating a list of those orders? In NT7 CancelAllOrders() exists.

    I think I can create a list of my existing orders and loop through that to cancel each order.
    But I am looking for something like the following that closes a long position but that would cancel all existing orders:

    if (some condition)
    {
    ExitLong("");
    }



    If there is not a similar method, does it matter if I cancel orders like this:


    if (some condition)
    {
    CancelOrder(long1EntryOrder);
    CancelOrder(long2EntryOrder);
    CancelOrder(long3EntryOrder);
    }

    // and allowing the entry orders to be reset to null in OnOrderUpdate



    Or is using the following code preferred to first check if the order exists before cancelling it?


    if (long1EntryOrder != null)
    {
    CancelOrder(long1EntryOrder);
    }

    if (long2EntryOrder != null)
    {
    CancelOrder(long2EntryOrder);
    }

    if (long3EntryOrder != null)
    {
    CancelOrder(long3EntryOrder);
    }

    // while again allowing the orders to be reset to null in OnOrderUpdate as they are cancelled



    Thank you for any help!

    #2
    Hi.

    To do so, I first instantiate an empty list of Instruments :
    Collection<Cbi.Instrument> instrumentsToCancel = new Collection<Instrument>();

    Then every time an order is filled, I store the concerned instrument in that collection :
    private void OnPositionUpdate(object sender, PositionEventArgs e)
    {
    instrumentsToCancel.Add(e.Position.Instrument);
    }

    Then, to Cancel all the positions, I use the Flatten method :
    lAccount.Flatten(instrumentsToCancel);

    Hope that will help you.

    Comment


      #3
      Hello 9817_dynamic,

      Thank you for your post.

      CancelAllOrders() is available under the Account object. This will cancel all orders on the account, not limited to the strategy.

      This method is documented here: https://ninjatrader.com/support/help...lallorders.htm

      For cancelling orders for a strategy itself, I would recommend tracking the orders with Order objects and cancelling those orders with your own method that will call CancelOrder() for each of those orders. The SampleOnOrderUpdate strategy for NT8 demonstrates how such a method can be created and how Order objects can be used.

      CancelOrder() - https://ninjatrader.com/support/help...ancelorder.htm

      SampleOnOrderUpdate - https://ninjatrader.com/support/foru...ead.php?t=7499

      Also worth mentioning is the CloseStrategy() method that will cancel order and close open positions of that strategy.

      CloseStrategy() - https://ninjatrader.com/support/help...sestrategy.htm

      Please let me know if I may be of further assistance to you.

      Comment


        #4
        Thank you thanajo! You rock!

        I knew there was a way to do this, but I had not looked into it yet. I have only been programming for about 6 months.
        You explained this very well. I will play around with it.

        That is very helpful!

        Comment


          #5
          Thank you NinjaTrader_Kate.

          I should have mentioned I am attempting to only cancel the orders in a specific strategy and not the entire account.

          I will use what I know for the moment and work on creating a list as thanajo suggested when I get some time to do so.

          Thank you for the help!

          Comment


            #6
            Originally posted by thanajo View Post
            Hi.

            To do so, I first instantiate an empty list of Instruments :
            Collection<Cbi.Instrument> instrumentsToCancel = new Collection<Instrument>();

            Then every time an order is filled, I store the concerned instrument in that collection :
            private void OnPositionUpdate(object sender, PositionEventArgs e)
            {
            instrumentsToCancel.Add(e.Position.Instrument);
            }

            Then, to Cancel all the positions, I use the Flatten method :
            lAccount.Flatten(instrumentsToCancel);

            Hope that will help you.
            Thanks for this explanation. I have a question: Let's say some of the initial positions are now filled by profit target but the remaining positions are still open. I want to flatten the remaining positions without closing the strategy using a ExitLong strategy button. Will the lAccount.Flatten(instrumentsToCancel); cancel those remaining positions?

            Comment


              #7
              Hello set2win,

              Thank you for your reply.

              The Flatten Everything command, either invoked through the Control Center by right-clicking a position on the positions tab and selecting Flatten Everything, or by invoking through code, will cause any NinjaScript Strategies to be disabled.

              Instead, I would recommend you check the positions and place an order of the same size in the opposite direction to flatten any open position through code.

              Please let us know if we may be of further assistance to you.

              Comment


                #8
                Hi thanajo ,
                Where to place those statements in the strategy like the Collection... , the Private void...
                I added using System.Collections.ObjectModel;
                but not sure where each statement should go, I keeping getting an error: instrumentsToCancel does not exist in current context

                Comment


                  #9
                  Hi Kate W.
                  I see what you are saying about Flatten/ So after entry I have 3 orders with 3 different PT and one StopLoss order. So how do I cancel those PT orders and the StopLoss?

                  Comment


                    #10
                    Hello set2win,

                    Thank you for your reply.

                    If you're closing out of the position you're in, simply submitting an order in the opposite direction of the position will close the position. When the position is closed, the profit target and stop loss orders would be automatically cancelled if you're using the managed approach. If you are using the unmanaged approach, you would need to be tracking the stop and target using order objects and use CancelOrder to explicitly cancel those orders:



                    Please let us know if we may be of further assistance to you.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by NullPointStrategies, Yesterday, 05:17 AM
                    0 responses
                    55 views
                    0 likes
                    Last Post NullPointStrategies  
                    Started by argusthome, 03-08-2026, 10:06 AM
                    0 responses
                    132 views
                    0 likes
                    Last Post argusthome  
                    Started by NabilKhattabi, 03-06-2026, 11:18 AM
                    0 responses
                    73 views
                    0 likes
                    Last Post NabilKhattabi  
                    Started by Deep42, 03-06-2026, 12:28 AM
                    0 responses
                    45 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