or referring to https://ninjatrader.com/support/help...utionupdate.ht m (Rithmic/Interactive Brokers Friendly Approach)
I modified the ManagedRithmicIBFriendlyExample by adding "RealtimeErrorHandling = RealtimeErrorHandling.StopCancelCloseIgnoreRejects ;"
I am trying to close all positions and orders without stopping the strategy in case of rejection for "Sell stop or sell stop limit orders can't be placed above the market.".
What's the best way to perform such action ?
To the original code I added
region new added code mycode #endregion new added code
protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
{
#region new added code
if (orderState == OrderState.Rejected)
{
if (Position.Quantity == 0) // || sumFilledLong1 != 0
{
//using zero quantity
stopLossLong1 = ExitLongStopMarket(0, true, 0, 0.0, "StopLossLong1", "Long limit entry 1");
targetLong1 = ExitLongLimit(0, true, 0, 0.0, "TargetLong1", "Long limit entry 1");
//cancelling the order
CancelOrder(longEntry1);
//nulling any orders
longEntry1 = null;
CancelOrder(stopLossLong1);
stopLossLong1 = null;
CancelOrder(targetLong1);
targetLong1 = null;
}
if (Position.Quantity != 0) // || sumFilledLong1 != 0)
{
ExitLong();
longEntry1 = null;
CancelOrder(longEntry1);
longEntry1 = null;
CancelOrder(stopLossLong1);
stopLossLong1 = null;
CancelOrder(targetLong1);
targetLong1 = null;
}
sumFilledLong1 = 0;
LongEntry1Prices.Clear();
return;
}
#endregion new added code
#region original code
// Assign Order objects here
// This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not guaranteed to be complete if it is referenced immediately after submitting
if (order.Name == "Long limit entry 1")
longEntry1 = order;
else if (order.Name == "StopLossLong1")
stopLossLong1 = order;
else if (order.Name == "TargetLong1")
targetLong1 = order;
// Null Entry order if filled or cancelled. We do not use the Order objects after the order is filled, so we can null it here
if (longEntry1 != null && longEntry1 == order)
{
if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
longEntry1 = null;
if (order.OrderState == OrderState.Filled)
longEntry1 = null;
}
// Null Target and Stop orders if filled or cancelled. We do not use the Order objects after the order is filled, so we can null them here
if ((targetLong1 != null && targetLong1 == order)
|| (stopLossLong1 != null && stopLossLong1 == order))
{
if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
targetLong1 = stopLossLong1 = null;
if (order.OrderState == OrderState.Filled)
targetLong1 = stopLossLong1 = null;
#endregion original code
} //if (orderState == OrderState.Rejected)
} //OnOrderUpdate
Which of the following methods of cancelling the stop/targets are correct or preferable?
Are the two instructions below
stopLossLong1 = null;
CancelOrder(stopLossLong1);
equivalent?
And for curiosity, can one cancel the stop loss order by amending its quantity to zero as below?
ChangeOrder(stopLossLong1, 0, 0.0, 0.0);
Thank you
Gio

Comment