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

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.
      Kate W.NinjaTrader Customer Service

      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.
              Kate W.NinjaTrader Customer Service

              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.
                    Kate W.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by judysamnt7, 03-13-2023, 09:11 AM
                    4 responses
                    59 views
                    0 likes
                    Last Post DynamicTest  
                    Started by ScottWalsh, Today, 06:52 PM
                    4 responses
                    36 views
                    0 likes
                    Last Post ScottWalsh  
                    Started by olisav57, Today, 07:39 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post olisav57  
                    Started by trilliantrader, Today, 03:01 PM
                    2 responses
                    21 views
                    0 likes
                    Last Post helpwanted  
                    Started by cre8able, Today, 07:24 PM
                    0 responses
                    10 views
                    0 likes
                    Last Post cre8able  
                    Working...
                    X