Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to close orders when user disables a running strategy

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

    How to close orders when user disables a running strategy

    I can't seem to find how to recognize in my script when a user (me) clicks the "enable" toggle button in the "Strategies" tab in the "Control Center". In particular, I want to see when the user disables the strategy. Is there a way to capture that and close all orders before exiting?

    #2
    Hello dweems,

    The easiest way is to enable the option to cancel open orders when disabled. That is in the options menu, you can read about those settings in the following page: https://ninjatrader.com/support/help...riptProperties

    For closing a position I would suggest to enable those options and then use one of the platforms tools like the chart trader or superdom and click Close. That will close the position which disables the strategy, the associated orders will also be cancelled.

    Comment


      #3
      Unfortunately the cancel open orders option doesn't seem to work. I should note that the wording is different than what you said. It isn't "Cancel open orders when disabled" but instead "Cancel entry orders when a strategy is disabled". It probably does that but doesn't close open orders. The other option "Cancel exit orders when a strategy is disabled" doesn't do it either. I think those cancellations are to cancel orders that haven't been filled yet but are pending. Yes, I can and do close the open orders by clicking on the "Positions" tab, right click on the listed position(s) and choose "Close Position". I just would prefer to do that programmatically if possible.

      Comment


        #4
        Hello dweems,

        There are two options, cancel entry and cancel exit orders, you would check both to close all orders that the strategy manages. The strategy needs to have submitted the orders and they need to be active and unfilled for those options to work.

        Comment


          #5
          I guess I need to reword my request. What I really want is to automatically close open "positions" upon exiting the strategy. Obviously my using the word "orders" was a bit misleading. My apologies.

          Comment


            #6
            Hello dweems,

            Positions are left open when disabling a strategy, that is expected because strategies can be programmed to later resume positions. To close the position and also disable the strategy you can click a Close button for that instrument in any tool or use the positions tab. That ensures the strategy is disabled and the position is closed. A strategy cannot trade during its terminated state so that would be the best option to make sure the position gets closed if you wanted it closed while also disabling the strategy.

            Comment


              #7
              To answer my question, there is a way to trap within your Ninjascript code when a user selects to disable a running strategy. You can see when the strategy is terminated within OnStateChange by checking State == State.Terminated. For example, I used:

              Code:
              if (State == State.Terminated)
              {
                  if (Position.MarketPosition != MarketPosition.Flat)
                  {
                      Position.Close("Exiting active position at market");
                  }
              }​
              This works wonderfully!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Yesterday, 05:17 AM
              0 responses
              54 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              130 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              71 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              44 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              49 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X