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

Handling of Stop Loss and Take Profit Levels in Backtesting

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

    Handling of Stop Loss and Take Profit Levels in Backtesting

    Hello,

    I have a question about how NinjaTrader handles stop loss (SL) and take profit (TP) levels in backtesting.

    Consider a scenario where I have an entry order, and in the same bar, both the SL and TP levels would be hit. I understand that with OHLC bar data, we can't know for certain the exact sequence of intra-bar price movements. How does NinjaTrader handle this situation in backtesting?

    For example, let's say I have a 5-minute bar where:
    • The open price is 100
    • The high price is 105
    • The low price is 95
    • The close price is 103

    And I place a long entry order at 100 with a SL at 98 and a TP at 102. Both the SL and TP levels would be hit within this bar. How does NinjaTrader determine whether the trade would be stopped out or reach the TP level first?

    Any clarification on this matter would be greatly appreciated.

    Best Regards,
    Aviram Y.


    #2
    Hello Aviram,

    Thanks for your post.

    Please review the help guide document on the differences on real-time vs backtest (historical).
    http://ninjatrader.com/support/helpG...ime_vs_bac.htm

    When in historical data (backtesting), only the Open, High, Low, and Close will be available and there will be no intra-bar data. This means actions cannot happen intra-bar, fills cannot happen intra-bar. All prices and actions come from and occur when the bar closes as this is all the information that is known.

    Because of this, OnBarUpdate will only update 'On bar close' as it does not have the intra-bar information necessary for 'On price change' or 'On each tick' and the script will not have the intra-bar information to accurately fill an order at the exact price and time.

    Below is a link to the help guide on Calculate.
    https://ninjatrader.com/support/help.../calculate.htm

    To improve the accuracy of the backtest, you may use Tick Replay along with an added 1-tick series to have logic processed intra-bar and have orders filled intrabar.

    Tick Replay would be used to have the logic process OnEachTick or OnPriceChange with historical data, but this does not allow for intra-bar order fills. You would need to add a single tick data series and submit orders to that single-tick data series for a strategy that uses Tick Replay.

    High Order Fill Resolution allows for intra-bar order fills with historical processing, but is not compatible with Tick Replay.

    Please reference the SampleIntrabarBacktest example and the following Help Guide links for more information.

    SampleIntrabarBacktest 'Backtesting NinjaScript Strategies with an intrabar granularity' - https://ninjatrader.com/support/helpGuides/nt8/backtesting_ninjascript_strate.htm

    TickReplay — https://ninjatrader.com/support/help...ick_replay.htm

    Developing for Tick Replay -
    https://ninjatrader.com/support/helpGuides/nt8/developing_for__tick_replay.htm?zoom_highlightsub= developing+for+tick+replay

    Additional information may be found in this NinjaTrader Forum post —
    https://ninjatrader.com/support/forum/forum/ninjatrader-8/strategy-development/100192-comparing-real-time-historical-and-replay-performance?t=102504
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hi Brandon, thanks for the response.
      all this is great and i already have a lot of experience with the platform, but you did not answer my question.
      I'll rephrase, specifically when backtesting with standard fill resolution, lets look at a 5 minute bar series, where its 10:00 and I've just entered a long position in the market at on bar close, with a working stop loss and profit target exit orders.
      The bar is as follows:
      • The open price is 100
      • The high price is 105
      • The low price is 95
      • The close price is 103
      The stop loss is at : 98
      The profit target is at: 104


      When the bar closes as you can see from the data, the stop loss and profit target orders both would have been filled in this case,
      and because there is no intra-bar data, we do not know which one would be the first.

      How does Ninjatrader handle this situation?
      Is it the worst case scenario, where the stop loss is always filled when a bar reaches both the stop loss and profit target prices on the same bar?
      Is it the profit target?
      Is it something else entirely?


      Best Regards and thanks again,

      Aviram Y.


      Comment


        #4
        Hello Aviram,

        Thanks for your notes.

        The bar wouldn't be able to close above the target and below the stop simultaneously. The market would have to cross one of those prices to generate a fill.

        The Standard order fill resolution uses an algorithm to break each historical bar into three virtual bars to mimic the movement of price within each bar's timeframe. The virtual bars are created based on the proximity of the Open price to the High and Low prices. This provides more realistic intra-bar fills compared to traditional backtesting algorithms which only use static OHLC values.

        See the Understanding Historical Fill Processing help guide linked below for more information about how NinjaTrader uses advanced historical fill processing methods and techniques to get the most realistic results possible on historical backtests.

        Understanding Historical Fill Processing: https://ninjatrader.com/support/help...ical_fill_.htm

        And, see this help guide page for more information about Historical Order Backfill Logic: https://ninjatrader.com/support/help...fill_logic.htm
        Last edited by NinjaTrader_BrandonH; 07-31-2023, 09:26 AM.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Thank you! this is exactly what i was looking for!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by patrickmlee007, Today, 09:33 AM
          2 responses
          15 views
          0 likes
          Last Post patrickmlee007  
          Started by magnatauren, 08-15-2020, 02:12 PM
          5 responses
          206 views
          0 likes
          Last Post RaddiFX
          by RaddiFX
           
          Started by rene69851, 05-02-2024, 03:25 PM
          1 response
          21 views
          0 likes
          Last Post rene69851  
          Started by ETFVoyageur, Yesterday, 07:05 PM
          5 responses
          45 views
          0 likes
          Last Post ETFVoyageur  
          Started by jpeep, 08-16-2020, 08:31 AM
          13 responses
          487 views
          0 likes
          Last Post notenufftime  
          Working...
          X