Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

IOrder, SubmitOrder

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

    IOrder, SubmitOrder

    I'm not understanding how to identify and close/cancel an open order. Example :

    IOrder openBuy = SubmitOrder(0,OrderAction.Buy,OrderType.Market,100 ,0,0,"","openBuy");

    // some logic..if(true) close openBuy
    IOrder closeBuy = SubmitOrder(0,...
    or

    at this point how do I specify to SubmitOrder to close "openBuy" order?

    #2
    Hello,

    In the case of your example, a Market order should should fill to put you in a position. If you are referring to closing the position you would need to submit an order in the opposite direction.

    If you had a Buy order and have become Long, you would need to submit a Sell order to become flat once again.

    If you are using other order types that would be a working order like a Limit order, you could use the IOrder and Cancel the order if needed before it fills. http://ninjatrader.com/support/helpG...ancelorder.htm



    I look forward to being of further assistance.

    Comment


      #3
      I'm not understanding how do I close and open order using SubmitOrder Unmanaged = true. Example:

      IOrder openBuy;
      IOrder closeBuy;
      IOrder buyStopLoss;

      openBuy =SubmitOrder(0,OrderAction.Buy,OrderType.Market,10 0,0,stopPrice,"","openBuy");

      If I want to close "openBuy" How do I close it?
      Last edited by d.allen101; 11-02-2016, 04:08 PM.

      Comment


        #4
        Originally posted by d.allen101 View Post
        I'm not understanding how do I close and open order using SubmitOrder Unmanaged = true. Example:

        Code:
        IOrder openBuy;
        IOrder closeBuy;
        IOrder buyStopLoss;
        
        openBuy = SubmitOrder(0,OrderAction.Buy,OrderType.Market,100,0,stopPrice,"","openBuy");
        If I want to close "openBuy" How do I close it?
        You don't yet, you wait until openBuy gets filled.

        [Btw, don't use a variable name like "stopPrice" in your above code. It's useless to put anything there when you're using a Market order, just specify 0. I mean, since you hardcoded Market, just go ahead and hardcode a zero, too, rather than stopPrice (which, when you think about it, is a damn poor name to begin with -- this being a Market entry order).]

        The reference to the IOrder made by openBuy becomes useless once that order's state becomes "Filled". In other words, your openBuy serves no other purpose than to get filled. It's only use is to serve as an entry order -- the 'close' order will be done by a different IOrder reference. The system lifespan of openBuy terminates when its state becomes "Filled".

        So, how do you close it? Well, as soon as openBuy becomes "Filled", which you detect inside OnExecution(), you'll want to create 2 new IOrder references, one for your profit target exit order, and another for your stop loss exit order.

        Something like,

        Code:
        StopOrder = SubmitOrder(0,OrderAction.Sell,OrderType.Stop,100,0,stopPrice,"","stp");
        and

        Code:
        TargetOrder = SubmitOrder(0,OrderAction.Sell,OrderType.Limit,100,0,targetPrice,"","tgt");
        where stopPrice and targetPrice are calculated (on the fly, inside OnExecution) once the EntryPrice is known.

        Comment


          #5
          Assuming it's opened/Filled...I'm not looking to close it the OnExecution handler. I want to close it OnBarUpdate according to some trade logic. How do I close it?

          Comment


            #6
            Is the Entry order identified by the "ocoID"

            Comment


              #7
              Originally posted by d.allen101 View Post
              Assuming it's opened/Filled...I'm not looking to close it the OnExecution handler. I want to close it OnBarUpdate according to some trade logic. How do I close it?
              In that case, since you are LONG, just use a Market Sell order.

              Code:
              exitBuy = SubmitOrder(0,OrderAction.Sell,OrderType.Market,100,0,0,"","exitBuy");
              Last edited by bltdavid; 11-02-2016, 06:34 PM.

              Comment


                #8
                So I don't have to identify or specify the order that I'm closing? I just simply call SubmitOrder with a OrderType.Sell and it "SubmitOrder" automatically closes any open long trades?

                Comment


                  #9
                  Originally posted by d.allen101 View Post
                  Is the Entry order identified by the "ocoID"
                  Short answer:
                  No.

                  Long Answer #1:
                  If this is a single entry order, then "No", the OcoId serves no purpose, so just use "".

                  Long Answer #2:
                  If you are using 2 entry orders to create a "bracket" order, then "Yes", specify a unique string for the OcoId.

                  If you wanted a bracket order, where the first filled entry order cancels the other unfilled entry order, you call SubmitOrder twice to create to the two IOrder references, but you must specify the same OcoId string for both orders.

                  [Edit: A bracket order always involves two orders. It can be used as an entry technique where two entry orders "bracket" current price, perhaps in anticipation of some news event. For ex, a LONG BUY STOP 10 ticks above current price and SHORT SELL STOP 10 ticks below current price using the same OcoId is a bracket order. After one of the orders becomes "Filled", the OCO feature (using the OcoId) automatically cancels the other order without any further code needed by you, it's all done via the OcoId being the same for both orders created by SubmitOrder.]

                  Summary:
                  If you're not using bracket orders for entry, then your single entry order does not need an OcoId, so just specify an empty string.
                  Last edited by bltdavid; 11-02-2016, 07:41 PM.

                  Comment


                    #10
                    Originally posted by d.allen101 View Post
                    So I don't have to identify or specify the order that I'm closing? I just simply call SubmitOrder with a OrderType.Sell and it "SubmitOrder" automatically closes any open long trades?
                    Yes, but your wording above may be getting in the way of your understanding.

                    [Once openBuy is filled and you have an "open long trade", I know what you mean, but don't say it that way.

                    Say it this way: you have "opened a long position".

                    If you add more contracts/shares/whatever to that open position (aka you make more trades), it doesn't change the fact that you only have ONE position, and it is LONG.

                    You have "trades" above, as plural. This is a mistake in your thinking. You have one position (per instrument). "Position" is singular, and it is either LONG or SHORT or FLAT. Adopting this world view helps to better understand the NinjaScript model.

                    Besides, use of the word "trade" should be reserved for what you see in the Trades tab of Strategy Analyzer. Getting a good handle on the correct words to use helps keep the concepts clearer.]

                    So, the real answer to your question is: you have total control over exiting your position because you specify the quantity to sell.

                    You want to exit the entire position? No problem, then make sure quantity is set to all contracts in that position. If you want to keep the position open, but reduce the size of the position, then specify a smaller amount.

                    When using a Market Sell order, NinjaTrader knows to exit the entire position because the quantity you specify tells it to.
                    Last edited by bltdavid; 11-02-2016, 07:47 PM.

                    Comment


                      #11
                      Example:

                      IOrder openBuy;
                      IOrder closeBuy;
                      IOrder buyStopLoss;
                      IOrder openSell;
                      IOrder closeSell;
                      IOrder sellStopLoss;

                      OnBarUpdate()
                      {
                      if (Close[0] > Close[4] && openBuyOrd == null)
                      {
                      if (openSellOrd != null)
                      closeSell = SubmitOrder(...

                      openBuy = SubmitOrder(...
                      }

                      if (Close[0] < Close[4] && openSell == null)
                      {
                      if (openBuy != null)
                      closeBuy = SubmitOrder(...

                      openSell = SubmitOrder(...
                      }
                      }

                      OnOrderUpdate(order)
                      {
                      // more logic..
                      }

                      OnExecution(execution)
                      {
                      if (openBuy = execution && openBuy.OrderState == OrderState.Filled && blah, blah, blah)
                      buyStopLoss = SubmitOrder(...

                      // more logic...blah, blah, blah
                      }

                      How do I close those openBuy and openSell orders? This logic isn't working correctly. I'm getting filled twice because my orders aren't being closed sometimes...

                      Comment


                        #12
                        Ok, thank you. I think you clarified it for it.

                        Comment


                          #13
                          Originally posted by d.allen101 View Post
                          How do I close those openBuy and openSell orders?
                          Are you asking how to close a position?

                          Once an order is filled, you don't 'close' the order, you close the position.

                          Comment


                            #14
                            Ok thank you. Yes, I meant/should have said position and not order.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            648 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            369 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by Mindset, 02-09-2026, 11:44 AM
                            0 responses
                            108 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                            0 responses
                            572 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by RFrosty, 01-28-2026, 06:49 PM
                            0 responses
                            573 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X