My strategy enters a limit order, then sets the stop loss and profit target in OnExecution(). I essentially used the code provided in SampleOnOrderUpdate. My strategy wants to move my stop when the order goes into profit. The problem is the IOrder object for my stop order is null, even though the stop order exists. Here is OnExecution code when I place the stop & target. Below that is the code used to attempt to move the stop, which is in a method called from OnBarUpdate().
protected override void OnExecution(IExecution execution)
{
/* 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. */
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 (execution.MarketPosition==MarketPosition.Long)
{
stopOrder = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - StopLoss * TickSize, "MyStop", "Monkey Long");
targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + ProfitTarget * TickSize, "MyTarget", "Monkey Long");
}
if (execution.MarketPosition==MarketPosition.Short)
{
stopOrder = ExitShortStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + StopLoss * TickSize, "MyTarget", "Monkey Short");
targetOrder = ExitShortLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - ProfitTarget * TickSize, "MyStop", "Monkey Short");
}
// Resets the entryOrder object to null after the order has been filled
if (execution.Order.OrderState != OrderState.PartFilled)
{
entryOrder = null;
}
}
}
// Reset our stop order and target orders' IOrder objects after our position is closed.
if ((stopOrder != null && stopOrder == execution.Order) || (targetOrder != null && targetOrder == execution.Order))
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
{
stopOrder = null;
targetOrder = null;
}
}
}
Here is my code used to try and move my stop. I've printed all values to Output for debugging, including "my condition", and stopOrder. It shows stopOrder is null, when there is actually a live stop order, so the code never triggers.
if (stopOrder != null && my condition exists)
{
stopOrder = ExitLongStop(0, true, stopOrder.Quantity, Position.AvgPrice - newStop * TickSize, "MyStop", "Monkey Long");
}
Any ideas what is going on here?
Thanks

Comment