Strategy opens a position with take profit and stoploss orders. Programmatically I want to close open position before the market closes. So in the bar update I check the time, if we are 5 minutes from close I check for open positions. If I have an open position I have tried to close it via position.close, account.flattenall. The only option I can think of is to cancel all open orders and then create a closing order at market. That seems like more work than it should be I would think position.close should work so I guess I am just wondering why that disables the strategy.
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Closing position disables strategy
Collapse
X
-
Closing position disables strategy
In my case right now, this is only relevant to testing with playback. If this were live I think the behavior is fine for now. Here is my scenario:
Strategy opens a position with take profit and stoploss orders. Programmatically I want to close open position before the market closes. So in the bar update I check the time, if we are 5 minutes from close I check for open positions. If I have an open position I have tried to close it via position.close, account.flattenall. The only option I can think of is to cancel all open orders and then create a closing order at market. That seems like more work than it should be I would think position.close should work so I guess I am just wondering why that disables the strategy.Last edited by TimWhitbeck; 03-02-2025, 01:08 PM.Tags: None
-
I can't seem to delete my own post so I guess I will post the very simple solution.- Loop through the account positions collection.
- Check MarketPosition property and use ExitLong() or ExitShort() to close.
- Make sure to either exit the loop
- If you have more than 1 position you will need to handle it a bit different because the Exit will modify the positions collection and you will error if you let it try to continue the loop.
-
Hello TimWhitbeck,
This is correct. Clicking the Close button, using Account.Flatten (from code or manually on the positions tab of the Control Center), or using Position.Close (from code or manually on the positions tab) will disable any running strategies with an open position.
This is to prevent the account position from becoming out of sync with the strategy position as these actions will not affect the strategy position.
To close positions from a strategy call ExitLong() or ExitShort() and cancel any working orders with CancelOrder() (or allowing these to cancel if using isLiveUntilCancelled false).
To close positions manually without disabling a strategy, place a manual market order in the opposite direction of the position. If you are long, place a sell market order. If you are short place a buy market order. (Note, this would make any strategy position on that instrument and account out of sync with the account position.Chelsea B.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
110 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
59 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
37 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
41 views
0 likes
|
Last Post
|
||
|
Started by Mindset, 02-28-2026, 06:16 AM
|
0 responses
78 views
0 likes
|
Last Post
by Mindset
02-28-2026, 06:16 AM
|

Comment