Thank you for your response.
You have already detailed that information and that would not provide the needed behavior. Please refer to my notes below as you will need to implement more logic to correctly exit the trade as needed.
First, your exit condition needs to be the following and only the following.
if (BarsSinceEntryExecution() >= 4 && Position.MarketPosition != MarketPosition.Flat && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 0)
if (BarsSinceEntryExecution() >= 4 && Position.MarketPosition != MarketPosition.Flat && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 0) { BackBrush = Brushes.LightSalmon; SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Market, Position.Quantity, 0, 0, "", "Close After Loss"); }
protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment) { if (order.Name == "LongMarket" && entryOrder == null) entryOrder = order; if (entryOrder != null && entryOrder.OrderState == OrderState.Cancelled) entryOrder = null; if (order.Name == "Profit Taker" && profitTakerOrder == null) profitTakerOrder = order; if (profitTakerOrder != null && profitTakerOrder.OrderState == OrderState.Cancelled) profitTakerOrder = null; if (order.Name == "Stop Loss" && stopLossOrder == null) stopLossOrder = order; if (stopLossOrder != null && stopLossOrder.OrderState == OrderState.Cancelled) stopLossOrder = null; }
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time) { if (execution.Order.OrderState != OrderState.Filled) return; [B] if (execution.Order.Name == "Close After Loss") { CancelOrder(profitTakerOrder); CancelOrder(stopLossOrder); }[/B]
I highly recommend reviewing the documentation on Order objects, OnOrderUpdate(),
OnExecutionUpdate(), and Unmanaged Order Approach.
- Order: https://ninjatrader.com/support/help...n-us/order.htm
- OnOrderUpdate(): https://ninjatrader.com/support/help...rderupdate.htm
- OnExecutionUpdate(): https://ninjatrader.com/support/help...tionupdate.htm
- Unmanaged Order Approach: https://ninjatrader.com/support/help...d_approach.htm
Please let me know if you have any questions.
Comment