the exit at target doesnt work. it exits with only the qty of the last entry.
I have multiple ENTRY points in the strategy. ( entry1Order , entry2Order)
and one target exit level .
I have an option to use one stop level or to exit 2/3 of a position at target and later to use trailing mechanism I have created.. at this stage I dont use the trailing !!!
looks like the order for target REPLACEs the previous order and not adding a new one with new qty.
what am I doing wrong pls?
(by the way, tried with canceling the order before creating a new on with a position.quantity , but sometimes it just cancels the target order entirely and I have no fill at target.)
protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment) { if (order.Name == "Long-0" || order.Name == "Short-0" ) { entry0Order = order; // Reset the entryOrder object to null if order was cancelled without any fill if (order.OrderState == OrderState.Cancelled && order.Filled == 0) entry0Order = null; } if (order.Name == "Long-A" || order.Name == "Short-A" ) { entry1Order = order; // Reset the entryOrder object to null if order was cancelled without any fill if (order.OrderState == OrderState.Cancelled && order.Filled == 0) entry1Order = null; } if (order.Name == "Long-B" || order.Name == "Short-B" ) { entry2Order = order; // Reset the entryOrder object to null if order was cancelled without any fill if (order.OrderState == OrderState.Cancelled && order.Filled == 0) entry2Order = null; } } //END of OnOrderUpdate() //*********************************************************************************************************** protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time) { if (entry0Order != null && entry0Order == execution.Order) { if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0)) { //STOP LOSS // stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - 4 * TickSize, "MyStop", "MyEntry"); if (Position.MarketPosition == MarketPosition.Long ) stopOrder = ExitLongStopMarket(1, true, Position.Quantity, HighOfRange - StopLossAmount, "MyStop", null); if (Position.MarketPosition == MarketPosition.Short ) stopOrder = ExitShortStopMarket(1, true, Position.Quantity, LowOfRange + StopLossAmount, "MyStop", null); } } if (entry1Order != null && entry1Order == execution.Order) { if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0)) { //STOP LOSS // stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - 4 * TickSize, "MyStop", "MyEntry"); if (Position.MarketPosition == MarketPosition.Long ) stopOrder = ExitLongStopMarket(1, true, Position.Quantity, HighOfRange - StopLossAmount, "MyStop", null); if (Position.MarketPosition == MarketPosition.Short ) stopOrder = ExitShortStopMarket(1, true, Position.Quantity, LowOfRange + StopLossAmount, "MyStop", null); } } if (entry2Order != null && entry2Order == execution.Order) { if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0)) { //STOP LOSS // stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - 4 * TickSize, "MyStop", "MyEntry"); if (Position.MarketPosition == MarketPosition.Long ) stopOrder = ExitLongStopMarket(1, true, Position.Quantity, HighOfRange - StopLossAmount, "MyStop", null); if (Position.MarketPosition == MarketPosition.Short ) stopOrder = ExitShortStopMarket(1, true, Position.Quantity, LowOfRange + StopLossAmount, "MyStop", null); } } //Target //============= if ((entry0Order != null && entry0Order == execution.Order) || (entry1Order != null && entry1Order == execution.Order) || (entry2Order != null && entry2Order == execution.Order)) { if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0)) { //if (targetOrder != null) CancelOrder(targetOrder); if (Position.MarketPosition == MarketPosition.Long) { if (UseTrailingStop==false) targetOrder = ExitLongMIT(1, true, Convert.ToInt32(execution.Order.Filled), HighOfRange + InitialPartialTarget, "MyTarget", null); else { //if (targetOrder != null) CancelOrder(targetOrder); targetOrder = ExitLongMIT(1, true, Convert.ToInt32(Math.Floor(Convert.ToDouble(execution.Order.Filled)*2/3)) , HighOfRange + InitialPartialTarget, "MyTarget", null); } } if (Position.MarketPosition == MarketPosition.Short) { if (UseTrailingStop==false) targetOrder = ExitShortMIT(1, true, Convert.ToInt32(execution.Order.Filled), LowOfRange - InitialPartialTarget, "MyTarget", null); else { //if (targetOrder != null) CancelOrder(targetOrder); targetOrder = ExitShortMIT(1, true, Convert.ToInt32(Math.Floor(Convert.ToDouble(execution.Order.Filled) *2/3)), LowOfRange - InitialPartialTarget, "MyTarget", null); } } } } // Reset our orders' Order objects after our position is closed. if ((stopOrder != null && stopOrder == execution.Order) || (targetOrder != null && targetOrder == execution.Order) || execution.Order.Name == "Sell" || execution.Order.Name == "Buy to cover" ) { if (execution.Order.OrderState == OrderState.Filled ) // || execution.Order.OrderState == OrderState.PartFilled) { if (targetOrder != null && targetOrder == execution.Order) { todayTargetRangeReached = true; } if (execution.Order.Name == "Sell" || execution.Order.Name == "Buy to cover") todayTargetRangeReached = false ; CancelOrder(entry0Order); CancelOrder(entry1Order); CancelOrder(entry2Order); CancelOrder(stopOrder); CancelOrder(targetOrder); stopOrder = null; targetOrder = null; entry0Order = null; entry1Order = null; entry2Order = null; Order0ARMED = false; Order1ARMED = false; Order2ARMED = false; } } } //End of OnExecutionUpdate()
Comment