In my strategy, when an entry order is filled, I submit a stoploss order and a target order one after the other (stop first, then target):
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{
...
stopPrice = execution.Order.AverageFillPrice - StoplossTicks*TickSize;
exitLongStop(execution.Order.Quantity, stopPrice, STOP_LOSS_1_ORDER_NAME, oco1ExitUnique);
targetPrice = execution.Order.AverageFillPrice + Target1Ticks*TickSize;
exitLongTarget(execution.Order.Quantity, targetPrice, PROFIT_TARGET_1_ORDER_NAME, oco1ExitUnique);
...
}
private void exitLongStop(int posSize, double price, string name, string oco) {
SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, posSize, 0.0, price, oco, name);
}
private void exitLongTarget(int posSize, double price, string name, string oco) {
SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Limit, posSize, price, 0.0, oco, name);
}
It looks like even though I submit the stop order first, the target ends up getting filled first, ie before the stop order even gets accepted. I suspect this is just due to how submitted orders can sometimes travel through the internet. Is there any way to force the stop order to get there first so this error doesn't happen?
Comment