Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trailing stop does not work in backtest

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

    Trailing stop does not work in backtest

    Hi,


    I am having trouble with the trailing stop feature when backtesting the attached strategy on the ES 60 minute chart.


    The strategy is supposed to set a trailing stop if, after X amount of days after entering the position, the position is winning. If the position is down after that X amount of days, then the position exits.

    The problem that happens is that when running the backtest, the trail stop never executes. The position never closes once opened, even if i set the trail stop for 1 tick on a 4 year backtest. Can anyone help?


    Thanks

    Code:
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Enter the description of your strategy here
        /// </summary>
        [Description("Enter the description of your strategy here")]
        public class ADXstrategy : Strategy
        {
            #region Variables
            private int adxLevel = 35;
            private int daysToHold = 8;
            private bool trailStopOn = true;
            private double trailStopAmount = .01;
            private bool trailstopset = false;
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                Add(ADX(14));
                Enabled = true;
                TraceOrders = true;
                CalculateOnBarClose = true;
                SetTrailStop(CalculationMode.Percent,1000000);
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            //According to book best results when ADX above 35 and held for 8 days
            {
            if (Position.MarketPosition == MarketPosition.Flat)
            {
            trailstopset = false;
            SetTrailStop(CalculationMode.Percent,1000000);
            if(ADX(14)[0] >= adxLevel)
            {
                EnterLong();
            }
            }
    
            if (Position.MarketPosition == MarketPosition.Long && BarsSinceEntry() >= daysToHold)
            {
            if (trailStopOn && Close [0] > Close [daysToHold] && !trailstopset)
                {
                    SetTrailStop(CalculationMode.Percent, trailStopAmount);
                    trailstopset = true;
                }
            else if (!trailstopset)
                ExitLong();
            }
            }
    
            #region Properties
            [Description("")]
            [GridCategory("Parameters")]
            public int AdxLevel
            {
            get { return adxLevel; }
            set { adxLevel = value; }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public int DaysToHold
            {
            get { return daysToHold; }
            set { daysToHold = value; }
            }    
            [Description("")]
            [GridCategory("Parameters")]
            public bool TrailStopOn
            {
            get { return trailStopOn; }
            set { trailStopOn = value; }
            }    
            [Description("")]
            [GridCategory("Parameters")]
            public double TrailStopAmount
            {
            get { return trailStopAmount; }
            set { trailStopAmount = value; }
            }    
            #endregion
        }
    }

    #2
    Hello sra18376,

    Thank you for your post.

    Please comment out SetTrailStop(CalculationMode.Percent,1000000); in both the Initialize() and OnBarUpdate() methods as seen in the attached screenshot - as the percent used is invalid.

    Then compile your code and run the test again. You should see entries and exits as expected.
    Attached Files

    Comment


      #3
      Okay, I did that, and it did somewhat work. But now the trail stop is never reset when flat, so it doesnt follow my rule of being initiated only after X amount of days after the entry. The first trade of the backtest it follows that rule, but afterward the trail stop is never reset so it is immediately initiated at entry instead of X days after. How to fix this? it needs to be reset somehow

      Comment


        #4
        I resolved the problem myself by manually coding in a trail stop in the OnBarUpdate. Thanks for the suggestions though!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        648 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        369 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        108 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        572 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        574 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X