KBJ
10-22-2007, 12:13 PM
I've written a strategy which has run into a bug in the way NinjaTrader handles SetStopLoss during backtesting.
The strategy first sets up trades on three contracts...
Stop_loss_1 = 16;
Stop_loss_2 = 17;
Stop_loss_3 = 17;
SetProfitTarget(long_id_1, CalculationMode.Ticks, Profit_target_1);
SetStopLoss(long_id_1, CalculationMode.Ticks, Stop_loss_1, false);
EnterLong(1, long_id_1);
SetProfitTarget(long_id_2, CalculationMode.Ticks, Profit_target_2);
SetStopLoss(long_id_2, CalculationMode.Ticks, Stop_loss_2, false);
EnterLong(1, long_id_2);
SetProfitTarget(long_id_3, CalculationMode.Ticks, Profit_target_3);
SetStopLoss(long_id_3, CalculationMode.Ticks, Stop_loss_3, false);
EnterLong(1, long_id_3);
Then, sometime later, the price drops sufficiently to stop out the first contract. The strategy then adjusts the stops on the other two contracts using this code:
if (Position.Quantity==2) // Only only 2 contracts left?
{ // Yes...
// Move stop up to breakeven+commission.
SetStopLoss(long_id_2, CalculationMode.Price, Position.AvgPrice+TickSize, false);
SetStopLoss(long_id_3, CalculationMode.Price, Position.AvgPrice+TickSize*2, false);
}The problem is that these last two contracts get stopped out at prices that lose only 1 and 0 ticks. This is not right, because at that point in the backtest, the market has already moved 16 ticks against us, so each of these last two contracts should lose about 16 ticks each. Instead, the StrategyAnalyzer stops them out with little or no loss.
Apparently NinjaTrader 6.0.1000.5 has a problem with changing a stop loss to a less-losing position when market prices should preclude that operation.
Here's the data from the Output window that was created by the "TraceOrders=true" statement:
10/15/2007 4:20:05 AM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='Long_1/1' Mode=Ticks Value=8 Currency=0 Simulated=False
10/15/2007 4:20:05 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Long_1/1' Mode=Ticks Value=22 Currency=0 Simulated=False
10/15/2007 4:20:05 AM Entered internal PlaceOrder() method: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Long_1/1' FromEntrySignal=''
10/15/2007 4:20:05 AM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='Long_2/2' Mode=Ticks Value=8 Currency=0 Simulated=False
10/15/2007 4:20:05 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Long_2/2' Mode=Ticks Value=23 Currency=0 Simulated=False
10/15/2007 4:20:05 AM Entered internal PlaceOrder() method: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Long_2/2' FromEntrySignal=''
10/15/2007 4:20:05 AM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='Long_3/3' Mode=Ticks Value=8 Currency=0 Simulated=False
10/15/2007 4:20:05 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Long_3/3' Mode=Ticks Value=23 Currency=0 Simulated=False
10/15/2007 4:20:05 AM Entered internal PlaceOrder() method: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Long_3/3' FromEntrySignal=''
10/15/2007 9:55:16 AM Cancelled pending exit order, since associated position is closed: Order='NT-00004/Back101' Name='Profit target' State=Initialized Instrument='ES 12-07' Action=Sell Limit price=1576.25 Stop price=0 Quantity=1 Strategy='DSB5' Type=Limit Tif=Day Oco='NT-00000' Filled=0 Fill price=0 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:16 AM Cancelled pending exit order, since associated position is closed: Order='NT-00003/Back101' Name='Stop loss' State=Filled Instrument='ES 12-07' Action=Sell Limit price=0 Stop price=1568.75 Quantity=1 Strategy='DSB5' Type=Stop Tif=Day Oco='NT-00000' Filled=1 Fill price=1568.75 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:16 AM Cancelled OCO paired order: BarsInProgress=0: Order='NT-00004/Back101' Name='Profit target' State=Cancelled Instrument='ES 12-07' Action=Sell Limit price=1576.25 Stop price=0 Quantity=1 Strategy='DSB5' Type=Limit Tif=Day Oco='NT-00000' Filled=0 Fill price=0 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:24 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Long_2/2' Mode=Price Value=1574.5 Currency=0 Simulated=False
10/15/2007 9:55:24 AM Amended stop order: Order='NT-00005/Back101' Name='Stop loss' State=Initialized Instrument='ES 12-07' Action=Sell Limit price=0 Stop price=1574.5 Quantity=1 Strategy='DSB5' Type=Stop Tif=Day Oco='NT-00001' Filled=0 Fill price=0 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:24 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Short2' Mode=Price Value=1574 Currency=0 Simulated=False
10/15/2007 9:55:24 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Long_3/3' Mode=Price Value=1574.75 Currency=0 Simulated=False
10/15/2007 9:55:24 AM Amended stop order: Order='NT-00007/Back101' Name='Stop loss' State=Initialized Instrument='ES 12-07' Action=Sell Limit price=0 Stop price=1574.75 Quantity=1 Strategy='DSB5' Type=Stop Tif=Day Oco='NT-00002' Filled=0 Fill price=0 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:24 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Short3' Mode=Price Value=1573.75 Currency=0 Simulated=False
10/15/2007 9:55:24 AM Cancelled pending exit order, since associated position is closed: Order='NT-00006/Back101' Name='Profit target' State=Initialized Instrument='ES 12-07' Action=Sell Limit price=1576.25 Stop price=0 Quantity=1 Strategy='DSB5' Type=Limit Tif=Day Oco='NT-00001' Filled=0 Fill price=0 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:24 AM Cancelled pending exit order, since associated position is closed: Order='NT-00005/Back101' Name='Stop loss' State=Filled Instrument='ES 12-07' Action=Sell Limit price=0 Stop price=1574.5 Quantity=1 Strategy='DSB5' Type=Stop Tif=Day Oco='NT-00001' Filled=1 Fill price=1574 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:24 AM Cancelled OCO paired order: BarsInProgress=0: Order='NT-00006/Back101' Name='Profit target' State=Cancelled Instrument='ES 12-07' Action=Sell Limit price=1576.25 Stop price=0 Quantity=1 Strategy='DSB5' Type=Limit Tif=Day Oco='NT-00001' Filled=0 Fill price=0 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:24 AM Cancelled pending exit order, since associated position is closed: Order='NT-00008/Back101' Name='Profit target' State=Initialized Instrument='ES 12-07' Action=Sell Limit price=1576.25 Stop price=0 Quantity=1 Strategy='DSB5' Type=Limit Tif=Day Oco='NT-00002' Filled=0 Fill price=0 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:24 AM Cancelled pending exit order, since associated position is closed: Order='NT-00007/Back101' Name='Stop loss' State=Filled Instrument='ES 12-07' Action=Sell Limit price=0 Stop price=1574.75 Quantity=1 Strategy='DSB5' Type=Stop Tif=Day Oco='NT-00002' Filled=1 Fill price=1574.25 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:24 AM Cancelled OCO paired order: BarsInProgress=0: Order='NT-00008/Back101' Name='Profit target' State=Cancelled Instrument='ES 12-07' Action=Sell Limit price=1576.25 Stop price=0 Quantity=1 Strategy='DSB5' Type=Limit Tif=Day Oco='NT-00002' Filled=0 Fill price=0 Token='' Gtd='12/1/2099 12:00:00 AM'
There may also be a problem related to the timing of the entries in the above output, as compared to the entries listed on the "Trades" screen and on the Chart (see attachments.) There are several entries at 9:55:16 and then a lot more occur 8 seconds later at 9:55:24 in the TradeOrders output. Shouldn't all of these entries be occurring at the same time?
KBJ
The strategy first sets up trades on three contracts...
Stop_loss_1 = 16;
Stop_loss_2 = 17;
Stop_loss_3 = 17;
SetProfitTarget(long_id_1, CalculationMode.Ticks, Profit_target_1);
SetStopLoss(long_id_1, CalculationMode.Ticks, Stop_loss_1, false);
EnterLong(1, long_id_1);
SetProfitTarget(long_id_2, CalculationMode.Ticks, Profit_target_2);
SetStopLoss(long_id_2, CalculationMode.Ticks, Stop_loss_2, false);
EnterLong(1, long_id_2);
SetProfitTarget(long_id_3, CalculationMode.Ticks, Profit_target_3);
SetStopLoss(long_id_3, CalculationMode.Ticks, Stop_loss_3, false);
EnterLong(1, long_id_3);
Then, sometime later, the price drops sufficiently to stop out the first contract. The strategy then adjusts the stops on the other two contracts using this code:
if (Position.Quantity==2) // Only only 2 contracts left?
{ // Yes...
// Move stop up to breakeven+commission.
SetStopLoss(long_id_2, CalculationMode.Price, Position.AvgPrice+TickSize, false);
SetStopLoss(long_id_3, CalculationMode.Price, Position.AvgPrice+TickSize*2, false);
}The problem is that these last two contracts get stopped out at prices that lose only 1 and 0 ticks. This is not right, because at that point in the backtest, the market has already moved 16 ticks against us, so each of these last two contracts should lose about 16 ticks each. Instead, the StrategyAnalyzer stops them out with little or no loss.
Apparently NinjaTrader 6.0.1000.5 has a problem with changing a stop loss to a less-losing position when market prices should preclude that operation.
Here's the data from the Output window that was created by the "TraceOrders=true" statement:
10/15/2007 4:20:05 AM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='Long_1/1' Mode=Ticks Value=8 Currency=0 Simulated=False
10/15/2007 4:20:05 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Long_1/1' Mode=Ticks Value=22 Currency=0 Simulated=False
10/15/2007 4:20:05 AM Entered internal PlaceOrder() method: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Long_1/1' FromEntrySignal=''
10/15/2007 4:20:05 AM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='Long_2/2' Mode=Ticks Value=8 Currency=0 Simulated=False
10/15/2007 4:20:05 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Long_2/2' Mode=Ticks Value=23 Currency=0 Simulated=False
10/15/2007 4:20:05 AM Entered internal PlaceOrder() method: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Long_2/2' FromEntrySignal=''
10/15/2007 4:20:05 AM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='Long_3/3' Mode=Ticks Value=8 Currency=0 Simulated=False
10/15/2007 4:20:05 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Long_3/3' Mode=Ticks Value=23 Currency=0 Simulated=False
10/15/2007 4:20:05 AM Entered internal PlaceOrder() method: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Long_3/3' FromEntrySignal=''
10/15/2007 9:55:16 AM Cancelled pending exit order, since associated position is closed: Order='NT-00004/Back101' Name='Profit target' State=Initialized Instrument='ES 12-07' Action=Sell Limit price=1576.25 Stop price=0 Quantity=1 Strategy='DSB5' Type=Limit Tif=Day Oco='NT-00000' Filled=0 Fill price=0 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:16 AM Cancelled pending exit order, since associated position is closed: Order='NT-00003/Back101' Name='Stop loss' State=Filled Instrument='ES 12-07' Action=Sell Limit price=0 Stop price=1568.75 Quantity=1 Strategy='DSB5' Type=Stop Tif=Day Oco='NT-00000' Filled=1 Fill price=1568.75 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:16 AM Cancelled OCO paired order: BarsInProgress=0: Order='NT-00004/Back101' Name='Profit target' State=Cancelled Instrument='ES 12-07' Action=Sell Limit price=1576.25 Stop price=0 Quantity=1 Strategy='DSB5' Type=Limit Tif=Day Oco='NT-00000' Filled=0 Fill price=0 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:24 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Long_2/2' Mode=Price Value=1574.5 Currency=0 Simulated=False
10/15/2007 9:55:24 AM Amended stop order: Order='NT-00005/Back101' Name='Stop loss' State=Initialized Instrument='ES 12-07' Action=Sell Limit price=0 Stop price=1574.5 Quantity=1 Strategy='DSB5' Type=Stop Tif=Day Oco='NT-00001' Filled=0 Fill price=0 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:24 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Short2' Mode=Price Value=1574 Currency=0 Simulated=False
10/15/2007 9:55:24 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Long_3/3' Mode=Price Value=1574.75 Currency=0 Simulated=False
10/15/2007 9:55:24 AM Amended stop order: Order='NT-00007/Back101' Name='Stop loss' State=Initialized Instrument='ES 12-07' Action=Sell Limit price=0 Stop price=1574.75 Quantity=1 Strategy='DSB5' Type=Stop Tif=Day Oco='NT-00002' Filled=0 Fill price=0 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:24 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Short3' Mode=Price Value=1573.75 Currency=0 Simulated=False
10/15/2007 9:55:24 AM Cancelled pending exit order, since associated position is closed: Order='NT-00006/Back101' Name='Profit target' State=Initialized Instrument='ES 12-07' Action=Sell Limit price=1576.25 Stop price=0 Quantity=1 Strategy='DSB5' Type=Limit Tif=Day Oco='NT-00001' Filled=0 Fill price=0 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:24 AM Cancelled pending exit order, since associated position is closed: Order='NT-00005/Back101' Name='Stop loss' State=Filled Instrument='ES 12-07' Action=Sell Limit price=0 Stop price=1574.5 Quantity=1 Strategy='DSB5' Type=Stop Tif=Day Oco='NT-00001' Filled=1 Fill price=1574 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:24 AM Cancelled OCO paired order: BarsInProgress=0: Order='NT-00006/Back101' Name='Profit target' State=Cancelled Instrument='ES 12-07' Action=Sell Limit price=1576.25 Stop price=0 Quantity=1 Strategy='DSB5' Type=Limit Tif=Day Oco='NT-00001' Filled=0 Fill price=0 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:24 AM Cancelled pending exit order, since associated position is closed: Order='NT-00008/Back101' Name='Profit target' State=Initialized Instrument='ES 12-07' Action=Sell Limit price=1576.25 Stop price=0 Quantity=1 Strategy='DSB5' Type=Limit Tif=Day Oco='NT-00002' Filled=0 Fill price=0 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:24 AM Cancelled pending exit order, since associated position is closed: Order='NT-00007/Back101' Name='Stop loss' State=Filled Instrument='ES 12-07' Action=Sell Limit price=0 Stop price=1574.75 Quantity=1 Strategy='DSB5' Type=Stop Tif=Day Oco='NT-00002' Filled=1 Fill price=1574.25 Token='' Gtd='12/1/2099 12:00:00 AM'
10/15/2007 9:55:24 AM Cancelled OCO paired order: BarsInProgress=0: Order='NT-00008/Back101' Name='Profit target' State=Cancelled Instrument='ES 12-07' Action=Sell Limit price=1576.25 Stop price=0 Quantity=1 Strategy='DSB5' Type=Limit Tif=Day Oco='NT-00002' Filled=0 Fill price=0 Token='' Gtd='12/1/2099 12:00:00 AM'
There may also be a problem related to the timing of the entries in the above output, as compared to the entries listed on the "Trades" screen and on the Chart (see attachments.) There are several entries at 9:55:16 and then a lot more occur 8 seconds later at 9:55:24 in the TradeOrders output. Shouldn't all of these entries be occurring at the same time?
KBJ