Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
How to close orders when user disables a running strategy
Collapse
X
-
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?Tags: None
-
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.
-
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
-
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
-
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:
This works wonderfully!Code:if (State == State.Terminated) { if (Position.MarketPosition != MarketPosition.Flat) { Position.Close("Exiting active position at market"); } }
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Today, 05:17 AM
|
0 responses
53 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
130 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
70 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
44 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
49 views
0 likes
|
Last Post
|

Comment