I am trying to place a stop-loss order (in this case, for a short position) by following the steps from the sample code "SampleOnOrderUpdate.zip". However, for some reason, after the short entry order is executed and the stop-loss order is placed, the stop-loss order is automatically closed after the close of the next bar, and I cannot figure out why this is happening.
[B]Variables[/B] private Order entryOrder = null; private Order stopOrder = null;
else if [B](State == State.Realtime)[/B]
{
if (stopOrder != null)
stopOrder = GetRealtimeOrder(stopOrder);
if (entryOrder != null)
entryOrder = GetRealtimeOrder(entryOrder);
}
[B]protected override void OnOrderUpdate[/B](Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
{
if (order.OrderState == OrderState.Cancelled)
{
if (order == stopOrder)
stopOrder = null;
if (order == entryOrder)
entryOrder = null;
}
}
OnBarUpdate
if (Position.MarketPosition == MarketPosition.Flat
&& Trigger
)
{
if (QTY1 > 0)
entryOrder = EnterShort(Convert.ToInt32(QTY1), "ShortEntry");
calculateDynamicSL = false;
}
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{
if (execution.Order != null && execution.Order.OrderState == OrderState.Filled && Position.MarketPosition == MarketPosition.Short)
{
if (execution.Order.Name == "ShortEntry")
{
double stopLossPrice = execution.Order.AverageFillPrice + 40 * TickSize;
if (stopOrder == null || stopOrder.OrderState == OrderState.Cancelled)
{
stopOrder = ExitShortStopMarket(Convert.ToInt32(QTY1), stopLossPrice, "SL", "ShortEntry");
}
}
}
}
Thanks

Comment