Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Questions about placing orders

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

    #16
    Originally posted by NinjaTrader_Brett View Post
    Hello,

    You would need to do this in your own code to correct the issue we discussed about earlier as there are limitations to how are exit on close works that unfortunately cannot be changed in our current NT release due to technical limitations however it is on developments list to look into for our next major release to make a change here.

    This is why I recommend doing any exit on close orders manually yourself hardcoded into the strategy so you can make sure you are doing these checks.

    -Brett
    OK Thanks. So how about this question:

    Regarding the orders, I am currently using this approach. Do you see any problems? I certainly could use the SetStopLoss() and profit target that you previously suggested, but I am just curious if this code works or not.

    private IOrder entryOrder = null;
    private IOrder stopLossOrder = null;
    private IOrder profitTargetOrder = null;


    protected override void OnBarUpdate()
    {
    entryOrder = EnterLongStop(0, true /*liveUntilCancelled*/, quantity, entryPrice, "long");
    stopLossOrder = ExitLongStop(0, true /*liveUntilCancelled*/, quantity, stopLossPrice, "long stop", "long");
    profitTargetOrder = ExitLongLimit(0, true /*liveUntilCancelled*/, quantity, targetPrice, "long target", "long");
    }

    protected override void OnExecution(IExecution execution)
    {
    if (stopLossOrder != null && stopLossOrder == execution.Order)
    {
    entryOrder = null;
    stopLossOrder = null;
    CancelOrder(profitTargetOrder);
    profitTargetOrder = null;
    entryOrderTime = null;
    }
    else if (profitTargetOrder != null && profitTargetOrder == execution.Order)
    {
    entryOrder = null;
    CancelOrder(stopLossOrder);
    stopLossOrder = null;
    profitTargetOrder = null;
    entryOrderTime = null;
    }
    }
    Last edited by zehua; 03-06-2012, 12:27 AM.

    Comment


      #17
      Hello,

      Sure, with your code this most likely would NOT work. Reason is order submission rules as the ExitLongLimit and ExitLongStop commands would be ignored by NinjaTrader if there was no position yet. Since orders take time to process on the brokers servers and return back to you most likely is not filled before you hit these lines in the NinjaScript execution.

      You would need to wait for the order to be filled then submit the stop loss and profit target. This is the entire model NinjaTrader is built upon as you CANNOT submit stop loss and profit targets until you are filled as you dont know several key items:

      A) You dont when or if you get filled

      B) You dont know if its a part fill or a full fill to determine what QTY you need to protect.


      This is why are system is setup the way it is. Therefor you take a chance when you submit orders that you are getting it for the right amounts otherwise you may run the risk of getting overfilled on a stop or target if you only got partfilled leaving you in an unexpected position.

      Therefor this is why SetStopLoss() is used this does all this for you and submits the order when the order is filled for the correct filled ammount. You can code your own version if you like inside OnExecution and OnOrderUpdate() if you prefer however and even switch into unmanaged code where you can submit the orders and then do the code flow you are asking on with the un-managed code.

      Comment


        #18
        Originally posted by NinjaTrader_Brett View Post
        Hello,

        Sure, with your code this most likely would NOT work. Reason is order submission rules as the ExitLongLimit and ExitLongStop commands would be ignored by NinjaTrader if there was no position yet. Since orders take time to process on the brokers servers and return back to you most likely is not filled before you hit these lines in the NinjaScript execution.

        You would need to wait for the order to be filled then submit the stop loss and profit target. This is the entire model NinjaTrader is built upon as you CANNOT submit stop loss and profit targets until you are filled as you dont know several key items:

        A) You dont when or if you get filled

        B) You dont know if its a part fill or a full fill to determine what QTY you need to protect.


        This is why are system is setup the way it is. Therefor you take a chance when you submit orders that you are getting it for the right amounts otherwise you may run the risk of getting overfilled on a stop or target if you only got partfilled leaving you in an unexpected position.

        Therefor this is why SetStopLoss() is used this does all this for you and submits the order when the order is filled for the correct filled ammount. You can code your own version if you like inside OnExecution and OnOrderUpdate() if you prefer however and even switch into unmanaged code where you can submit the orders and then do the code flow you are asking on with the un-managed code.

        http://www.ninjatrader.com/support/h...tsub=unmanaged
        Aha. Got it. Thanks a lot! That makes sense. I will try your example with SetStopLoss() first and see how it goes.

        Since you mentioned there are a few brokers that may allow entry and stop loss orders to be submitted together, I am wondering if IB is one of those? (Or maybe I misunderstood you) IB has its own APIs, so maybe I can import their APIs in ninjascript and use those APIs for order submission.

        Comment


          #19
          Glad to hear you understand the stop and target submission model.

          TDA is the only broker that requires both stop and target submitted at the same time. Otherwise if you call both lines of code at the same time to submit the orders it is virtually the same thing. You are looking a very small period of time difference on executing code, the kind of time difference that you wouldn't be able to see a difference.

          IB allows native OCO however on their servers, meaning that one cancels other functionality will run regardless on if your PC is connected or not which is a good thing.

          -Brett

          Comment


            #20
            Originally posted by NinjaTrader_Brett View Post
            Hello,

            You would need to do this in your own code to correct the issue we discussed about earlier as there are limitations to how are exit on close works that unfortunately cannot be changed in our current NT release due to technical limitations however it is on developments list to look into for our next major release to make a change here.

            This is why I recommend doing any exit on close orders manually yourself hardcoded into the strategy so you can make sure you are doing these checks.

            -Brett

            It is a little bit weird that in this post you kept telling me submitting an entry limit order with a stop loss and profit target order in one atomic operation is impossible.
            AtmStrategyCreate() seems to be doing exactly this. I just need to open an account in IB to make sure this is a true OCO order instead of local machine simulation, which is doable for me.
            So you guys aren't event familiar with your own APIs?

            Also AtmStrategyClose() says it first tries to find the matching profit target order, and modify that to close out the position. If not found, then it is going to submit a market sell order.
            But when ExitOnClose is set to true, you don't use the above machenism to close out, but to always submit market orders to close out, and this could cause bugs when your market order is submitted and at the same time the stop loss is triggered, leaving user a non-flat position.

            If I always stick with AtmStrategyCreate(), and then set ExitOnClose to false, and then when market is approaching end of session, I call AtmStrategyClose, is it going to guarantee a flat position at the end of the session?

            Right now I am already setting ExitOnClose to false, but during backtesting, the system still closes my positions at the end of the session. Why?

            Comment


              #21
              Hello,

              Part of the confusion in lies in the terminology being used.

              Can you please clarify exactly the sequence of events you are referring to when you mention atomic operation so that I can offer an answer to match your understanding.

              Most likely based on the history of this thread ATM Strategies will not do what you are referring to for atomic operation. As the ATM strategy will not submit orders for the PT and SL until your entry order is filled.

              What I refer to in atomic operation in a single command that will execute all legs. For stop loss and profit submission even with ATM's require at minimum two command line executions.

              Would not use ExitOnClose with ATM Strategies and you would need to make sure to check your settings on the backtest we would need to treat that as a separate item where you turn on TraceOrders to see why the order is being submitted to close.



              -Brett
              Last edited by NinjaTrader_Brett; 03-17-2012, 10:28 PM.

              Comment


                #22
                Originally posted by NinjaTrader_Brett View Post
                Hello,

                Part of the confusion in lies in the terminology being used.

                Can you please clarify exactly the sequence of events you are referring to when you mention atomic operation so that I can offer an answer to match your understanding.

                Most likely based on the history of this thread ATM Strategies will not do what you are referring to for atomic operation. As the ATM strategy will not submit orders for the PT and SL until your entry order is filled.

                What I refer to in atomic operation in a single command that will execute all legs. For stop loss and profit submission even with ATM's require at minimum two command line executions.

                -Brett
                Yes. We mean the same thing for atomic operation. I want the entry limit order to be submitted along with the stop loss order and profit target order. So this means if my internet crashes, either my orders are all in, or none of them are in.

                OK. I understand the limitation of ATM. So for AtmStrategyCreate(), at least I can put in the entry limit order with the stop loss order in one atomic operation for Interactive Brokers?

                From your post here:
                Note: This information is relevant for NinjaTrader 7 only. For NinjaTrader 8, please click here (https://ninjatrader.com/support/helpGuides/nt8/where_do_your_orders_reside_.htm). CQG Orders in a state "Accepted" or "Working" are at the exchange. If the exchange does not support a specific order type, the

                My understanding is that for Interactive broker, you can do this for me. But for other brokers, you are simulating this by holding the stop loss and profit target orders on the local machine until the execution of the entry order is confirmed.
                Last edited by zehua; 03-17-2012, 10:33 PM.

                Comment


                  #23
                  Originally posted by zehua View Post
                  Yes. We mean the same thing for atomic operation. I want the entry limit order to be submitted along with the stop loss order and profit target order. So this means if my internet crashes, either my orders are all in, or none of them are in.

                  OK. I understand the limitation of ATM. So for AtmStrategyCreate(), at least I can put in the entry limit order with the stop loss order in one atomic operation for Interactive Brokers?

                  From your post here:
                  Note: This information is relevant for NinjaTrader 7 only. For NinjaTrader 8, please click here (https://ninjatrader.com/support/helpGuides/nt8/where_do_your_orders_reside_.htm). CQG Orders in a state "Accepted" or "Working" are at the exchange. If the exchange does not support a specific order type, the

                  My understanding is that for Interactive broker, you can do this for me. But for other brokers, you are simulating this by holding the stop loss and profit target orders on the local machine until the execution of the entry order is confirmed.
                  There is no(!) order type that will submit stops and targets at the same time as the stop loss in NinjaTrader unfortunately.

                  1) Placing ENTRY then STOP AND TARGET at the same time.

                  ->NT is not able to do the atomic operation with any of our providers.

                  2).Placing STOP AND TARGET at the same time.

                  ->NT can do this atomic operation with some providers and can put OCO on the servers as well with some providers. IB can keep the OCO on their servers and TDA will send the stops and targets in 1 call.


                  ATM's will wait for the entry to be filled and then submit the stop and targets after filled once you know how much you are filled.

                  -Brett

                  Comment


                    #24
                    Entry orders and OCO - where are stored ???

                    Originally posted by NinjaTrader_Brett View Post

                    ATM's will wait for the entry to be filled and then submit the stop and targets after filled once you know how much you are filled.

                    -Brett
                    Hello guys,
                    I am reading this thread and I am a little confused. I am still in SIM mode (NT7 and IB account).

                    I am using ATM strategy to atach Target and Stop for my Entry (I have Interactive Brokers for data)
                    What will happend?

                    1) I set ENTRY (BUY/SELL LMT) - my execution is waiting for fill. Where my order is waiting? In my platform or in my Brookers platform (TWS) / server ???
                    2) If it wait on IB server / TWS platform, SL and PT will be placed only after my entry order was filled, Question is: Why NT7 cannot send both SL and PT OCO order with Entry order at the same time?

                    Why my questions? For example this situation.
                    I have Full NT7 and Live account.
                    15:30 I will place BUY LMT order.
                    15:35 My Internet fail
                    15:40 meanwhile BUY LMT was filled.
                    18:00 Market goes 200ticks down without activating my SL.

                    If the OCO PT&SL will be send after entry wass filled, I will loose hundreds of dolars!
                    No Internet connection, no OCO SL and PT orders cannot be send to IB server.

                    Where is the truth?
                    Last edited by Jaroslav; 03-20-2012, 01:52 PM.

                    Comment


                      #25
                      Hello,

                      Thanks for the forum post let me answer them below.


                      Originally posted by Jaroslav View Post
                      Hello guys,
                      I am reading this thread and I am a little confused. I am still in SIM mode (NT7 and IB account).

                      I am using ATM strategy to atach Target and Stop for my Entry (I have Interactive Brokers for data)
                      What will happend?

                      1) I set ENTRY (BUY/SELL LMT) - my execution is waiting for fill. Where my order is waiting? In my platform or in my Brookers platform (TWS) / server ???

                      [Brett] - TWS server for the ENTRY ORDER only.

                      2) If it wait on IB server / TWS platform, SL and PT will be placed only after my entry order was filled, Question is: Why NT7 cannot send both SL and PT OCO order with Entry order at the same time?

                      [Brett] - The reason why this is not done as what if your entry order does not fill. Instead the market moves down lower and ends up filling your stop loss instead. Then you are left with an unprotected short position.

                      Why my questions? For example this situation.
                      I have Full NT7 and Live account.
                      15:30 I will place BUY LMT order.
                      15:35 My Internet fail
                      15:40 meanwhile BUY LMT was filled.
                      18:00 Market goes 200ticks down without activating my SL.

                      If the OCO PT&SL will be send after entry wass filled, I will loose hundreds of dolars!
                      No Internet connection, no OCO SL and PT orders cannot be send to IB server.

                      Where is the truth?

                      [Brett] - Correct you will want to not step away form the PC when trading and have your brokers 24 hour phone desk ready if you needed to place a phone order. These are the risks of electronic trading.


                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by AttiM, 02-14-2024, 05:20 PM
                      12 responses
                      213 views
                      0 likes
                      Last Post DrakeiJosh  
                      Started by cre8able, 02-11-2023, 05:43 PM
                      3 responses
                      237 views
                      0 likes
                      Last Post rhubear
                      by rhubear
                       
                      Started by frslvr, 04-11-2024, 07:26 AM
                      8 responses
                      117 views
                      1 like
                      Last Post NinjaTrader_BrandonH  
                      Started by stafe, 04-15-2024, 08:34 PM
                      10 responses
                      47 views
                      0 likes
                      Last Post stafe
                      by stafe
                       
                      Started by rocketman7, Today, 09:41 AM
                      3 responses
                      12 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Working...
                      X