In the meantime, I’m working in parallel on the code that doesn’t freeze, which uses ExitLongStop/ExitShortStop. Now that Bertrand was kind enough to inform me about TraceOrders, I’ve figured out why my stop is not being submitted. I get the following message:
“A BuyToCover stop order placed at '10/21/2011 8:59:00 AM' has been ignored since the stop price is less than or equal to the close price of the current bar. This is an invalid order and subsequent orders may also be ignored. Please fix your strategy.”
Now look at the attached image to see the trade. Remember that entry occurs on breakage of a trendline. Black dots are stop levels, yellow profit targets.
So at 7:35 the strategy goes long. At 8:59, the strategy reverses position and goes short. But look where this happens! It happens near the low of the candle, obviously some time before the close. Yes the close is above the stop level, but so what? When the stop was submitted, the price was down there around the low!
Here is where the stop and reverse orders are being placed:
protected override void OnExecution(IExecution execution)
{
if(longStopOrder != null && longStopOrder == execution.Order && shortOnReversal)
{
CancelOrder(longTargetOrder);
}
if(shortStopOrder != null && shortStopOrder == execution.Order && longOnReversal)
{
CancelOrder(shortTargetOrder);
}
}
/// <summary>
/// Called on each incoming order event
/// </summary>
protected override void OnOrderUpdate(IOrder order)
{
// When a position is stopped out, the target order is canceled in OnExecution. Check for that condition, and then place entry orders
if (longTargetOrder != null && longTargetOrder == order && shortOnReversal
&& order.OrderState == OrderState.Cancelled && order.Filled == 0)
{
shortStopOrder = ExitShortStop(shortStop, "short_entry");
shortTargetOrder = ExitShortLimit(shortPT, "short_entry");
EnterShortStopLimit(shortEntry, shortEntry, "short_entry");
}
if (shortTargetOrder != null && shortTargetOrder == order && longOnReversal
&& order.OrderState == OrderState.Cancelled && order.Filled == 0)
{
longStopOrder = ExitLongStop(longStop, "long_entry");
longTargetOrder = ExitLongLimit(longPT, "long_entry");
EnterLongStopLimit(longEntry, longEntry, "long_entry");
}
}
Comment