Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Proper Use of OrderEntry
Collapse
X
-
Thanks.
In the attached code you iterate through each of the account positions and assign the newly created order to myFlattenOrder. This will result in only one order when you submit. The structure of the code doesn't handle if multiple positions are open. It creates the orders but because myFlattenOrder only points to the last order created its the only order that will be submitted.
If the API for Flatten had a parameter to specify the OrderEntry parameter it would look like this.Code:Order myFlattenOrder = null; foreach (Position pos in account.Positions) { if (pos.Instrument == Instrument) { // Only the last order created here will end up being submitted. if (pos.MarketPosition == MarketPosition.Long) myFlattenOrder = account.CreateOrder(Instrument, OrderAction.Sell, OrderType.Market, OrderEntry.Automated, TimeInForce.Gtc, pos.Quantity, 0, 0, "", "FlattenPosition "+DateTime.Now.ToString(), DateTime.Now, null); if (pos.MarketPosition == MarketPosition.Short) myFlattenOrder = account.CreateOrder(Instrument, OrderAction.BuyToCover, OrderType.Market, OrderEntry.Automated, TimeInForce.Gtc, pos.Quantity, 0, 0, "", "FlattenPosition "+DateTime.Now.ToString(), DateTime.Now, null); } } if (myFlattenOrder != null) account.Submit(new[] { myFlattenOrder });
I am not sure what use case scenario, other then perhaps creating a button clicked by the user called Flatten, in which the Account.Flatten API is useable by scripts.Code:namespace NinjaTrader.Cbi { public static class AccountExtensions { public static void Flatten(this Account account, ICollection<Instrument> instruments, OrderEntry orderEntry) { var orders = new List<Order>(); foreach(var position in account.Positions) { if(instruments.Contains(position.Instrument)) { if(position.MarketPosition == MarketPosition.Long) { orders.Add(CreateFlattenOrder(account, position, OrderAction.Sell)); } if(position.MarketPosition == MarketPosition.Short) { orders.Add(CreateFlattenOrder(account, position, OrderAction.BuyToCover)); } } } if(orders.Count > 0) { account.Submit(orders); } } private static Order CreateFlattenOrder(Account account, Position position, OrderAction action) { return account.CreateOrder( position.Instrument, action, OrderType.Market, OrderEntry.Automated, TimeInForce.Gtc, position.Quantity, 0, 0, string.Empty, "FlattenPosition " + DateTime.Now.ToString(), DateTime.Now, null); } } }
Comment
-
Hello ntbone,
The example describes how you can flatten a position with an order created with Account.CreateOrder so you can set the OrderEntry property and achieve your goal to mark the order as an automated order. I am simply showing how you can flatten a position using Account.CreateOrder and Account.Submit since Account.Flatten does not have an overload that has an EntryOrder parameter.
If you want to mark an order as OrderEntry.Automated, this would be the way.
Instrument is relative to the indicator that is running the code and it is not intended to be a drop in for your AddOn. If you want to close positions on other instruments that are handled in your AddOn, you would need to create separate checks to see if pos.Instrument matches any of the instruments managed in your AddOn.
As a rough example:
Please let us know if you have any additional questions.Code:foreach (Position pos in account.Positions) { if (pos.Instrument == InstrumentA) { Order myFlattenOrderA = null; if (pos.MarketPosition == MarketPosition.Long) myFlattenOrderA = account.CreateOrder(Instrument, OrderAction.Sell, OrderType.Market, OrderEntry.Automated, TimeInForce.Gtc, pos.Quantity, 0, 0, "", "FlattenPosition "+DateTime.Now.ToString(), DateTime.Now, null); if (pos.MarketPosition == MarketPosition.Short) myFlattenOrderA = account.CreateOrder(Instrument, OrderAction.BuyToCover, OrderType.Market, OrderEntry.Automated, TimeInForce.Gtc, pos.Quantity, 0, 0, "", "FlattenPosition "+DateTime.Now.ToString(), DateTime.Now, null); account.Submit(new[] { myFlattenOrderA }); } if (pos.Instrument == InstrumentB) { Order myFlattenOrderB = null; if (pos.MarketPosition == MarketPosition.Long) myFlattenOrderB = account.CreateOrder(Instrument, OrderAction.Sell, OrderType.Market, OrderEntry.Automated, TimeInForce.Gtc, pos.Quantity, 0, 0, "", "FlattenPosition "+DateTime.Now.ToString(), DateTime.Now, null); if (pos.MarketPosition == MarketPosition.Short) myFlattenOrderB = account.CreateOrder(Instrument, OrderAction.BuyToCover, OrderType.Market, OrderEntry.Automated, TimeInForce.Gtc, pos.Quantity, 0, 0, "", "FlattenPosition "+DateTime.Now.ToString(), DateTime.Now, null); account.Submit(new[] { myFlattenOrderB }); } }
Comment
-
I was clarifying for those who might later come across.
If you look at my code, it takes in a collection of instruments just like the Flatten official API, and then checks each position to see if that position is in the instrument list, and if so then adds the newly created order to the collection. Once all the positions for the account have been iterated, it then takes the collection of orders created and submits them. My extension API should execute similar to Account.Flatten but allows one to specify the OrderAction.
The point I was making with the code you provided, is that your code iterated through the positions and created multiple orders, but only saved the last created order for submission. I suppose this is ok since an account can only have one open position per instrument.
Comment
-
Some other questions about OrderEntry.Automated vs OrderEntry.Manual.
Who is privy to this information and can it be used against my orders?
Can clearing houses and/or market makers use this information to funnel may trades into different buckets where they may or may not be front run by their own algos?
What is the cost of lying? Am I in violation of the law or just what CME Globex wants me to do in an ideal world?
At some point, I'll be far enough along to collect data on whether setting this value one way or the other impacts results. If it does, I'll obviously want to go where the money is, if it's not creating legal exposure for myself or others.
Thoughts?
Comment
-
Hello carnitron,
Thank you for your reply.
This is a regulatory thing set by and policed by the CME. We've seen cases where a user was using an addon to trade that placed trades automatically for him like a strategy that did not have this flag set to Automatic, and the CME noticed and suspended their account until the user reported they would correct it to reflect the automatic basis for their trades.
Please let us know if we may be of further assistance to you.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
627 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
359 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
562 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