How do I get the orderID that the user placed so I can close that order in the strategy if needed?
Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
getting proper OrderID
Collapse
X
-
getting proper OrderID
I have a strategy that will place orders, but there is a scenario that I have that if a user has an order in play and then starts the strategy it will take the users orderID as the first transaction. When flat then the strategy will take over automatically placing orders
How do I get the orderID that the user placed so I can close that order in the strategy if needed?Tags: None
-
Hello ballboy11,
Thank you for your post.
You can use the Account class to pull the Orders collection. Please visit the following link for more information: https://ninjatrader.com/support/help...ount_class.htm
Please let me know if you have any questions.
-
I am close I have my auto entries working perfectly but I don't know how to close my manual entry this is what I have. This scenario is if the user placed an order THEN turned on the strategy.
if(bManualOrder==true)
{
Print("Manual order is true");
ExitLong();
ExitShort();
}
i know in my logic i can get to my manual order but I don't know how to close I am manually using an atmstrategy called AtmStrategyTemplate.
Lastly, it is a very simple strategy it will only have 1 contract. I just added exitlong and exit short because i wasn't sure how to close.Last edited by ballboy11; 03-08-2018, 06:54 PM.
Comment
-
how to find an active order in a strategy that the strategy did not place
what I want is have a strategy that the user can place the order then start the strategy.
My scenario would be this.
Stopped out
Profit target
Strategy closes order.
after that scenario is finished the strategy will place orders.
condition is managed. i can get the strategy to place, cancel and close orders but I can not find the manual order to close.
Comment
-
Hello ballboy11,
Thank you for your response.
If you utilize the logic to pull the account orders for the instrument the strategy is running on you would then need to use the Account functions for CreateOrder() and Submit(). The managed approach functions for strategies (such as ExitLong() and ExitShort()) will not work to close the account position as they focus on the Strategy position. Please visit the following link for information on account versus strategy position: https://ninjatrader.com/support/help..._account_p.htm
CreateOrder(): https://ninjatrader.com/support/help...reateorder.htm
Submit(): https://ninjatrader.com/support/help...-us/submit.htm
Please let me know if you have any questions.
Comment
-
Originally posted by ballboy11 View Postif the order was created BEFORE the strategy started will create order work?
Thank you for your response.
The Account Orders collection needs to be used to capture the order if you wish to manage it. If you need to manage a position then you would use Create and Submit.
Please let me know if you have any questions.
Comment
-
i am not sure if i am in the the right area for closing an order
Print("I am in the account loop");
DateTime tCheckTime = new DateTime(2018, 01, 01);
foreach (Order order in Position.Account.Orders)
{
// Print(String.Format("Order placed: {0} - {1}", order.Name, order.OrderAction));
if (order.OrderState == OrderState.Filled)
{
if (tCheckTime < order.Time)
{
lManualOrderID = order.Id;
tCheckTime = order.Time;
}
Print(order.Name);
}
if(order.OrderState == OrderState.Working)
{
lManualTargetID = order.Id;
}
if(order.OrderState == OrderState.Accepted)
{
lManualStop1ID = order.Id;
}
// }
Order stopOrder;
stopOrder = Account.CreateOrder(myInstrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Manual, TimeInForce.Day, 1, 0, 1400, "", "stopOrder", Core.Globals.MaxDate, null);
Account.Submit(new[] { stopOrder });
myAccount.Submit(new[] { stopOrder });
Comment
-
Hello ballboy11,
Thank you for your patience.
To cancel an order on the account for the instrument that the strategy is running on you can use the following:
Code:private Account acc; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Indicator here."; Name = "TestGrabAccountOrder"; } else if (State == State.Configure) { lock (Account.All) acc = Account.All.FirstOrDefault(a => a.Name == "Sim101"); } else if (State == State.Realtime) { if (acc.Orders.Count != 0) { acc.CancelAllOrders(Instrument); } } }
Please let me know if you have any questions.
Comment
-
I figured how to close my position. In a strategy if a manual strategy is entered and then the strategy starts MarketPosition will display flat eventhough there is a position activated.
I can close the targets and stops AND I can close the postion. by CreateOrder AND Submit.
There is a scenario that I do have an issue with. If a target is hit OR a stop is hit AND at the time I close the order with CreateOrder it will of course make one. What I need to do is NOT Create the order. If MarketPosition is flat in a strategy because it can not find the Manual order what trigger do I use in order to see if the target was hit. i tried these triggers.
protected override void OnAccountItemUpdate(Cbi.Account account, Cbi.AccountItem accountItem, double value)
{
Print(" in OnAccountItemUpdate");
}
protected override void OnConnectionStatusUpdate(ConnectionStatusEventArgs connectionStatusUpdate)
{
}
protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
Cbi.MarketPosition marketPosition, string orderId, DateTime time)
{
Print("in onExecutionUpdate");
}
protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,
int quantity, int filled, double averageFillPrice,
Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
{
Print("In Order update");
}
protected override void OnPositionUpdate(Cbi.Position position, double averagePrice,
int quantity, Cbi.MarketPosition marketPosition)
{
Print("in On Position Update");
}
All will not show if the target or stop hit except OnAccountItemUpdate. This one activates every tick when there is a positon active.
Comment
-
Hello ballboy11,
Thank you for your response.
It looks like you are mixing the Strategy functions with the Account functions. You actually want to use the OrderUpdate of the Account class to see when orders on the account fill. Please visit the following link for more information and an example on how to use the function: https://ninjatrader.com/support/help...rderupdate.htm
Please let me know if you have any questions.
Comment
-
I am going through the orders create order etc. and this is where I am having the issue.
I can not find my manually entered order if I place the Manual order BEFORE the strategy has started
All I want to do is close my Manual order at a specific time if my order has not hit the target OR hit the stoploss.
This is the logic in the area
{
//ISSUES: CAN NOT FIND MANUAL ORDER POSITION SIZE IF IT LONG OR SHORT
// IT ALWAYS STATES MARKET POSITION IS FLAT IF MANUAL ORDER IS ENTERED
// IF MANUAL POSITION IS CLOSED BY TARGET OR STOP CREATE ORDER WILL
// CREATE A SELL.
// NEED TO FIND POSITION SIZE POSITION DIRECTION CLOSE MANUAL POSITON
//SCENARIO OF AUTOTRADE HAS NO ISSUES
int tHour = Trade1_EndTime.Hour;
int tMinute = Trade1_EndTime.Minute;
Print("In Closing section");
// CLOSE ALL ORDERS IF MANUAL OR STRATEGY DRIVEN
if (tHour == tMarketHour && tMinute == tMarketMinute)
{
bTrade1Complete = true;
// IF A MANUAL ORDER DO SCENARIO ELSE DO CLOSE STRATEGY SCENARIO
if (bManualOrder == true)
{
if (acc.Orders.Count != 0)
{
//THIS SECTION WORKES FOR CANCELLING ALL TARGETS AND STOPS
acc.CancelAllOrders(Instrument);
//MANUAL ORDER MARKET POSTION IS ALWAYS FLAT EVEN IF THERE IS AN ACTIVE POSITION
// if (Position.MarketPosition == MarketPosition.Long)
// {
Print("marketPosition is " + Position.MarketPosition );
Order stopOrder;
stopOrder = acc.CreateOrder(Instrument,
OrderAction.Sell,
OrderType.Market,
OrderEntry.Automated,
TimeInForce.Day,
1, 0, 0, "", "",
Core.Globals.MaxDate, null);
acc.Submit(new[]{stopOrder});
// }
// else if (Position.MarketPosition == MarketPosition.Short)
}
}
else
{
//THIS WORKS PERFECTLY IF THERE IS NO MANUAL ORDER ENTERED BEFORE THE STRATEGY HAS STARTED
if (GetAtmStrategyMarketPosition(atmStrategyId) != MarketPosition.Flat)
{
Print("in close time");
AtmStrategyClose(atmStrategyId);
}
}
atmStrategyId = String.Empty;
orderId = String.Empty;
}
}
}
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by bmartz, Today, 09:30 AM
|
2 responses
11 views
0 likes
|
Last Post
by bltdavid
Today, 11:24 AM
|
||
Started by f.saeidi, Today, 11:02 AM
|
1 response
3 views
0 likes
|
Last Post Today, 11:08 AM | ||
Started by geotrades1, Today, 10:02 AM
|
4 responses
12 views
0 likes
|
Last Post
by geotrades1
Today, 11:08 AM
|
||
Started by rajendrasubedi2023, Today, 09:50 AM
|
3 responses
16 views
0 likes
|
Last Post Today, 10:57 AM | ||
Started by lorem, Today, 09:18 AM
|
2 responses
11 views
0 likes
|
Last Post Today, 10:55 AM |
Comment