Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

TakeProfit on same bar not triggering in backtest

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

    TakeProfit on same bar not triggering in backtest

    Hi,

    I am running a strategy in the builder and when I backtest it I noticed that some of the profit targets where not triggering properly.

    Specifically, it seems that when the profit target is met in the entry bar, the trade is captured as a breakeven trade. However, if the profit target is met in subsequent bars, then the profit target is captured as a profitable trade. The screenshots show this.

    My profit target and trail stop targets are both about 10 points. I have attached a screenshot of the relevant code as well. I would like the trade to be captured as a profitable trade even if the profit target is met in the same bar.

    I have searched the forum but could not find a similar query so apologies if this has been asked and answered elsewhere.

    Thank you!




    Attached Files

    #2
    Hello nagaselv,

    I'm not sure I am understanding. You have trail orders being filled at a loss and not profit targets.

    Are you asking how to not place the trail stop so that the position is not exited until the profit target is hit?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      Maybe I am missing something. Perhaps you can help me understand my backtest summary.

      I have attached the screenshot of the summary of one of the backtests I have run.

      Here, I have set the take profit to 41 ticks and trail stop to be 39 ticks which is basically 10 points on the NQ. As you can see, there are 21 winning trades, 556 losing trades and 335 even trades.

      If the stop loss is working right, then do these 335 even trades mean that after I entered, the price went up to 39 ticks in my favor (thus moving the trailstop up to breakeven) and then reversed back to stop out? I am sure some of these trades were like that but all 335?

      I have attached a screenshot of a sampling of the trades taken and you can see the trail stop loss is working when the position ends in a loss. For example, the first trade at the top of the screenshot is a loss of -$135 which means that the trade presumably went in my favour for about 3 points before reversing back down and stopping me out at -7 or so points.

      I have also attached a screenshot of one such trade that has been marked as a breakeven one where the profit target hit but is marked as breakeven.

      You can see that the entry price which was a short is on open which is 15007.25 and the low of this bar is 14992.5 and the high is 15012. This short should have yielded the profit target of 41 ticks but is marked as breakeven.

      I hope this clarifies the issue I am having and I hope you can help me understand if I am misunderstanding something crucial.

      Thank you!




      Attached Files

      Comment


        #4
        Hi Chelsea,

        I have found out a bit more about the behavior that I describe above in the backtesting.

        If after entry, the same entry bar contains the profit target, the trade will be registered as a breakeven trade.

        Thus, the following seem to be true (see the attached screenshots for examples):
        a) If the entry bar contains the profit target and the stop loss target, the trade will be registered as a breakeven trade.
        b) If the entry bar does not contain the profit target but contains the stop loss, the stop loss is accurately registered.
        c) If the entry bar contains the profit target but not the stop loss target, the trade will be registered as a breakeven trade.


        How do I set up my backtest so that the historical tick data is correctly registered in my trades? So regardless of whether the entry bar contains the profit target and stop loss or not, I would like the trade to be correctly registered in accordance with historical data. I am on a demo account, could this be why historical tick data is not being correctly reflected? I have already set the calculation mode to be on each tick.

        Hope I have characterized the issue I am facing clearly enough for you to be able to help me.

        Thank you!

        Attached Files

        Comment


          #5
          Hello nagaselv,

          In backtest, if 1-tick intra-bar granularity is not implemented orders will fill if they are between the high and the low.

          Below is a link to the help guide on the historical fill algorithm.



          And a link to a forum post on 1-tick intra-bar granularity for historical order fill accuracy.



          As far as what happened with your trades, it would necessary to investigate and I would want offer guesses.

          You can print the values of all conditions to see when the conditions are evaluating as true.
          You can print the order object in OnOrderUpdate() to see when orders are being submitted, changed, and filled. (So you can see when the trailing happened and when the price changed and understand why the order filled at the price it did)
          You can enable TraceOrders to see when orders are being submitted, ignored, rejected, or automatically cancelled.

          Below is a link to a forum post that demonstrates using Print() and TraceOrders to understand behavior.



          To "set up my backtest so that the historical tick data is correctly registered in my trades" add a 1 tick series, place the orders to the BarsInProgress of the added 1 tick series.
          The forum post on 1-tick granularity has a link to the reference sample which demonstrates.
          Chelsea B.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          58 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          133 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          73 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          45 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          50 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X