string[] instruments = {"ES"}
protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice, int quantity, int filled,
double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
{
...
else if (order.Name == "EnterLong_" + instruments[0] || order.Name == "EnterShort_" + instruments[0])
{
if (orderState == OrderState.Filled)
{
SendBracket_Orders(order.Name);
}
}
...
}
private void SendBracket_Orders(string orderName)
{
Print("sending bracket orders...");
string ocoString = string.Format("unmanagedexitlongoco{0}-{1}", DateTime.Now.ToString("hhmmssffff"), instruments[0]);
if (orderName == "EnterLong_" + instruments[0])
{
double profitTargetPrice = PositionAccount.AveragePrice + tpInTicks * TickSize;
double stopLossPrice = PositionAccount.AveragePrice - slInTicks * TickSize;
SubmitOrderUnmanaged(BarsInProgress, OrderAction.Sell, OrderType.MIT, quantities[0], 0, profitTargetPrice, ocoString, "profit_target_long");
SubmitOrderUnmanaged(BarsInProgress, OrderAction.Sell, OrderType.StopMarket, quantities[0], 0, stopLossPrice, ocoString, "stop_loss_long");
}
else if (orderName == "EnterShort_" + instruments[0])
{
double profitTargetPrice = PositionAccount.AveragePrice - tpInTicks * TickSize;
double stopLossPrice = PositionAccount.AveragePrice + slInTicks * TickSize;
SubmitOrderUnmanaged(BarsInProgress, OrderAction.Buy, OrderType.MIT, quantities[0], 0, profitTargetPrice, ocoString, "profit_target_short");
SubmitOrderUnmanaged(BarsInProgress, OrderAction.Buy, OrderType.StopMarket, quantities[0], 0, stopLossPrice, ocoString, "stop_loss_short");
}
else
{
Print("Can't submit bracket orders for flat position");
return;
}
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Timing issues for bracket orders submission
Collapse
X
-
Timing issues for bracket orders submission
Hello! I have some issues implementing my bracket orders strategy. When I submit an market order (Buy or Sell) the bracket orders (target profit and stop loss) should be send too according to this piece of code :
However, that is not always the case in reality. Sometimes the market order is filled but the position is not yet open and therefore the PositionAccount.AveragePrice variable seems empty which leads to an incorrect calculation of the stop loss price and target profit price values when entering the "SendBracket_Orders" method. Maybe I could implement a while loop in that method that would wait for the position to be open or maybe there is a better solution to this problem ?Code:Last edited by Tessan; 03-14-2025, 12:30 PM. -
Hello Tessan,
Thank you for your post.
We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate() which ensures your strategy has received the execution which is used for internal signal tracking. By doing so, you can avoid having your strategy submit stops/targets before the entry is working.
The post linked below has a strategy (UnmanagedTemplate_December2020.zip) that demonstrates how to trigger stop/target order through OnExecution() while using the unmanaged approach for orders:
Please let me know if I may be of any further assistance.
- Likes 1
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Yesterday, 05:17 AM
|
0 responses
54 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
131 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
73 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
44 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
49 views
0 likes
|
Last Post
|

Comment