Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Unmanaged - Close all positions
Collapse
X
-
Hello Tdschulz,
Thank you for your note.
There is not a method in NinjaScript that will close all positions. You will need to exit the orders with SubmitOrder() if you wish to exit the positions without disabling your strategy.
There are unsupported methods to do this however. As with the following link to a post in a thread on closing all positions: http://www.ninjatrader.com/support/f...16&postcount=6
The line of code at the link above should work for both Unmanaged and Managed approaches.
Please let me know if I may be of further assistance.
-
I actually didn't even realize there was more to the thread.
Thanks for that.
I read through it and I think I understand your suggestion. However, doesn't the SubmitOrder need to be attached to an IOrder? For example, I have:
Am I making this overly complicated? This is my first Unmanaged attempt so I'm sifting through threads but there doesn't seem to be a vast amount of info which I've found.Code:if (Position.MarketPosition == MarketPosition.Long) { if (targetOrderlong1 != null && targetOrderlong1 == order) if (order.OrderState == OrderState.Rejected && order.Filled == 0) { targetOrderlong1 = SubmitOrder(0, OrderAction.Sell, OrderType.Market, Position.Quantity, 0, 0, "LongOCOID1", "RejectedOrderExit"); }
Comment
-
I think that you may be making it more complicated than it is.Originally posted by Tdschulz View PostI actually didn't even realize there was more to the thread.
Thanks for that.
I read through it and I think I understand your suggestion. However, doesn't the SubmitOrder need to be attached to an IOrder? For example, I have:
Am I making this overly complicated? This is my first Unmanaged attempt so I'm sifting through threads but there doesn't seem to be a vast amount of info which I've found.Code:if (Position.MarketPosition == MarketPosition.Long) { if (targetOrderlong1 != null && targetOrderlong1 == order) if (order.OrderState == OrderState.Rejected && order.Filled == 0) { targetOrderlong1 = SubmitOrder(0, OrderAction.Sell, OrderType.Market, Position.Quantity, 0, 0, "LongOCOID1", "RejectedOrderExit"); }
I will repeat my earlier assertion that, given that your only real intent is to unconditionally exit your entire position, the only real question is: "Do I have a position on or not, and if so, how large is the position?" Once you answer that question, it is a matter of entering an order, with the correct quantity to exit the entire position. You can then cancel (if necessary) and/or nullify all IOrders, also unconditionally, using a null filter.
Comment
-
koganam,
How would you do this for multiple instruments. If I'm checking BarsInProgress == 1 and meet a condition to exit all positions and orders, I can't use Position.MarketPosition, as that'll only refer to the instrument related to BarsInProgress == 1.
Or how would I create a method that would do this for me?
So far I believe the solution is to use OnPositionUpdate and keep track of your positions and quantities through your own variables, but I'm still wondering if there's a cleaner way.
Code:protected override void OnBarUpdate(){ if (BarsInProgress == 1){ if (condition to exit all positions){ //Position.MarketPosition only refers to BarsInProgress == 1 CloseFlatten(); } } private void CloseFlatten(){ //how do I implement this? }
Comment
-
I am not sure that I see the issue here. If there are multiple instruments, each has a position, no? Are you saying that you want to exit for all BIP when only BIP == 1 signals an exit? If so, set a class bool variable to use as a test condition. Each BIP checks the selfsame bool, and exits accordingly.Originally posted by :::grimReaper::: View Postkoganam,
How would you do this for multiple instruments. If I'm checking BarsInProgress == 1 and meet a condition to exit all positions and orders, I can't use Position.MarketPosition, as that'll only refer to the instrument related to BarsInProgress == 1.
Or how would I create a method that would do this for me?
So far I believe the solution is to use OnPositionUpdate and keep track of your positions and quantities through your own variables, but I'm still wondering if there's a cleaner way.
Code:protected override void OnBarUpdate(){ if (BarsInProgress == 1){ if (condition to exit all positions){ //Position.MarketPosition only refers to BarsInProgress == 1 CloseFlatten(); } } private void CloseFlatten(){ //how do I implement this? }
For each BIP, your CloseFlatten() does exactly as suggested: checks Position,Quantity, and sends the appropriate exit order.
Otherwise, of course, your idea to keep a running tally of each instrument position using OnPositionUpdate() seems eminently viable.
Comment
-
Correct. A calculation is performed involving other instruments to determine whether all positions should be abandoned . Likewise, I do the same for the other BIPs.Originally posted by koganam View PostAre you saying that you want to exit for all BIP when only BIP == 1 signals an exit?
This is where I'm stuck. How do I ask Position (Position.MarketPosition) about a specific BIP?Originally posted by koganam View PostFor each BIP, your CloseFlatten() does exactly as suggested: checks Position,Quantity, and sends the appropriate exit order.
Comment
-
Have you tried keeping a running tally inside each BIP block?Originally posted by :::grimReaper::: View Post... This is where I'm stuck. How do I ask Position (Position.MarketPosition) about a specific BIP?
if (BIP == n)
{
qty_n = Position.Quantity;
//etc
}
Then you can put the quantities in a List, and use foreach to iterate through the list, exiting the relevant quantity that is indexed in the list.
Even simpler, you can just iterate through the Positions array, that holds all the positions. ref: http://www.ninjatrader.com/support/h.../positions.htm
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
656 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
371 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
109 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
574 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
579 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment