When using Unmanaged approach, and submitting multiple entry orders via the SubmitOrderUnmanaged() method, and there are more than one open positions at a time, the exit orders submitted for those open positions are filled in a chronological order instead of their respective entry order's exit order.
for example:
Entry orders: STD_1_Entry_1, STD_2_Entry_1.
Exit orders: STD_1_T1, STD_1_L1, STD_2_T1, STD_2_L1.
Fill price for exit limit orders: STD_1_T1 = 100.0, STD_2_T1 = 99.0.
STD_1_Entry_1 will be filled one bar before STD_2_Entry_1.
but the open position relating to STD_1_Entry_1 will be filled by STD_2_T1, since its lower.
Chart Image:
Strategy analyzer Trades:
As you can see, the exit order filled the first open position available.
the question is:
How can I connect entry orders to their corresponding exit orders, and essentially have the open position of that entry order only fill exit orders directly related to it ( unless its time in force exits the open position).
This is the code for entry orders, it is submitted via OnBarUpdate(), the calculation is OnBarClose:
Order entryOrder = SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Market, quantity, 0, 0, "", signalName);
I have created a custom class to keep orders as a reference for different uses (exit order advancing, canceling, etc.), and In this example I am changing orders to reduce profit target value each bar to avoid big loss on trades, but keeping them in sync inside my order managing class.
This is the currently used code to enter those exit orders, they are connected by an OCO following the order managing class Id system i put in place
var stopLossOrder = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, 1, 0, entryOrderStopPrice,orderManager.GenerateUniqueExitOrderSignalName(orderDetails.Pattern, "", orderQuantity), orderManager.GenerateUniqueExitOrderSignalName(orderDetails.Pattern, "L", orderQuantity)); var profitTargetOrder = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Limit, 1, entryOrderProfitTarget, 0, orderManager.GenerateUniqueExitOrderSignalName(orderDetails.Pattern, "", orderQuantity), orderManager.GenerateUniqueExitOrderSignalName(orderDetails.Pattern, "T", orderQuantity));
This is the code for changing orders
// profitTargetOrder is assigned the right order object via the order managing class. ChangeOrder(profitTargetOrder, profitTargetOrder.Quantity, profitTargetPrice, 0);
Aviram Y.

Comment