Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Deleting Unexecuted Stop Orders

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

    Deleting Unexecuted Stop Orders

    I wrote a script that opens a buy stop and sell stop order but I can't find the command to remove/delete/cancel a stop order? I am using the unmanaged approach for my strategy.

    #2
    to cancel active orders
    CancelOrder(orderStop);

    or do you mean stuck CancelPending orders that wont dissapear from the sim account?
    MicroTrends
    NinjaTrader Ecosystem Vendor - micro-trends.co.uk

    Comment


      #3
      Welcome to the forums mr1997!

      As MicroTrends mentions, you can cancel active orders with CancelOrder.

      CancelOrder - https://ninjatrader.com/support/help...ancelorder.htm

      If you are referring to stuck simulation orders, these would need to be cleared out by resetting the sim account. (Control Center > Accounts tab > Sim101 > Right Click > Edit Account> Reset) Stuck simulation orders can come about when the simulation engine gets a high number of order submissions/changes/fills/cancels all at one. If this is a common problem for your strategy, I suggest looking into how often it is changing orders/submitting new orders/attempting cancels. If these operations are in high succession you could get a stuck simulation order.

      We look forward to assisting.

      Comment


        #4
        MicroTrends I was looking for the actual command to delete stop orders that have not been executed. I wasn't aware of the "stuck pending orders" issue with the sim account. They've made programming for this platform absolutely maddening.

        So is there not a script command to flatten all orders???

        Comment


          #5
          O.K. I added the flatten command to my script.

          It looks like the flatten command is executing but...

          My script successfully created a buy stop and sell stop but when the flatten command was fired off in the script it removed the unfilled sell stop but left my market position active.

          All orders were closed when I fired the flatten command manually???
          Last edited by mr1997; 12-09-2020, 02:50 PM.

          Comment


            #6
            Originally posted by mr1997 View Post
            O.K. I added the flatten command to my script.
            It looks like the flatten command is executing but...
            My script successfully created a buy stop and sell stop but when the flatten command was fired off in the script it removed the unfilled sell stop but left my market position active.
            All orders were closed when I fired the flatten command manually???
            Your flatten command must cancel orders and close the position


            If you are using a default mode strategy you can use ExitLong or ExitShort





            if (Position.MarketPosition==MarketPosition.Long)
            ExitLong();
            else if (Position.MarketPosition==MarketPosition.Short)
            ExitShort();
            MicroTrends
            NinjaTrader Ecosystem Vendor - micro-trends.co.uk

            Comment


              #7
              "Your flatten command must cancel orders and close the position"

              Sorry Micro...I don't follow... Can you elaborate?

              I am using the unmanaged strategy since opening a buy stop and sell stop simultaneously does not work in managed mode.

              Comment


                #8
                Hello mr1997,

                Are you using Account.Flatten or similar? Please note that this is an Account level AddOn Framework method that would take actions outside of the context of the NinjaScript strategy. It will be similar to using the Close button. I have attached a demonstration video showing this in action as well as the test script so you may do your own tests on your end.

                Demo - https://drive.google.com/file/d/1alF...w?usp=drivesdk

                To get you on track to a solution, if you are using Unmanaged Approach and want to cancel active orders that the strategy has submitted, I suggest tracking the orders as Order objects so you can cancel them with CancelOrder.

                To close a position, you would use SubmitOrderUnmanaged to submit a market order that matches the strategy's Position.Quantity.

                The above could be combined in a custom method "CancelAndClose" that cancels all of your orders and sets a bool "WaitingForCancels" to true. Since we set Order objects to null in OnOrderUpdate when they are cancelled, you could check for "WaitingForCancels" to be true in addition to checking if your Order objects are null at the end of OnOrderUpdate.

                The result is one method that is called to cancel orders, and then when the cancels come back and the order objects are nulled, the position gets closed. The strategy stays running.

                Unmanaged Approach means you are taking control of all aspects of the strategy's order handling, so this would be more complex than the Managed Approach where you can just call ExitLong and ExitShort to cancel orders and close positions.

                Please let us know if you have any additional questions.
                Attached Files

                Comment


                  #9
                  Jim

                  Thanks for the thorough answer. Not exactly a c programmer but I get the gist of what you're telling me. I'll have a look at your example script when I get home this evening.

                  Thought I had seen somewhere where you can set an expiration time for a stop or limit order.... Might be an easier way to accomplish my goal at this point.

                  Comment


                    #10
                    Hello mr1997,

                    Order expiration time would be set with TimeInForce.

                    TimeInForce is set when configuring the strategy. We can use a time In force of DAY or GTC (Good Till Cancelled) GTD (Good Till Day) is not supported for NinjaScript strategies.

                    If you want more control than what TIF offers, it would be best to implement the canceling behaviors you wish in your strategy logic.

                    Please let us know if there is anything else we can do to help.

                    Comment


                      #11
                      NinjaTrader_Jim How do I edit/update your script so I can back test it? Also are profit & stop loss targets in ticks?

                      I enabled your script and watched it in action but was confused when I viewed the executions tab. It's placing order quantities of 67, 33, 63, 37???

                      Notes:
                      1) I am using simulated data feed
                      2) I am trading micro Nasdaq contact

                      Click image for larger version  Name:	Screenshot (24).png Views:	0 Size:	59.3 KB ID:	1131889
                      Attached Files
                      Last edited by mr1997; 12-11-2020, 09:16 AM.

                      Comment


                        #12
                        Hello mr1997,

                        I have recorded a demonstration where the script is modified to use Stop Market orders for the entry. Please note the same changes when testing on your end.



                        We look forward to assisting.

                        Comment


                          #13
                          No results when I run a backtest. I assume removing the following code would fix that?

                          if (State == State.Historical)
                          return;

                          Comment


                            #14
                            Hello mr1997,

                            Yes, you will have to remove the if(State == State.Historical) return; lines to make the strategy backtestable.

                            Please also note that the strategy uses bools to prevent multiple entries and these bools get reset as orders execute. You will need to ensure that BarsRequiredToTrade is set to 0 in the Strategy Analyzer, or you can incorporate a BarsRequiredToTrade check in the strategy logic to prevent the logic from flipping these bools when order submissions are attempted.

                            I.E.

                            Code:
                            if (longEntry == null && shortEntry == null && !entrySubmitted[B] && CurrentBar >BarsRequiredToTrade[/B]
                            && Position.MarketPosition == MarketPosition.Flat)
                            We look forward to assisting.

                            Comment


                              #15
                              NinjaTrader_Jim I made the changes you advised above but when I right click the chart and select Strategy/Performance/Historical it pops up quickly with the results but no trades were executed.

                              ...the following error appears in the output:

                              Strategy 'UnmgdTemplateMOD NT8': Error on calling 'OnBarUpdate' method on bar 0: Object reference not set to an instance of an object.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Today, 05:17 AM
                              0 responses
                              24 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