If I call ExitLong() when my exit conditions are met, will it close the preexisting positions, or do I have to bring in the number of shares and then call EnterShort() to offset my existing long position to get to a flat position?
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Strategy to close positions left hanging
Collapse
X
-
Strategy to close positions left hanging
I want to set up a strategy for the explicit purpose of closing out long positions that were left open after a previous strategy terminated before being flattened.
If I call ExitLong() when my exit conditions are met, will it close the preexisting positions, or do I have to bring in the number of shares and then call EnterShort() to offset my existing long position to get to a flat position? -
-
Bertrand:
Thanks for the help.
Are you saying that I could just submit an order, either EnterLong(NumberShares) or SubmitOrder() while Historical == true, and the Position object of the strategy would be updated without placing an order to my real account so that I can then issue an ExitLong() when my conditions are met after Historical == false and then the order will go to my real account?
Alternatively, is there a way to just set the Position object of the strategy to be a clone of the Position object of the account at startup? This would seem to be a very easy way to solve all the problems with syncronization. But maybe I am missing something.
Thanks
Originally posted by NinjaTrader_Bertrand View PostYes, you would need to do this programmatically, so the strategy has a position once you start it - this would be true for the managed mode I should add.
If you're comfortable working in the unmanaged mode you can simply issue the needed SubmitOrder() to offset the open position.
Comment
-
rmt81, setting the position object directly of the strategy would not be supported unfortunately, the only venue would be executing a historical entry for the strategy that would then bring the strategy position in sync to the account position at strategy startup and then issuing the Exit order that would be replicated by the strategy on the account bringing it flat then if the qty was entered correctly.
Comment
-
OK, so am I interpreting what you said correctly? Are you saying that I could just submit an order, either EnterLong(NumberShares) or SubmitOrder() while Historical == true, and the Position object of the strategy would be updated without placing an order to my real account so that I can then issue an ExitLong() when my conditions are met after Historical == false and then the order will go to my real account?
Originally posted by NinjaTrader_Bertrand View Postrmt81, setting the position object directly of the strategy would not be supported unfortunately, the only venue would be executing a historical entry for the strategy that would then bring the strategy position in sync to the account position at strategy startup and then issuing the Exit order that would be replicated by the strategy on the account bringing it flat then if the qty was entered correctly.
Comment
-
Bertrand:
I know this is unsupported, but the following code does appear to copy the account positions to the strategy positions:
protectedoverridevoid Initialize()
{
Add(SMA(Fast));
Add(SMA(Slow));
// Add the instrument list to the strategy
InstrumentList InstrList = InstrumentList.GetObject("Positions to Close"); // “Positions to Close” is the name of an Instrument List
foreach (Instrument i in InstrList.Instruments)
{
if (i.FullName != Instrument.FullName)
Add(i.FullName, BarsPeriod.Id, BarsPeriod.Value);
}
}
protectedoverridevoid OnStartUp()
{
// Copy any outstanding Positions from the Account to the Strategy
for (int i = 0; i < Positions.GetLength(0); i++)
{
Position StratPos = Positions[i];
Position AcctPos = Account.Positions.FindByInstrument(StratPos.Instru ment);
if (AcctPos != null)
Positions.SetValue(AcctPos, i);
}
}
Are there any particular issues or problems that I should look out for when using this approach to synchronize to strategy to match the account? That is the essence of what I was trying to accomplish in my original question anyway.
I suppose that if there was a possibility of orders left outstanding, I would need to do something very similar for the Orders object as I did above for the Positions object.
Are there other objects that would need to be brought into sync in order to get the Strategy as a whole into sync with the Account? For example, would the statistics in the Performance object be incorrect as a result of doing this? What else am I forgetting?
Thanks for your help with this.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
633 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
364 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
105 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
567 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
568 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment