Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

[Backtesting] Wierd profit target/stop loss behavior

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

    [Backtesting] Wierd profit target/stop loss behavior

    Edit: This problem was a programming error of myself. Please see the third post for the solution if you have the same problem.



    Hi there,

    I came across something quite wierd when backtesting a strategy in NT 7.0, and spend a couple of hours in vain to find a solution. I'm hoping someone can help me out.

    I'm backtesting a strategy with 2 profit targets and a stoploss. In short: if the price closes above the Pivot PP level, enter long. Stoploss is placed 3 ticks under PP level. First target is R1, second target is R2. As you can see, nothing fancy and quite straightforward.

    If I backtest the code it works well (i.e. does what it's supposed to do), but there are two trades in the whole backtest period in which the long is exited at a non-existing price.

    Please see the attachment for a graphical representation.

    This is the output of TraceOrders:
    PHP Code:
    22-9-2009 9:30:00 Entered internal SetStopTarget() method: Type=Target FromEntrySignal='LongPP-a' Mode=Price Value=2942,10333333333 Currency=0 Simulated=False
    22-9-2009 9:30:00 Entered internal SetStopTarget() method: Type=Target FromEntrySignal='LongPP-b' Mode=Price Value=2990,87666666667 Currency=0 Simulated=False
    22-9-2009 9:30:00 Amended target order: Order='NT-00011/Backtest' Name='Profit target' State=Working Instrument='ESTX50 ##-##' Action=Sell Limit price=2990,88 Stop price=0 Quantity=1 Strategy='WeeklyPivot' Type=Limit Tif=Gtc Oco='NT-00005' Filled=0 Fill price=0 Token='29bb5c8ada1542e4bf43638964e33251' Gtd='1-12-2099 0:00:00'
    22-9-2009 9:30:00 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=2861,91666666667 Currency=0 Simulated=False
    22-9-2009 9:30:00 Amended stop order: Order='NT-00010/Backtest' Name='Stop loss' State=Working Instrument='ESTX50 ##-##' Action=Sell Limit price=0 Stop price=2861,92 Quantity=1 Strategy='WeeklyPivot' Type=Stop Tif=Gtc Oco='NT-00005' Filled=0 Fill price=0 Token='cffa0b3b3c4a4b689108fce96d7006b4' Gtd='1-12-2099 0:00:00'
    22-9-2009 9:30:00 Cancelled expired order: BarsInProgress=0: Order='NT-00008/Backtest' Name='Stop loss' State=Cancelled Instrument='ESTX50 ##-##' Action=Sell Limit price=0 Stop price=2852,56 Quantity=1 Strategy='WeeklyPivot' Type=Stop Tif=Gtc Oco='NT-00004' Filled=0 Fill price=0 Token='393c6bb2cb124ba8a5aafada040a877d' Gtd='1-12-2099 0:00:00' 
    
    The first two lines are correct: 2942.10 is the R1 level and 2990.87 is the R2 Pivot level. The position gets entered at around 2880, so that's okay.

    What I find wierd is the fourth line. In that line gets the profit target of R1 changed to just above the PP level, at which it gets sold, even though the price never goes there. Besides that, there is no reason to change the profit target to that value?

    I can't find where I did go wrong, because the other trades (except one other instance) in the backtest get all done correctly.


    Any thoughts would be highly appreciated,


    Regards,
    Attached Files
    Last edited by J_o_s; 04-23-2010, 02:08 AM. Reason: Solved

    #2
    Here's the code I used for the stoploss & profit target:

    // Variables
    private double stopLossPivot = 3000; // Default setting for Stoploss from PP signal

    protectedoverridevoid Initialize()
    {
    CalculateOnBarClose =
    true;

    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.UniqueEntries;

    TraceOrders =
    true;

    SetStopLoss(CalculationMode.Ticks, stopLossPivot);
    }

    protectedoverridevoid OnBarUpdate()
    {
    // Resets the stop loss to the original value when all positions are closed
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, stopLossPivot);
    }


    if( Position.MarketPosition == MarketPosition.Long)
    {
    // Stoploss = 3 points below the PP level
    SetStopLoss(CalculationMode.Price, Pivots(PivotRange.Weekly, HLCCalculationMode.CalcFromIntradayData, 200).PP[0] - (TickSize * 300));
    // Set profit target for the first part of the position.
    SetProfitTarget("LongPP-a", CalculationMode.Price, (Pivots(PivotRange.Weekly, HLCCalculationMode.CalcFromIntradayData, 200).R1[0]) - (TickSize * 200));
    // Set profit target for the second part of the position.
    SetProfitTarget("LongPP-b", CalculationMode.Price, (Pivots(PivotRange.Weekly, HLCCalculationMode.CalcFromIntradayData, 200).R2[0]) - (TickSize * 200));
    }

    Comment


      #3
      Whoops

      I made quite a stupid error (Duh!). Here's what I changed to solve this problem:

      Code:
       
      [FONT=Courier New][SIZE=1][COLOR=#0000ff][FONT=Courier New][SIZE=1][COLOR=#0000ff][FONT=Courier New][SIZE=1][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=1][FONT=Courier New][SIZE=1](Position.MarketPosition == MarketPosition.Flat)[/SIZE][/FONT]
      [SIZE=1][FONT=Courier New]{[/FONT][/SIZE]
      [SIZE=1][FONT=Courier New]EnterLong([/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=#800000][FONT=Courier New][SIZE=1][COLOR=#800000][FONT=Courier New][SIZE=1][COLOR=#800000]"LongPP-a"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=1][FONT=Courier New][SIZE=1]);[/SIZE][/FONT]
      [SIZE=1][FONT=Courier New]EnterLong([/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=#800000][FONT=Courier New][SIZE=1][COLOR=#800000][FONT=Courier New][SIZE=1][COLOR=#800000]"LongPP-b"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=1][FONT=Courier New][SIZE=1]);[/SIZE][/FONT]
      [/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=#008000][FONT=Courier New][SIZE=1][COLOR=#008000][FONT=Courier New][SIZE=1][COLOR=#008000]// Profit target first part of the position.[/COLOR][/SIZE][/FONT]
      [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=1][FONT=Courier New][SIZE=1]SetProfitTarget([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=#800000][FONT=Courier New][SIZE=1][COLOR=#800000][FONT=Courier New][SIZE=1][COLOR=#800000]"LongPP-a"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=1][FONT=Courier New][SIZE=1], CalculationMode.Price, (Pivots(PivotRange.Weekly, HLCCalculationMode.CalcFromIntradayData, [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=#800080][FONT=Courier New][SIZE=1][COLOR=#800080][FONT=Courier New][SIZE=1][COLOR=#800080]200[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=1][FONT=Courier New][SIZE=1]).R1[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=#800080][FONT=Courier New][SIZE=1][COLOR=#800080][FONT=Courier New][SIZE=1][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=1][FONT=Courier New][SIZE=1]]) - (TickSize * [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=#800080][FONT=Courier New][SIZE=1][COLOR=#800080][FONT=Courier New][SIZE=1][COLOR=#800080]200[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=1][FONT=Courier New][SIZE=1]));[/SIZE][/FONT]
      [/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=#008000][FONT=Courier New][SIZE=1][COLOR=#008000][FONT=Courier New][SIZE=1][COLOR=#008000]// Profit target second part of the position.[/COLOR][/SIZE][/FONT]
      [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=1][FONT=Courier New][SIZE=1]SetProfitTarget([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=#800000][FONT=Courier New][SIZE=1][COLOR=#800000][FONT=Courier New][SIZE=1][COLOR=#800000]"LongPP-b"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=1][FONT=Courier New][SIZE=1], CalculationMode.Price, (Pivots(PivotRange.Weekly, HLCCalculationMode.CalcFromIntradayData, [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=#800080][FONT=Courier New][SIZE=1][COLOR=#800080][FONT=Courier New][SIZE=1][COLOR=#800080]200[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=1][FONT=Courier New][SIZE=1]).R2[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=#800080][FONT=Courier New][SIZE=1][COLOR=#800080][FONT=Courier New][SIZE=1][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=1][FONT=Courier New][SIZE=1]]) - (TickSize * [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=1][COLOR=#800080][FONT=Courier New][SIZE=1][COLOR=#800080][FONT=Courier New][SIZE=1][COLOR=#800080]200[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=1][FONT=Courier New][SIZE=1]));[/SIZE][/FONT]
      [SIZE=1][FONT=Courier New]}[/FONT][/SIZE]
      [/SIZE][/FONT]
      So, the profit targets get submitted only once (when the entry conditions are true), and not on a continuous basis whenever there is a long position. By using the exact same code in a different part of the backtest code this 'problem' was solved.

      I'm sorry for the multiple posts in this topic, but well, let's hope it prevents someone else from making the same error.

      Regards,

      Comment


        #4
        Great you have it resolved and thanks for posting your solution here.

        Comment


          #5
          Changing Profit Target

          So there is no way to change a profit target OnBarUpdate after the entry condition is set?

          Thanks
          RB

          Comment


            #6
            rbcom, sorry I do not follow you - you could just call SetProfitTarget again to reset the target price to a new value in the OnBarUpdate().

            Comment


              #7
              when I call SetProfitTarget in OnBarUpdate() it keeps using the very first value and does not recalculate for every bar

              Comment


                #8
                rbcom,

                Please post a code snippet of how you are doing so with the SetProfitTarget(). Thank you.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Change Profit Target

                  Here's an example of what I'm trying to do:

                  protected override void OnBarUpdate()
                  {
                  // Condition set 1
                  if (my condition is here)
                  {
                  EnterShortLimit(DefaultQuantity, ShortEntryPrice, "DCS");
                  }

                  // Condition set 2
                  if (Position.MarketPosition == MarketPosition.Short)
                  {
                  SetProfitTarget("DCS", CalculationMode.Price, ShortExitPrice);
                  }
                  }

                  Comment


                    #10
                    rbcom, thanks, did you print out your ShortExitPrice to check if it's updating the stoplevel on each bar as you expect?

                    What is the TraceOrders output showing?

                    Comment


                      #11
                      Does TraceOrders work when I'm just trying to Backtest?
                      How can I establish a different profit target for each order that is submitted using a Ninjascript strategy?

                      I'd like to have the same stop loss OCO, but a different profit target for each order.

                      Thank you

                      Comment


                        #12
                        Yes, the TraceOrders would work in a backtest as well offering insight into the order behavior 'under the hood'.

                        If you to submit an explicit target order each time, just use the ExitLongLimit / ExitShortLimit methods for example.

                        However those are not OCO'ed then by default, as only the Set() methods would offer this feature.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        607 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        353 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        105 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        560 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        561 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X