I opened a similar discussion a few weeks ago, I thought I had solved my issue, but apparently it's still there.
Below is the strategy I am running, and attached the trade log that shows the issue
In summary I want the strategy to enter via limit order when a cross over occurs and exit at market when the cross over occurs in the opposite direction. The limit order is valid only for 2 bars after the cross over occurs. The code apparently works fine, BUT I noticed it duplicates my position when exit and new entry occur on the same bar session. I think this might be due to the fact that when the limit order is entered the market position is NOT flat, hence Ninjatrader enters the limit order equal to 2x default quantity in order to bring my position from long to short, ignoring the fact that i have a market order that would flat my position.
if this is the case, is there anything I can do in the managed approach with CalculateOnBarClose = true; to avoid this strategy to double up my position?
private IOrder entryOrder = null;
private int barNumberOfOrder = 0;
#endregion
protected override void Initialize()
{
EntryHandling = EntryHandling.UniqueEntries;
CalculateOnBarClose = true;
ExitOnClose = false;
TraceOrders = true;
}
protected override void OnBarUpdate()
{
//Enter long
if (entryOrder == null && CrossAbove(...))
{
entryOrder = EnterLongLimit(0, true, DefaultQuantity, Close[0] - 0.2 * (High[0] - Low[0]), "Long");
barNumberOfOrder = CurrentBar;
}
//Enter short
if (entryOrder == null && CrossBelow(...))
{
entryOrder = EnterShortLimit(0, true, DefaultQuantity, Close[0] + 0.2 * (High[0] - Low[0]),"Short");
barNumberOfOrder = CurrentBar;
}
//Cancel limit order if not done within lookback period
if(CurrentBar > barNumberOfOrder +1 && entryOrder != null)
{
CancelOrder(entryOrder);
}
//Exit long
if (Position.MarketPosition == MarketPosition.Long && CrossBelow(...))
{
ExitLong(0,DefaultQuantity, "Exit Long On Cross", "Long");
}
//Exit short
if (Position.MarketPosition == MarketPosition.Short && CrossAbove(...))
{
ExitShort(0,DefaultQuantity, "Exit Short On Cross", "Short");
}
}
protected override void OnOrderUpdate(IOrder order)
{
if (entryOrder != null && entryOrder == order)
{
if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
{
entryOrder = null;
}
}
}
protected override void OnExecution(IExecution execution)
{
if (entryOrder != null && entryOrder == execution.Order)
{
if (execution.Order.OrderState != OrderState.PartFilled)
{
entryOrder = null;
}
}
}

Comment