as basis for this code.
What I'm trying to do is:
- Enter long 2 contracts at limit
- Have the same initial stop loss for both contracts
- Have different targets for each contract
- Be able to trail my stop on 2nd contract (Long 1b)
The problem I'm having is the the 1st profit target order below (targetOrder1) triggers but not the stop orders (stopOrder1)...
stopOrder1 = ExitLongStop(0, true, 1, execution.Price - 8 * TickSize, "stop", "Long Entry");
targetOrder1 = ExitLongLimit(0, true, 1, execution.Price + 4 * TickSize, "target1a", "Long Entry");
stopOrder2 = ExitLongStop(0, true, 1, execution.Price - 8 * TickSize, "stop1b", "Long Entry");
targetOrder2 = ExitLongLimit(0, true, 1, execution.Price + 20 * TickSize, "target1b", "Long Entry");
Can anyone please point me in the right direction?

protected override void OnBarUpdate()
{
// Return if there aren't enough loaded bars
if (CurrentBar < 20)
return;
// First, we need a simple entry. Then entryOrder == null checks to make sure entryOrder does not contain an order yet.
if (Position.MarketPosition == MarketPosition.Flat)
{
bLongSignal = Close[1] > SMA(20)[1];
bShortSignal = Close[1] < SMA(20)[1];
// Check IOrder objects for null to ensure there are no working entry orders before submitting new entry order
if (entryOrder == null && bLongSignal)
{
/* Our IOrder object, entryOrder, is assigned an entry order.
It is offset 5 ticks below the low to try and make it not execute to demonstrate the CancelOrder() method. */
entryOrder = EnterLongLimit(0, true, 2, Median[0], "Long Entry");
// Here, we assign barNumberOfOrder the CurrentBar, so we can check how many bars pass after our order is placed.
barNumberOfOrder = CurrentBar;
}
// If entryOrder has not been filled within 3 bars, cancel the order.
else if (entryOrder != null && CurrentBar > barNumberOfOrder + 3)
{
// When entryOrder gets cancelled below in OnOrderUpdate(), it gets replaced with a Market Order via EnterLong()
CancelOrder(entryOrder);
}
}
}
protected override void OnOrderUpdate(IOrder order)
{
// Checks for all updates to entryOrder.
if (entryOrder != null && entryOrder.Token == order.Token)
{
// Check if entryOrder is cancelled.
if (order.OrderState == OrderState.Cancelled)
{
// Reset entryOrder back to null
entryOrder = null;
// Replace entry limit order with a market order.
//mar****rder = EnterLong(1, "market order");
}
}
}
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.
This first if-statement is in place to deal only with the long limit entry. */
if (entryOrder != null && entryOrder.Token == execution.Order.Token)
{
// This second if-statement is meant to only let fills and cancellations filter through.
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
// Simple stop and target
stopOrder1 = ExitLongStop(0, true, 1, execution.Price - 8 * TickSize, "stop", "Long Entry");
targetOrder1 = ExitLongLimit(0, true, 1, execution.Price + 4 * TickSize, "target1a", "Long Entry");
stopOrder2 = ExitLongStop(0, true, 1, execution.Price - 8 * TickSize, "stop1b", "Long Entry");
targetOrder2 = ExitLongLimit(0, true, 1, execution.Price + 20 * TickSize, "target1b", "Long Entry");
// Resets the entryOrder object to null after the order has been filled
if (execution.Order.OrderState != OrderState.PartFilled)
{
entryOrder = null;
}
}
}
}

Comment