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

Does partial fill canceling OCO?

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

    Does partial fill canceling OCO?

    Hello

    If one of OCO orders partial filled - what happened after?
    Does it take effect on other OCO orders?

    What if 2 protective OCO orders working for long position of 5 lots.
    Then, tatget order partial filled with 3 lots.
    Then price drop deep down and stop order filled.
    What would be result?

    I suppose 2 lots short position will remain.
    Isn’t it?
    Last edited by fx.practic; 05-28-2018, 02:39 PM.
    fx.practic
    NinjaTrader Ecosystem Vendor - fx.practic

    #2
    Hello fx.practic,

    Thank you for your post.

    If you are using the SetStopLoss and SetProfitTarget options then the adjustments to the order quantity will be automatic and appropriate.

    If you are using Exit methods such as ExitLongLimit or ExitShortStopMarket and do not specify a quantity in their overloads then they too will adjust automatically.

    If you are using the Unmanaged Order Approach then you need to implement logic to handle the partial fills and adjust the orders.

    For information on the Order methods and Approaches please visit the following link: https://ninjatrader.com/support/help...er_methods.htm

    Please let me know if you have any questions.

    Comment


      #3
      I mean unmanaged approach.

      What is correct way to handle partial fills of protective OCO orders (profit target and stop loss)?

      I suppose, strategy should monitor OCO orders partial executions in OnExecutionUpdate() method and change all OCO-bounded orders on each execution.Order.OrderState.PartFilled.

      Is this right?
      Last edited by fx.practic; 05-28-2018, 03:13 PM.
      fx.practic
      NinjaTrader Ecosystem Vendor - fx.practic

      Comment


        #4
        Hello fx.practic,

        Thank you for your response.

        You are correct. You would use OnExecutionUpdate() and the execution.Order properties.

        Please let me know if you have any questions.

        Comment


          #5
          Thank You.
          fx.practic
          NinjaTrader Ecosystem Vendor - fx.practic

          Comment


            #6
            Have once more OCO-related question.

            In this article we can see example of how to handle umnanaged protective orders:

            PHP Code:
            protected override void OnExecutionUpdate()
            {
                
            // Reset our stop order and target orders' Order objects after our position is closed.
                
            if ((stopOrder   != null && stopOrder   == execution.Order) || 
                    (
            targetOrder != null && targetOrder == execution.Order))
                {
                    if (
            execution.Order.OrderState == OrderState.Filled ||
                        
            execution.Order.OrderState == OrderState.PartFilled)
                    {
                        
            stopOrder null;
                        
            targetOrder null;
                    }
                }




            Keeping in mind my very first post in this thread want to ask: why sample script not updating OCO-pair on OrderState.PartFilled of protective orders ?
            Last edited by fx.practic; 05-29-2018, 02:12 PM.
            fx.practic
            NinjaTrader Ecosystem Vendor - fx.practic

            Comment


              #7
              Hello fx.practic,

              Thank you for your response.

              I may not fully understand your latest inquiry. Do you have a question on the sample you linked?

              Comment


                #8
                I mean, what if one protective OCO order (say, profit target) will be partial filled, and then price will move to counterpart OCO protective order (say, stop loss).

                If not to adjust stop loss price - after profit target order partial filled - stop loss will reverse position.
                And new position quantity will be (initial_lot_size - part_filled_lot_size).

                Am I right?
                Last edited by fx.practic; 05-30-2018, 03:17 AM.
                fx.practic
                NinjaTrader Ecosystem Vendor - fx.practic

                Comment


                  #9
                  Hello fx.practic,

                  Thank you for your response.

                  If you are using the Unmanaged Order Approach then you need to handle these types of items in your code.

                  The sample you refer to is specifically for the Managed Order Approach. This means the Exit order methods will adjust their quantity appropriately and automatically as they are in the Managed Order Approach.

                  Please let me know if you have any questions.

                  Comment


                    #10
                    Could You, please, to inspect this sample and to give Your opinion: is this right place and right safe and guaranteed way to update protective orders on partial fills?

                    (It looks working on historical market replay)
                    (I mean Umnanaged approach)


                    PHP Code:
                    OnExecutionUpdate()
                    {
                        if( 
                    execution.Order.Name == Take_Profit_Name )
                        {
                            
                            if( 
                    Take_Profit_Order != null     &&     Take_Profit_Order == execution.Order) )
                            {
                                if( 
                    execution.Order.OrderState == OrderState.PartFilled ) &&
                                    
                    Stop_Loss_Order != null && 
                                    !
                    Order.IsTerminalStateStop_Loss_Order.OrderState ) &&                                                                
                                    
                    execution.Order.Quantity != Stop_Loss_Order.Quantity     
                                        
                                        
                    ChangeOrderStop_Loss_Orderexecution.Order.QuantityStop_Loss_Order.LimitPriceStop_Loss_Order.StopPrice );
                                }
                            }
                        }

                    Last edited by fx.practic; 05-31-2018, 03:36 AM.
                    fx.practic
                    NinjaTrader Ecosystem Vendor - fx.practic

                    Comment


                      #11
                      Hello fx.practic,

                      Thank you for your response.

                      Your code is correct. You can test this on the Sim101 account and set the simulator to enforce partial fills by going to the NinjaTrader Control Center > Tools > Options > Trading > Simulator > Enforce partial fills > OK.

                      Please let me know if you have any questions.

                      Comment


                        #12
                        I know this is a couple years old, but I don't see how this could possibly be correct.
                        If the TakeProfit order had an original Quantity of 5, and receives a partial fill for 2, then there are 3 contracts remaining. So you need to adjust the stoploss order so that instead of 5 contracts it is now for 3 contracts. But in your code, you are checking this:

                        PHP Code:
                        execution.Order.Quantity != Stop_Loss_Order.Quantity 
                        How is this possibly correct? Your Stop_Loss_Order.Quantity is 5 (that's why you want to change it). Isn't execution.Order.Quantity also = 5? The filled property is 2 but the Quantity property is still 5. Shouldn't you be doing this instead:

                        PHP Code:
                        execution.Order.filled  != Stop_Loss_Order.Quantity 
                        and if so then:
                        PHP Code:
                        ChangeOrderStop_Loss_Orderexecution.Order.filledStop_Loss_Order.LimitPriceStop_Loss_Order.StopPrice ); 
                        Or am i misunderstanding something about Quantity in execution.Order?
                        Last edited by westofpluto; 09-28-2020, 12:50 PM.

                        Comment


                          #13
                          Hello westofpluto,

                          When I test changing a target/stop order with execution.Order.filled, I don't get an issue with partial fills.

                          I also would not suggest using ChangeOrder on the target/stop when they fill unless you do not have OCO functionality on your connection. If you have native OCO support, I would suggest to simply tie the target/stop together with OCO, and let native OCO handling reduce the order size and cancelling the order.

                          If I wanted to modify a target/stop order quantity when the other fills and I do not have OCO, I would do something like the following:

                          Code:
                          if ((stopLossLong != null && stopLossLong == execution.Order) || (targetLong != null && targetLong == execution.Order))
                          {
                              if (execution.Order.OrderState == OrderState.PartFilled)
                              {
                                  if (execution.Order == stopLossLong)
                                      ChangeOrder(targetLong, Position.Quantity, execution.Order.LimitPrice, execution.Order.StopPrice);
                                  if (execution.Order == targetLong)
                                      ChangeOrder(stopLossLong, Position.Quantity, execution.Order.LimitPrice, execution.Order.StopPrice);
                              }
                              if (execution.Order.OrderState == OrderState.Filled)
                              {
                                  CancelOrder(stopLossLong);
                                  CancelOrder(targetLong);
                                  stopLossLong = null;
                                  targetLong = null;
                              }
                          }
                          Note I am using the strategy Position object here, which is updated as soon as OnExecutionUpdate is processed.

                          This can be tested with the UnmanagedTemplate strategy shared in your other thread. Linked below for other's convenience.

                          https://ninjatrader.com/support/foru...29#post1120229

                          We look forward to assisting.
                          JimNinjaTrader Customer Service

                          Comment


                            #14
                            I thought I read that when you submit Unmanaged orders with Unmanaged SL and TP, then for partial fills you have to manage these yourself. Is that ONLY if we don't have native OCO functionality at our broker?

                            I am using NT8 and my broker is Ninjatrader. Do I have native OCO functionality? Which brokers do and do not?

                            Comment


                              #15
                              NinjaTrader Brokerage CQG based connections (NinjaTrader Continuum) support native OCO. Rithmic for NinjaTrader Brokerage uses locally simulated OCO, where the platform manages OCO.

                              Our Connection Guides will list any advisories regarding OCO support. For example, with TD Ameritrade, native OCO is supported, but we cannot modify an order that is OCO paired. TD Ameritrade requires we cancel and replace the orders.

                              Connection Guides can be found here - https://ninjatrader.com/Help-Connection-Guides

                              We can also note with the UnmanagedTemplate example, additional code is not used to reduce order quantity of the target and stop when they are filling. You could remove OCO strings from these submissions and test my snippet from post #13 instead of just nulling targetOrder and stopOrder in OnExecutionUpdate if you want to test your own order quantity reduction handling.

                              I.E. Replace the following with the code from post #13:

                              if ((stopLossLong != null && stopLossLong== execution.Order) || (targetLong != null && targetLong == execution.Order))
                              {
                              if (execution.Order.OrderState == OrderState.Filled
                              || execution.Order.OrderState == OrderState.PartFilled)
                              {
                              stopLossLong= null;
                              targetLong = null;
                              }
                              }

                              We look forward to assisting.
                              JimNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Radano, 06-10-2021, 01:40 AM
                              19 responses
                              606 views
                              0 likes
                              Last Post Radano
                              by Radano
                               
                              Started by KenneGaray, Today, 03:48 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post KenneGaray  
                              Started by thanajo, 05-04-2021, 02:11 AM
                              4 responses
                              470 views
                              0 likes
                              Last Post tradingnasdaqprueba  
                              Started by aa731, Today, 02:54 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post aa731
                              by aa731
                               
                              Started by Christopher_R, Today, 12:29 AM
                              0 responses
                              11 views
                              0 likes
                              Last Post Christopher_R  
                              Working...
                              X