Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop loss backtesting not working

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

    Stop loss backtesting not working

    Good evening,

    i'm trying to backtest a MACD crossover strategy created with the strategy wizard.
    The strategy works but the stop loss doesn't seem to function.
    Can anyone help me, I'm using Ninjatrader 7.
    This is the generated code from the wizard:


    // 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 MACDCrossing : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int myInput0 = 1; // Default setting for MyInput0
    // User defined variables (add any user defined variables below)
    #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()
    {
    SetStopLoss("CrossoverLong", CalculationMode.Percent, 5, false);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (CrossAbove(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 1))
    {
    EnterLong(DefaultQuantity, "CrossoverLong");
    }

    // Condition set 2
    if (CrossBelow(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 1))
    {
    ExitLong("", "");
    }
    }
    ecc.

    #2
    Hello Mario1983,

    Thanks for your post and welcome to the forums!

    As you are using the CalculationMode.Percent, the input value should be 0.05 for 5%.

    Comment


      #3
      Good evening,

      I have changed the value from 5 to 0.05 but it doesn't work.
      Can you help me?
      Perhaps I have set something wrong in the entry signal.

      Comment


        #4
        Hello Mario1983,

        Thanks for your reply.

        Can you clarify that you are or are not seeing entries?

        What instrument are you testing the strategy on?

        I see that you are using the signal name of CrossoverLong but you are not using that signal name for your ExitLong().

        Can you provide a screenshot of your strategy on a chart that shows an entry (and any exits)?

        Comment


          #5
          Good morning,

          1)I can observe entry and exit on the chart, only the stop loss doesn't work properly.
          2)I am testing the strategy on some yahoo finance imported stocks from FTSEMIB.
          In the afternoon I will post the chart with entry and exit points.

          Regards

          Comment


            #6
            Backtesting stop loss

            Good afternoon,

            I've fixed the problem. Here the new 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 MACDCrossing : Strategy
            {
            #region Variables
            // Wizard generated variables
            private double stopLoss = 0.05; // Default setting for StopLoss
            // User defined variables (add any user defined variables below)
            #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()
            {
            SetStopLoss("CrossoverLong", CalculationMode.Percent, StopLoss, false);

            CalculateOnBarClose = true;
            }

            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
            // Condition set 1
            if (CrossAbove(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 1))
            {
            EnterLong(DefaultQuantity, "CrossoverLong");
            }

            // Condition set 2
            if (CrossBelow(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 1))
            {
            ExitLong("ExitLong", "CrossoverLong");
            }
            }

            #region Properties
            [Description("")]
            [GridCategory("Parameters")]
            public double StopLoss
            {
            get { return stopLoss; }
            set { stopLoss = Math.Max(0.001, value); }
            }
            #endregion

            Regards

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            627 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            359 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
            562 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            567 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X