Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Changing SetStopLoss Messes Up Backtesting

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Changing SetStopLoss Messes Up Backtesting

    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...

    Code:
    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:

    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:

    Code:
    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
    Attached Files

    #2
    Hi KBJ,

    I am looking into this for you and will report back when I have some answers.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thanks. This is 100% reproduceable, but please let me know if you need any more data from me.

      Comment


        #4
        Here's a little more data... (see attachment).

        You can see how the graphing of the trades from a recent backtest displays the stop loss values in a location that completely doesn't make sense.

        KBJ
        Attached Files
        Last edited by KBJ; 10-22-2007, 10:51 PM.

        Comment


          #5
          Hi KBJ,

          The orders themselves are performing as expected. What you are experiencing is a issue with the actual fills themselves. From what I can see you are saying that the stop loss shouldn't be able to fill at the set price correct?

          Please try this: http://www.ninjatrader-support.com/v...4&postcount=28

          Let me know if that fixes the issue or not. If it doesn't work out I will need to think some more.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            Josh,

            Your modified @DefaultFillType.cs file solves the problem.

            Thanks.

            Best regards,

            KBJ

            Comment


              #7
              Great. Thanks for reporting back.
              Josh P.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by JuanPabloQuevedo, 10-25-2020, 05:27 PM
              2 responses
              355 views
              0 likes
              Last Post nuobo
              by nuobo
               
              Started by SugarAuBlood, Today, 04:40 AM
              0 responses
              5 views
              0 likes
              Last Post SugarAuBlood  
              Started by gwenael, 04-27-2024, 09:29 AM
              2 responses
              27 views
              0 likes
              Last Post gwenael
              by gwenael
               
              Started by thread, 04-15-2024, 11:58 PM
              2 responses
              35 views
              0 likes
              Last Post AunkerSlave  
              Started by giulyko00, 04-24-2024, 12:03 PM
              4 responses
              25 views
              0 likes
              Last Post giulyko00  
              Working...
              X