I'm entring a trade and want to make sure my stop loss is updated each time it gets a partial fill.
I wonder if the correct way to do this is to simply cancel the previous stop order and create a new stop order with an updated quantity as follows (Whenever I get a notification that the entryOrder was udpated, I check if the market position has changed. If so, I cancel the previous stopOrder and issue a new stop order with an updated quantity):
protected override void OnExecution(IExecution execution)
{
if (entryOrder != null && entryOrder == execution.Order)
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
if (stopOrder==null)
{
// Set ths stop loss for the entryOrder 20 cents cents below stop price
stopOrder = ExitLongStop(0, true, execution.Order.Filled, entryOrder.StopPrice-20* TickSize, "stop","entry");
} else if (stopOrder.Quantity!=Position.Quantity)
{
// Need to update the stop order with the correct quantity
CancelOrder(stopOrder);
stopOrder = ExitLongStop(0, true, execution.Order.Filled, entryOrder.StopPrice+20* TickSize, "stop" , "entry");
}
}
}
}
I'd appreciate if you could let me know if that's the right approach.
Regards,
Ron

Comment