I'm trying to use limit orders in a 'Stop and Reverse' strategy. I would like to make sure the prior limit order (if unfilled) has been cancelled before placing a new limit order in the opposition direction. In addition, if the order was filled, the position would be exited before taking a position in the opposite direction. Can someone look at the code below and let me know if I'm on the right path to accomplishing this?
I'm working with these variables/Order objects:
bool notExecutedShort = true;
IOrder xLong = null;
IOrder xShort = null;
I'm setting the bool variables in the OnExecute method:
protected override void OnExecution(IExecution execution)
{
if (xLong != null && xLong == execution.Order)
notExecutedLong = false;
if (xShort != null && xShort == execution.Order)
notExecutedShort = false;
}
Here's the code within OnBarUpdate:
{
if (notExecutedShort) // Short order not filled
{
CancelOrder(xShort);
while (xShort.OrderState != OrderState.Cancelled)
Thread.Sleep(100); // sleep for 1/10th of a second, do not proceed until order has been cancelled
}
else if (!notExecutedShort) // Short order was filled
{
ExitShort("Short");
}
xShort = null;
xLong = EnterLongLimit(0, true, qty, GetCurrentBid(), "Long");
notExecutedLong = false;
}
else if (lowIndex == 0) // Cancel Long, Enter Short
{
if (notExecutedLong) // Long order not filled
{
CancelOrder(xLong);
while (xLong.OrderState != OrderState.Cancelled)
Thread.Sleep(100); // sleep for 1/10th of a second, do not proceed until order has been cancelled
}
else if (!notExecutedLong) // Long order was filled
{
ExitLong("Long");
}
xLong = null;
xShort = EnterShortLimit(0, true, qty, GetCurrentAsk(), "Short");
notExecutedShort = false;
}
Thanks,
gbrad
Comment