Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Incorrect quantity / partially filled problem

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

    Incorrect quantity / partially filled problem

    I was backtesting my one of my strategies using market replay and I noticed a specific trade that didn't do what it should have done. I have Default Quantity throughout my code and the quantity is set to 4 in the strategy options. Each trade should be for 4 contracts and each SL/TP should be the same quantity. I use the same signal name.

    In the screenshot you can see an order for 4 contracts with a TP and SL. However, instead of being 4 as it should be. The TP and SL are for only a quantity of just 1. The order trace also shows a quantity of 4 submitted. I went through the trade again and again and each time it does the same thing in market replay. Every other trade goes through fine.

    TRIGGER Long - Limit Order Submitted 11/06/2024 17:21:34
    11/06/2024 17:21:35 Strategy 'MS/3277': Entered internal SubmitOrderManaged() method at 11/06/2024 17:21:35: BarsInProgress=0 Action=Buy OrderType=Limit Quantity=4 LimitPrice=5359.00 StopPrice=0 SignalName='myLong' FromEntrySignal=''

    MyStop = 5357.5 & MyProfit = 5360
    11/06/2024 17:21:35 Strategy 'MS/3277': Entered internal SubmitOrderManaged() method at 11/06/2024 17:21:35: BarsInProgress=0 Action=Sell OrderType=StopMarket Quantity=4 LimitPrice=0 StopPrice=5357.50 SignalName='' FromEntrySignal='myLong'
    11/06/2024 17:21:35 Strategy 'MS/3277': Entered internal SubmitOrderManaged() method at 11/06/2024 17:21:35: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=4 LimitPrice=5360.00 StopPrice=0 SignalName='' FromEntrySignal='myLong'

    So when the SL/TP is hit, it leaves my original on running with a position of 3.

    In the log in control panel I have lots of entries at the same exact time. After the original order was Submitted > Accepted > Working....the next entry shows 'Partially filled'. Then after this the sell orders go through show here a quantity of 1. Then after this the original order finishes executing leaving me a position of 4L.

    In my code SL/TP orders are submitted with the same signal name as the original order. I would have thought they would update to reflect the size of the position?

    The exit orders are submitted after the original order and also after the SL/TP values have been stored in a variable. These orders would be resubmitted every bar to stop it expiring.

    // Set 5
    if ((TriggerState == 2)
    && (Position.MarketPosition == MarketPosition.Long)
    && (MyBar != CurrentBars[0]))
    {
    ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), MyStop, "", @"myLong");
    ExitLongLimit(Convert.ToInt32(DefaultQuantity), MyProfit, "", @"myLong");
    MyBar = Convert.ToInt32(CurrentBars[0]);
    }​

    What do I have to do to have this not happen again? and make sure the exit orders always match the position ? I'm using strategy builder but have just started to unlock code.
    Attached Files
    Last edited by lucyb; 07-01-2024, 05:43 PM.

    #2
    Hello lucyb,

    Thank you for your post.

    "After the original order was Submitted > Accepted > Working....the next entry shows 'Partially filled'."

    To confirm, the entry order is showing as partially filled at some point?

    Unfortunately, there is no way to prevent partial fills. Being partially filled is a function of what is happening in the market.

    You can track partial fills in OnOrderUpdate() or OnExecutionUpdate().

    OnOrderUpdate() - https://ninjatrader.com/support/help...rderupdate.htm
    OnExecutionUpdate() - https://ninjatrader.com/support/help...tionupdate.htm

    SampleOnOrderUpdate takes partial fills into account and adjusts the exit order quantities based on how many orders are filled.

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

    Comment


      #3
      Originally posted by NinjaTrader_Gaby View Post
      Hello lucyb,

      To confirm, the entry order is showing as partially filled at some point?
      Yes, and this is in MarketReplay. I would have thought this wouldn't apply to market replay?

      Comment


        #4
        Hello lucyb,

        This would still apply in Market Replay. With Market Replay data, you replay the market movements the instrument experienced as accurately as possible on the platform (like they were happening in real-time). The data is recorded live on our servers.

        Comment


          #5
          Hey. So what I noticed is. When the ExitLong is resubmitted at the start of the next bar. The quantities update to the correct ones of the original order.

          However, if the SL or TP is hit before the next bar starts, then it will fill at the lesser incorrect value(ie, the trade is over within 1 bar). I can fix this by using onPriceChange and having the Exit orders resubmit every single price change. Then immediately the SL/TP go from 1 to 4 on the next price change. But is there a better way? resubmitting the same orders on price change seems excessive.

          I've not advanced enough yet to use those advanced methods you mention above.

          Last edited by lucyb; 07-03-2024, 02:42 AM.

          Comment


            #6
            Hello lucyb,

            Thank you for your post.

            Yes, this would be the expected behavior. For example, if your entry filled 1 of 4 you will initially have targets 1 of 4, then when the entry fills you'll have to wait until the next update of OnBarUpdate() in order for the strategy to also submit the associated stops and targets for the rest of the partially filled order.

            Changing your Calculate setting to one that updates more frequently (OnPriceChange) would be the way if you don't want to wait until the next bar closes (OnBarClose) for the strategy to be able to update the quantities.

            Comment


              #7
              Thanks, and without using these advanced techniques there's no way to know how much of the quality has been filled? and also the quantity in the Exit orders?

              And it's not advisable to wait until the Position.Quantity == Default.Quantity before submitting the exit orders?
              Last edited by lucyb; 07-03-2024, 09:47 AM.

              Comment


                #8
                Hello,

                No, not with just the Strategy Builder. Tracking partial fills / quantity filled is not possible in the Builder since it requires monitoring from OnOrderUpdate and OnExecutionUpdate().

                I'm not sure what you mean by Position.Quantity = Default.Quantity? The exit orders are tied to the entry using signal names, so the quantity will match the filled amount for the associated position. We can't anticipate or predict if or when a partial fill is going to happen. You can submit the entry order with quantity 4 however if a partial fill occurs, there is no logic you can add to prevent that from happening.

                Please let me know if you have any other questions.

                Comment


                  #9
                  I'm just talking about the delay between the original order filling in full and the exit orders only showing the first fill position.

                  Comment


                    #10
                    Hello lucyb,

                    As mentioned, we can't predict when a partial fill will occur. If you want less of a "delay" for the strategy to update the exit orders position after a partial fill has occurred, you can change your calculate setting as discussed.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by NullPointStrategies, Today, 05:17 AM
                    0 responses
                    23 views
                    0 likes
                    Last Post NullPointStrategies  
                    Started by argusthome, 03-08-2026, 10:06 AM
                    0 responses
                    120 views
                    0 likes
                    Last Post argusthome  
                    Started by NabilKhattabi, 03-06-2026, 11:18 AM
                    0 responses
                    63 views
                    0 likes
                    Last Post NabilKhattabi  
                    Started by Deep42, 03-06-2026, 12:28 AM
                    0 responses
                    41 views
                    0 likes
                    Last Post Deep42
                    by Deep42
                     
                    Started by TheRealMorford, 03-05-2026, 06:15 PM
                    0 responses
                    45 views
                    0 likes
                    Last Post TheRealMorford  
                    Working...
                    X