Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trailing stopping-out logic inside a Strategy not working in backtests

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

    Trailing stopping-out logic inside a Strategy not working in backtests

    Hello everyone.

    I have created a Strategy based on the well-known TrailBuilderExample logic, for stopping-out of a trade that turns to losing money.
    The default trail distance is 5 ticks from the trail trigger.

    When I run this Strategy as (State==State.RealTime) on SIM101 , trailing seems to work and in case of a losing trade I am usually stopping-out around 5-6-7 ticks away from the trail trigger, as it should be.
    When I backtest the same Strategy as (State==State.Historical) , trailing fails to work, and the whole strategy turns red because of huge trailing stop losses. I can see in the Strategy Analyzer that many trades stopped-out after 60-70 ticks (!) in the losing territory although the setting was only 5 ticks !

    It seems as the losing trades were executed as completely unprotected. That is impossible to happen in real since the trailing logic is there to prevent exactly that from happening.
    If it doesn’t stop-out at exactly 5 ticks, it should do that at 6, or even 7 ticks, but not at 70 ticks !

    I can understand that running a backtest is not exactly like actually running the Strategy, and there are many simplified assumptions simulating the market movements, but on the other side not “listening” the trailing logic at all, is indeed problematic.
    This would greatly reduce the very usefulness of backtest as an evaluation tool.

    Maybe I am doing something wrong. Maybe some of my settings are not appropriate and need some adjustments.
    I would like to ask you for your advice. Has anybody noticed a similar case, where your trailing stop logic is not actually or properly working in backtests ?

    Last edited by KonstantinosNT; 12-31-2022, 02:33 AM.

    #2
    Hello KonstantionosNT,

    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

    A strategy running real-time (live brokerage account, live market simulation, Playback connection etc...) will produce different results than the performance results generated during a backtest.

    When in historical data, 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 a 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/foru...mance?t=102504

    Please let us know if we may be of further assistance to you.​
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hi Brandon and thanks for your reply.
      First I would like to wish "Happy New Year 2023" to you, and to all personnel of NinjaTrader, and to all members of the Forums.

      On the subject now:

      My question is not about the accuracy of the fills in Backtest, as I can understand the lack of granularity inside the historical bars.

      My question is specifically about the complete lack of Trailing capability in the Backtests.
      Trailing is not working at all in Backtests. It's not working not even on a bar-to-bar basis, and not only inside a bar.

      Comment


        #4
        Hello KonstantinosNT,

        Thanks for your note and Happy New Year.

        When backtesting a strategy that uses a trailing stop I am seeing the trailing stop action working when viewing the Chart display of the Strategy Analyzer window.

        See the attached image. I have also attached the strategy used to test this.

        In the screenshot, we can see that a buy order was placed and a couple of bars later the trailing stop was filled above this buy order. Since the trailing stop order was filled above the buy order, this indicates that the trailing stop was placed below the buy order and trailed above the buy order before being filled.

        If you run the attached strategy using the same settings seen in the screenshot, do you see the same behavior?

        Let me know if I may assist further.
        Attached Files
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        51 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        127 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        69 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        42 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        46 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X