Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Issue with entry and exit orders that were filled in partials.

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

    Issue with entry and exit orders that were filled in partials.

    I am using the SampleOnOrderUpdate example code provided by NT. However, I am having an issue where if the entry order was filled in partials, then the exit order (i.e. profit or stop) quality does not equal the full quantity. The exit order quantities are equal to the quantity of the last partial fill of the entry order. Is there an issue with the example code? How can I ensure that if my entry orders are filled in partials, then the exit orders have proper quantity? Also, with this example code, sometimes I notice that if the exit order is executed (e.g. profit target), sometimes the other exit order (e.g. stop) does not fully cancel automatically.


    #2
    Hello wzgy0920,

    Thank you for your post.

    The SampleOnOrderUpdate strategy takes partial fills into account and adjusts the exit order quantities based on how many orders are filled. With that said, the script accesses the execution.Order.Filled to get the quantity and if you are using a broker such as Rithmic or Interactive Brokers, the Execution, Order, and Position update events may not come through in an expected sequence. We have some IB and Rithmic friendly examples that have been modified to keep track of the sum filled and use that quantity in the stop and target submissions. These examples do not rely on Order/Execution/Position events being sent in a specific sequence and instead focus on isolating logic to just use Execution information or Order information separately. Please see the examples found at the following link:
    Hello, I see in the documentation you have fixed: 15048 Added Adapter, Rithmic Updated Rithmic API to version 11.3.0.0 Can you elaborate on that please? Will OnOrderUpdate() OnExecutionUpdate() and OnPositionUpdate() be called in the expected order?


    Please let us know if we may be of further assistance.

    Comment


      #3
      Originally posted by NinjaTrader_Emily View Post
      Hello wzgy0920,

      Thank you for your post.

      The SampleOnOrderUpdate strategy takes partial fills into account and adjusts the exit order quantities based on how many orders are filled. With that said, the script accesses the execution.Order.Filled to get the quantity and if you are using a broker such as Rithmic or Interactive Brokers, the Execution, Order, and Position update events may not come through in an expected sequence. We have some IB and Rithmic friendly examples that have been modified to keep track of the sum filled and use that quantity in the stop and target submissions. These examples do not rely on Order/Execution/Position events being sent in a specific sequence and instead focus on isolating logic to just use Execution information or Order information separately. Please see the examples found at the following link:
      Hello, I see in the documentation you have fixed: 15048 Added Adapter, Rithmic Updated Rithmic API to version 11.3.0.0 Can you elaborate on that please? Will OnOrderUpdate() OnExecutionUpdate() and OnPositionUpdate() be called in the expected order?


      Please let us know if we may be of further assistance.
      Thank you for letting me know this is a Rithmic issue. Yes, I had this issue on live trading as opposed to Sim. How can I do development troubleshoot/test the issue on Rithmic without risking live account?

      Comment


        #4
        Hello wzgy0920,

        Thank you for your reply.

        I suggest reviewing the "Notes" section on the following help guide pages, as there is a note specifically for Rithmic and Interactive Brokers users:I have summarized the main reasons behind this in the following post from a similar forum thread:
        Hello, I'm curious to know what is the best approach to code to when using Interactive Brokers. While not 100% the same scenario, in this post: https://forum.ninjatrader.com/forum/...18#post1114218 (https://forum.ninjatrader.com/forum/ninjatrader-8/strategy-development/1058812-exit-methods-instead-of-set-methods-for-stop-loss


        Please feel free to reach out with any additional questions or concerns.

        Comment


          #5
          Emily,

          Thank you for the info. I followed the suggested example code. However now I have a new issue. If the either one of the Exit Profit/Stop get filled in partials, the other order doesn't fully cancel. For example, if I my ExitLongLimit order gets filled in parts of 2-2-1 (total 5), then my ExitShortLimit order still gets stuck at 1x quantity and does not cancel. This is hard to troubleshoot or replicate because I have to place orders on live account.

          Comment


            #6
            Originally posted by wzgy0920 View Post
            Emily,

            Thank you for the info. I followed the suggested example code. However now I have a new issue. If the either one of the Exit Profit/Stop get filled in partials, the other order doesn't fully cancel. For example, if I my ExitLongLimit order gets filled in parts of 2-2-1 (total 5), then my ExitShortLimit order still gets stuck at 1x quantity and does not cancel. This is hard to troubleshoot or replicate because I have to place orders on live account.
            I would like to review your diagnostic (log and trace) files to see the sequence of events during these partial fills and the corresponding behavior from your exit orders. You can send these files by going to the Control Center-> Help-> Email Support. Ensuring 'Log and Trace Files' is checked will include these files. This is checked by default. Please include "Attn Emily C" in the subject line along with a link to this forum thread in the body of the email and please also answer all of the following in your email:
            • What Order IDs wereassigned to these orders when this occurred? You can check in the Executions or Orders tab of the Control Center, or you can check in the Control Center > New > Trade Performance reports from the day/time this occurred.
            • Do you receive an error on the screen? Are there errors on the Log tab of the Control Center? If so, what do these errors report?
            ​Thanks in advance; I look forward to assisting you further.

            Comment


              #7
              Emily,

              I have have to create a simplified strategy to demonstrate the issue. I'll email you later. Meanwhile, I have been troubleshooting. Below is one example scenario. I enter MNQ 5x @ $15270.00 in partials of 3-1-1. The Exit orders (i.e. Profit/Loss) are correctly submitted in quantity of 5x. Then profit target gets hit (i.e. ExitLongLimit) and it gets filled in partials of 1-1-1-1-1. Then I am flat but there is 1x ExitLongStopMarket order still pending that does NOT get canceled. I have trace orders enabled, so I would see prints like below. But I do not see this being printed for the last final stop order that needs to be canceled.

              Code:
              10/10/2023 2:57:03 PM Strategy 'MyStrategyName/308032564: Amended pending exit order, since associated position is closed, orderId='1688955000' account='MyAccountName' name='MyStopLoss' orderState=ChangeSubmitted instrument='MNQ 12-23' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=15231.5 quantity=4 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=16602 time='2023-10-10 14:57:03' gtd='2099-12-01' statementDate='2023-10-10'


              Could the issue be related to this segment of the example code inside OnOrderUpdate()? In my case, the targetLong1/stopLossLong1 are becoming nulled because the profit target was filled. Since the stopLossLong1 is becoming nulled immediately after all profit targets are filled, maybe NT is unable to access the stopLossLong1 to fully cancel it????

              Code:
              // Null Target and Stop orders if filled or cancelled. We do not use the Order objects after the order is filled, so we can null them here
              if ((targetLong1 != null && targetLong1 == order)
              || (stopLossLong1 != null && stopLossLong1 == order))
              {
              if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
              targetLong1 = stopLossLong1 = null;
              if (order.OrderState == OrderState.Filled)
              targetLong1 = stopLossLong1 = null;
              }
              Last edited by wzgy0920; 10-10-2023, 01:12 PM.

              Comment


                #8
                Originally posted by wzgy0920 View Post


                Could the issue be related to this segment of the example code inside OnOrderUpdate()? In my case, the targetLong1/stopLossLong1 are becoming nulled because the profit target was filled. Since the stopLossLong1 is becoming nulled immediately after all profit targets are filled, maybe NT is unable to access the stopLossLong1 to fully cancel it????

                Code:
                // Null Target and Stop orders if filled or cancelled. We do not use the Order objects after the order is filled, so we can null them here
                if ((targetLong1 != null && targetLong1 == order)
                || (stopLossLong1 != null && stopLossLong1 == order))
                {
                if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                targetLong1 = stopLossLong1 = null;
                if (order.OrderState == OrderState.Filled)
                targetLong1 = stopLossLong1 = null;
                }

                Just another update. Modified the above segment of the code to the one below. Initial results indicate the issue does not happen. But I need to do additional test. However, I am unsure if this is the right approach since I have to worry about Canceling all the Exit orders myself instead of it being managed automatically.

                Code:
                if ( (myStopOrder != null && myStopOrder == order) || (myTargetOrder != null && myTargetOrder == order) )
                {
                if ((order.OrderState == OrderState.Cancelled && order.Filled == 0) || (order.OrderState == OrderState.Filled))
                {
                
                CancelOrder(myTargetOrder);
                CancelOrder(myStopOrder);
                myStopOrder = null;
                myTargetOrder = null;
                }
                
                }

                Comment


                  #9
                  Hello wzgy0920,

                  Thank you for your reply.

                  Are you using the fromEntrySignal field in your stop and target orders? I would suspect that this would cancel the order once the position becomes flat, as the position opened from the associated entry signal would then be closed and the remaining exit order would no longer be valid. More info about FromEntrySignal is mentioned on the following page in the "How to close a position" section:


                  In your testing/simplified example I am also curious about the results of the TraceOrders output when it is enabled in State.SetDefaults:


                  When enabled, TraceOrders provides information to the NinjaScript Output window about when orders are submitted, changed, canceled, ignored, etc.

                  I look forward to assisting you further.

                  Comment


                    #10
                    Emily,

                    I was able to reproduce the issue with a simplified strategy example. I have sent it via email. I have also send the log/trace files with references to oderID and signal names that need investigation. This issue occurs both on 8.1.1.7 and on 8.0.28.0.

                    Comment


                      #11
                      Originally posted by wzgy0920 View Post


                      Just another update. Modified the above segment of the code to the one below. Initial results indicate the issue does not happen. But I need to do additional test. However, I am unsure if this is the right approach since I have to worry about Canceling all the Exit orders myself instead of it being managed automatically.

                      Code:
                      if ( (myStopOrder != null && myStopOrder == order) || (myTargetOrder != null && myTargetOrder == order) )
                      {
                      if ((order.OrderState == OrderState.Cancelled && order.Filled == 0) || (order.OrderState == OrderState.Filled))
                      {
                      
                      CancelOrder(myTargetOrder);
                      CancelOrder(myStopOrder);
                      myStopOrder = null;
                      myTargetOrder = null;
                      }
                      
                      }
                      Emily, the above temp work around does NOT work. I left the strategy on overnight and notice I had an open position without any Exit orders.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Today, 05:17 AM
                      0 responses
                      50 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      126 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      69 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      42 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      46 views
                      0 likes
                      Last Post TheRealMorford  
                      Working...
                      X