The trade never triggered due to NT creating duplicated order? Below is the error
2016-06-02 06:29:58:437 (IB TWS) Cbi.Account.CreateOrder: orderId='ae7ad48a171448f4bcbe4446bdbb2e7d' account='U122122' name='StrategyA' orderState=Initialized instrument='NQ 06-16' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=2502 gtd='2099-12-01'
2016-06-02 06:29:58:439 (IB TWS) Cbi.Account.Submit0: orderId='ae7ad48a171448f4bcbe4446bdbb2e7d' account='U122122' name='StrategyA' orderState=Initialized instrument='NQ 06-16' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=2502 gtd='2099-12-01' currentOrderState=Initialized
2016-06-02 06:29:58:440 (IB TWS) Cbi.Account.Submit1: orderId='ae7ad48a171448f4bcbe4446bdbb2e7d' account='U122122' name='StrategyA' orderState=Initialized instrument='NQ 06-16' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=2502 gtd='2099-12-01' currentOrderState=Initialized
2016-06-02 06:29:58:440 (IB TWS) IB.Adapter.Submit0: count=1
2016-06-02 06:29:58:440 (IB TWS) IB.Adapter.Submit: orderId='ae7ad48a171448f4bcbe4446bdbb2e7d' account='U122122' name='StrategyA' orderState=Initialized instrument='NQ 06-16' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=2502 gtd='2099-12-01'
2016-06-02 06:29:58:442 (IB TWS) IB.Adapter.OnError: id=12601 errorCode='DuplicateOrderID' errorMessage='Duplicate order id'
2016-06-02 06:29:58:446 ERROR: Failed to submit orders: System.ArgumentException: An item with the same key has already been added. at System.ThrowHelper.ThrowArgumentException(Exceptio nResource resource) at System.Collections.Generic.Dictionary`2.Insert(TKe y key, TValue value, Boolean add) at NinjaTrader.IB.Adapter.Submit(Order[] orders) at NinjaTrader.Cbi.Account.<>c__DisplayClass254_3.<Su bmit>b__4()
I am thinking the duplicate order might be related to the mutitreading of NT 8 in where the entry order, exit order and target order happened on different threads, thsus causing the issue? If indeed this is the case,, is there other conditions I need to incorporate in the code below such that this does not happen again?
Below is the sample code used for this strategy.
protected override void OnBarUpdate()
{
if (State == State.Historical)
return;
if (entryOrder == null && ToTime(Time[0]) >= startTime)
{
if (GetCurrentBid() > longMaxEntry
&& GetCurrentBid() < longMinEntry
&& longEnabled == true)
// Go Long
entryOrder = EnterLong (1,Name);
else
if (GetCurrentAsk() < ShortMaxEntry
&& GetCurrentAsk() > ShortMinEntry
&& shortEnabled == true)
// Go Short
entryOrder = EnterShort (1,Name);
else
{
SetState(State.Terminated);
Print("Condition not met. Disabling " + Name + " Strategy" + " " + State + " " + ToTime(Time[0]));
}
}
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{
if (entryOrder != null
&& entryOrder == execution.Order
&& (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled))
{
entryOrder = execution.Order;
if (execution.Order.IsLong)
{
Print ("Strategy " + Name + " is Long. Entry Price @" + entryOrder.AverageFillPrice + " " + ToTime(Time[0]));
if (stopOrder == null)
{
stopOrder = ExitLongStopMarket(0, true, 1, entryOrder.AverageFillPrice - (stopLossTicks*Bars.Instrument.MasterInstrument.Ti ckSize), "MyStop", Name);
//stopOrder = ExitLongStopMarket(0, true, 1, entryOrder.AverageFillPrice - .05, "MyStop", Name);
Print ("Strategy " + Name + " Stop Order Price @" + stopOrder.StopPrice + " " + ToTime(Time[0]));
}
if(targetOrder == null)
{
targetOrder = ExitLongLimit(0, true, execution.Order.Filled, profitTargetPrice, "MyTarget", Name);
//targetOrder = ExitLongLimit(0, true, execution.Order.Filled, entryOrder.AverageFillPrice +.01, "MyTarget", Name);
Print ("Strategy " + Name + " Profit Target Price @" + targetOrder.LimitPrice + " " + ToTime(Time[0]));
}
}
Comment