In order to clean up outstanding orders and close positions when my strategy is terminated I have the following code in OnTermination(). For reference, theOrder is the IOrder structure associated with the order.
if (theOrder != null)
{
if (Position.MarketPosition == MarketPosition.Flat)
{
CancelOrder(theOrder);
}
else
{
if (Position.MarketPosition == MarketPosition.Long)
ExitLong("Long");
else if (Position.MarketPosition == MarketPosition.Short)
ExitShort("Short");
}
}
This code is, apparently, executing correctly, as positions are being closed and orders cancelled as appropriate. However, OnExecution() is not being called when these transactions occur. I've confirmed this with a Print("Execution: " + execution.Order.OrderState.ToString()) at the top of OnExecution(IExecution execution) which is not being hit when positions are closed in OnTermination() but is being hit when positions are closed elsewhere within the strategy. I do tabulations of orders and trade results in OnExecution(), and it is my understanding that it should be called whenever an execution is completed.
Is this behaviour expected? It sure isn't expected by me!
How can I ensure that an order which is cancelled or a position which is closed in OnTermination() will be reflected in OnExecution()? Or is there a better way to accomplish what I'm trying to do (ensure that I am flat relative to the strategy when the strategy is terminated while capturing those closing transactions in OnExecution())?
Your assistance would be most appreciated.
Comment