So I set the Stop and Target orders when the original order is filled (using OnExecution). The Target qty is 1/3 of the qty. When the target order is filled, I would like to change the stop, to be over the entry price, to reduce the risk, and I would like to move it, and I am riding the wins.
So on OnExecution of the target order, when I reset the stop order, I may get this message:
**NT** A Sell stop order placed at '11/1/2013 7:05:00 AM' has been ignored since the stop price is greater than or equal to close price of the current bar. This is an invalid order and subsequent orders may also be ignored. Please fix your strategy.
You can see the example at 07:10. My guess is the error happening intrabar, and considered as 7:05, closes at 4290.0, which happen to be below the entry price 4291.0.
How can you deal with it?
Here is my OnExecution:
protected override void OnExecution(IExecution execution) { int eStopQty, eTargetQty; double eOrderStopPrice; string eOrderName; // once the order is filled, set the stop and the target if (entryOrder != null && entryOrder == execution.Order) { if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0)) { eStopQty = execution.Order.Filled; eTargetQty = eStopQty < 3 ? 1 : (int)Math.Round((double)eStopQty / 3,0); eOrderName = execution.Order.Name; if (execution.MarketPosition == MarketPosition.Long) { // Stop Order stopOrder = ExitLongStop(0, true, eStopQty, orderStopPrice - (stopPassThrough ? priceTouch : 0), eOrderName+".Loss", eOrderName); // Target Order targetOrder = ExitLongLimit(0, true, eTargetQty, orderTargetPrice + (targetPassThrough ? priceTouch : 0), eOrderName+".Profit", eOrderName); } else { // Stop Order stopOrder = ExitShortStop(0, true, eStopQty, orderStopPrice + (stopPassThrough ? priceTouch : 0), orderName+".Loss", orderName); // Target Order targetOrder = ExitShortLimit(0, true, eTargetQty, orderTargetPrice - (targetPassThrough ? priceTouch : 0), orderName+".Profit", orderName); } #if (DEBUG) this.Debug("eOrderName="+eOrderName+", Stop:[Price="+orderStopPrice+", Qty="+eStopQty+"], Target:[Price="+orderTargetPrice+", Qty="+eTargetQty+"]"); #endif // Resets the entryOrder object to null after the order has been filled if (execution.Order.OrderState != OrderState.PartFilled) { entryOrder = null; } } } // set new stop for the rest of the qty, when target reached if (targetOrder != null && targetOrder == execution.Order && (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)) { setWinStop(Position.AvgPrice, execution.Price, trialingStopTicks); eStopQty = Position.Quantity; eOrderStopPrice = getWinStop(); eOrderName = execution.Order.FromEntrySignal; #if (DEBUG) this.Debug("Target filled="+eOrderName+", FilledPrice="+targetOrder.AvgFillPrice +", Price="+eOrderStopPrice+", Qty="+eStopQty +", Dir="+execution.Order.OrderAction +", Filled="+execution.Order.OrderState+"]"+execution); #endif if (execution.Order.OrderAction == OrderAction.Sell || execution.Order.OrderAction == OrderAction.SellShort) // its the opposit dir { stopOrder = ExitLongStop(0, true, eStopQty, eOrderStopPrice - (stopPassThrough ? priceTouch : 0), eOrderName+".Stop", eOrderName); #if (DEBUG) this.Debug("ExitLongStop="+stopOrder); #endif } else { stopOrder = ExitShortStop(0, true, eStopQty, eOrderStopPrice + (stopPassThrough ? priceTouch : 0), eOrderName+".Stop", eOrderName); #if (DEBUG) this.Debug("ExitShortStop="+stopOrder); #endif } targetOrder = null; orderTimeBaseCounter = Off; } // Reset our stop order and target orders' IOrder objects after our position is closed. if (stopOrder != null && stopOrder == execution.Order && (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)) { stopOrder = null; targetOrder = null; orderTimeBaseCounter = Off; } }
Comment