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.
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 });
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:
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 }); } }
JimNinjaTrader Customer Service
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.Kate W.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by gyilaoliver, Today, 08:28 AM
|
3 responses
12 views
0 likes
|
Last Post
|
||
Started by Darkslide_Tom, 03-23-2025, 11:08 PM
|
3 responses
17 views
0 likes
|
Last Post
|
||
Started by rtwave, 03-13-2025, 04:09 PM
|
4 responses
29 views
0 likes
|
Last Post
|
||
Started by MiCe1999, 12-01-2024, 09:01 PM
|
4 responses
45 views
0 likes
|
Last Post
![]() |
||
Started by coopgrafik, Today, 07:57 AM
|
1 response
7 views
0 likes
|
Last Post
|
Comment