• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unable to clean up an un-submitted Order object created with Account.CreateOrder.

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

    Unable to clean up an un-submitted Order object created with Account.CreateOrder.

    Using the documentation found

    https://ninjatrader.com/support/help...reateorder.htm

    the following code creates an order that cannot be deleted when using simulation accounts.

    Code:
    void SubmitAnOrder90
    {
        Order stopOrder;
        stopOrder = myAccount.CreateOrder(myInstrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, 1, 0, 1400, "myOCO", "stopOrder", Core.Globals.MaxDate, null);
    
        if(TestSomeMarketCondition() == false)
        {
            myAccount.Cancel(new[] { stopOrder });  // with or without this, we end up with an orphaned order
                                                    // that cannot be deleted, canceled or removed from simulation accounts
            return;   
        }
    
        myAccount.Submit(new[] { stopOrder });
    }
    The simulation account will either end up with an order stuck in the "Initialized" state, or an order stuck in the "Cancel pending" state. How does one properly clean up orders created with CreateOrder but never submitted?

    #2
    Hello ntbone,

    If the order was never submitted, you can set the variable to null. The garbage collection will clean up it up eventually. You will need to create a new order after that.

    Below is a link to an example you may find helpful.
    https://ninjatrader.com/support/foru...720#post820720
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks. I read through the thread and the example code in that thread but it dind't provide me with an answer.

      If I call CreateOrder, but then don't call Submit on that order, it will forever show up in the list of orders as initialized.
      If I call CreateOrder, but have a code path that decides not to submit, and instead I call Cancel, the order is left in an "cancel pending" state.

      After that, my only recourse to get rid of the orders is to reset my database. I have not tried this with a real broker vs a simulation account.

      I have found that if I explicitly set the order state to Canceled, then everything appears as expected.

      What I am looking for is the official way to clean up an order that is created with Account.CreateOrder but for which Account.Submit is never called.

      Comment


        #4
        Hello ntbone,

        There isn't code to remove orders from the database once they are created.

        If you don't want to submit them, i would recommend you do not create them.

        Creation and submission should happen at the same time.

        You can choose to ignore them if you want.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I understand that they should happen at the same time, but since the operations are not atomic there is always the possibility of error, without a chancel for clean up. Consider the follwoing

          Code:
          var order1 = account.CreateOrder(...);
          var ordre2 = account.CreateOrder(...);
          var order3 = account.CreateOrder(...);
          ...
          var order 8 = account.CreateOrder(...);
          
          account.Submit(new [] { order1, order2, order 3, ....order 8};
          There are problems that can come up during creation of those orders. CreateOrder could fail for some reason, an execption could be thrown while trying to figure out the information to be placed inside the orders.

          In this case the orders might have a one cancels all situation so it doesn't make sense to submit them one at a time.

          Not providing a means to properly clean up, perhaps in a catch(...) block, results in non-robust code. There doesn't nee to be code to necessarily delete the orders from the database. Just code to properly abort or cancel orders that have not been submitted. The API leaves developers up to writing code that can corrupt the database and leave pending orders without anyway to properly clean them up.

          Comment


            #6
            Hello ntbone,

            It shouldn't cause any issues to ignore the order. Set the variable to null, and then ignore the order. Once an order is created in the database, there isn't a way to remove it other than resetting the account.

            This is true for orders being made internally by the core of NinjaTrader as well.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              The problem is the orders remain in the initialized state which makes them stick out in the UI. Setting the state of the order explicitly to canceled seems to remedy this.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by CandySNK, Today, 10:24 PM
              0 responses
              3 views
              0 likes
              Last Post CandySNK  
              Started by user1986, Today, 07:28 PM
              2 responses
              11 views
              0 likes
              Last Post NinjaTrader_EricB  
              Started by ericson, Today, 08:14 PM
              0 responses
              7 views
              0 likes
              Last Post ericson
              by ericson
               
              Started by jertrade, Today, 06:57 PM
              0 responses
              5 views
              0 likes
              Last Post jertrade  
              Started by Emma1, Today, 02:42 PM
              1 response
              17 views
              0 likes
              Last Post NinjaTrader_DrewO  
              Working...
              X