The code is below. Am I misunderstanding how CancelOrder is supposed to work? There are some print statements I have which show that CancelOrder was called but it's not always being called apparently.
===
#region Variables
private IOrder MyEntryOrder = null; // This variable holds an object representing our entry order.
private IOrder stopOrder = null; // This variable holds an object representing our stop loss order.
private IOrder targetOrder = null; // This variable holds an object representing our profit target order.
private int barNumberOfOrder = 0; // This variable is used to store the entry bar
#endregion
protected override void OnBarUpdate()
{
...
Print("BarOrderNumber = " + barNumberOfOrder);
Print("CurrentBar = " + CurrentBar);
// First, we need a simple entry. Then MyEntryOrder == null checks to make sure MyEntryOrder does not contain an order yet.
if (Position.MarketPosition == MarketPosition.Flat)
{
{
if (MARKET_DIRECTION == "short")
{
/* Our IOrder object, MyEntryOrder, is assigned an entry order. */
MyEntryOrder = EnterShortLimit(0, true, 1, GetCurrentBid(), "short limit entry");
// Here, we assign barNumberOfOrder the CurrentBar, so we can check how many bars pass after our order is placed.
barNumberOfOrder = CurrentBar;
}
else if (MARKET_DIRECTION == "long")
{
/* Our IOrder object, MyEntryOrder, is assigned an entry order. */
MyEntryOrder = EnterLongLimit(0, true, 1, GetCurrentAsk(), "long limit entry");
// Here, we assign barNumberOfOrder the CurrentBar, so we can check how many bars pass after our order is placed.
barNumberOfOrder = CurrentBar;
}
}
// If MyEntryOrder has not been filled within 5 bars, cancel the order.
else if (MyEntryOrder != null && CurrentBar > barNumberOfOrder + 5)
{
// When MyEntryOrder gets cancelled below in OnOrderUpdate(), it gets replaced with a Market Order via EnterLong()
CancelOrder(MyEntryOrder);
}
}
}
protected override void OnOrderUpdate(IOrder order)
{
// Checks for all updates to MyEntryOrder.
if (MyEntryOrder != null && MyEntryOrder == order)
{
// Check if MyEntryOrder is cancelled.
if (order.OrderState == OrderState.Cancelled)
{
// Reset MyEntryOrder back to null
MyEntryOrder = null;
currentOrderDirection = "";
}
}
}
protected override void OnExecution(IExecution execution)
{
if (MyEntryOrder != null && MyEntryOrder == execution.Order)
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
if (currentOrderDirection == "long")
{
// Simple stop and target
stopOrder = ExitLongStop(0, true, 1, execution.Price - (AcceptableLoss * TickSize), "stop", "long limit entry");
targetOrder = ExitLongLimit(0, true, 1, execution.Price + (DesiredProfit * TickSize), "target", "long limit entry");
}
else if (currentOrderDirection == "short")
{
// Simple stop and target
stopOrder = ExitShortStop(0, true, 1, execution.Price + (AcceptableLoss * TickSize), "stop", "short limit entry");
targetOrder = ExitShortLimit(0, true, 1, execution.Price - (DesiredProfit * TickSize), "target", "short limit entry");
}
// Resets the MyEntryOrder object to null after the order has been filled
if (execution.Order.OrderState != OrderState.PartFilled)
{
MyEntryOrder = null;
currentOrderDirection = "";
}
}
}
}
Comment