Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Managing the Partially filled order

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

    Managing the Partially filled order

    Hi, From my strategy, I want to place 100 order if the criteria met. Also I want to set Profit Target and StopLoss for filled order.
    If the order is partially filled (for example 20 contracts) and price moved beyond some price,
    1. I want to cancel the order for 80 quantity.
    2. I want to place PT and Stop Loss order for partially filled order until the price move beyond my price limit. This is where OnExecutionUpdate triggered multiple time with OrderState.PartFilled.

    Please provide some sample or code / api reference for above 2 requirement.

    #2
    Hello nandhumca,

    I'm not aware of a specific sample that contains all of that that but I can reference some more simple examples of using OnExecutionUpdate. You have the correct approach to use OnExecutionUpdate, you would need to monitor the incoming executions to check if it was part filled and the quantity that was filled.

    To cancel an order you can use CancelOrder: https://ninjatrader.com/support/help...thod_to_ca.htm

    2. I want to place PT and Stop Loss order for partially filled order until the price move beyond my price limit. This is where OnExecutionUpdate triggered multiple time with OrderState.PartFilled.
    You can use OnExecutionUpdate to submit profit target/ stop loss orders. How you do that would depend on when you wanted to submit them. If you wanted you could do that based on each partial fil however if you submit them on each partial fill and it continues to fill you would need to continue to update those targets to have them be the correct quantity. You could also monitor for the Cancelled event for the entry, after that was cancelled you could submit the target all at once for the partially filled amount.

    In the following link you can find a sample of submitting targets from OnExecutionUpdate and then using a price action (breakeven) to control it later: https://ninjatrader.com/support/help...and_onexec.htm

    Comment


      #3
      Thanks Jesse. I will explore the links.

      In case of any exception in submitting stop loss or profit target, I want to flatten the position. How to do that ? Similarly I want to handle all the negative scenarios of order. So that I will not ended up with unhandled order. Kindly share your input or share some samples.

      Comment


        #4
        I have got the below error msg from log when i tested my strategy with 100 contracts. I want to handle this error in code and close all open position and cancel all open orders. Below error message is printed in log multiple times with different order id and NT got closed automatically.

        2022-04-12 13:19:57:214|1|64|Instrument='ES 03-22' Account='Playback101' Average price=4777 Quantity=5 Market position=Short Operation=Update
        2022-04-12 13:20:03:298|1|16|NinjaScript strategy 'SolzzStrategy/236615534' submitting order
        2022-04-12 13:20:03:298|1|32|Order='d32dd7e18c06472c97f19c023 8cd9d90/Playback101' Name='Close position' New state='Rejected' Instrument='ES 03-22' Action='Buy to cover' Limit price=4785 Stop price=0 Quantity=5 Type='Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='Unable to submit order' Native error='Order 'd32dd7e18c06472c97f19c0238cd9d90' can't be submitted: Not enough excess margin.'
        2022-04-12 13:20:03:298|0|32|Playback101, Order 'd32dd7e18c06472c97f19c0238cd9d90' can't be submitted: Not enough excess margin. affected Order: BuyToCover 5 Limit @ 4785
        2022-04-12 13:20:03:301|1|16|NinjaScript strategy 'SolzzStrategy/236615534' submitting order




        My order is in below state (screenshot attached) and I couldn't cancel it manually also.

        Click image for larger version

Name:	image_72967.png
Views:	572
Size:	269.8 KB
ID:	1197481
        Attached Files
        Last edited by nandhumca; 04-12-2022, 11:55 AM.

        Comment


          #5
          I am able to catch below error on OnOrderUpdate method. But I couldn't Exit from short/long and cancel my StopLoss and Profit Target Order. am i missing anything ?

          if(error == ErrorCode.UnableToChangeOrder){
          ExitLong();
          ExitShort();
          CancelOrder(shortStopOrder);
          CancelOrder(shortTargetOrder);
          }
          }

          Comment


            #6
            Hello nandhumca,

            For a rejection you would generally need to manually manage the position as the strategy would get disabled. You can make a more advanced strategy that includes rejection handling, you do still need to be around to close the error text boxes when that happens. https://ninjatrader.com/support/help...tsub=rejection

            If you get an order stuck on the sim account you would need to reset the sim account to clear the orders. https://ninjatrader.com/support/help...TheAccountsTab

            Regarding the code you provided, you cannot call both exits at once. you would need to track what position you are in and call the appropriate method. When cancelling orders you should also wait for confirmation of the cancel before submitting exits, that will prevent an inflight execution where the exit is submitted but the target is filled as well before the cancel. Exit orders are also ignored depending on other factors, you can read about the order handling rules here: https://ninjatrader.com/support/help...antedPositions


            Comment


              #7
              Hi , My concern is more about trading on live. If my order stuck in live with cancel pending for long time or no way close it , what to do ?

              Comment


                #8
                Hello nandhumca,

                Stuck orders are not something which can occur in live trading, that is a item that can happen on the sim account based on various circumstances. The sim account in some cases will need reset to clear orders, with the playback connection you would disconnect/reconnect to do that.

                Comment


                  #9
                  Resetting Sim Accounts to clear Cancel pending certainly works ... but I have explored this in the Forum previously, and it is my belief that this is aberrant behaviour that should be "enhanced". It's a Sim Account, completely within control of the platform. If an Order is Cancelled, it should, of course, go through the Cancel pending state, but should be easily cancelled after that. This is both logical and sensible. Surely this can be fixed by the Development Team? One should not need to completely erase an Account's history simply to clear a condition that should not exist in the first place.

                  Thanks.
                  Multi-Dimensional Managed Trading
                  jeronymite
                  NinjaTrader Ecosystem Vendor - Mizpah Software

                  Comment

                  Latest Posts

                  Collapse

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