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

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%.
    Paul H.NinjaTrader Customer Service

    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)?
        Paul H.NinjaTrader Customer Service

        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 LawrenHom, Today, 10:45 PM
            0 responses
            3 views
            0 likes
            Last Post LawrenHom  
            Started by love2code2trade, Yesterday, 01:45 PM
            4 responses
            28 views
            0 likes
            Last Post love2code2trade  
            Started by funk10101, Today, 09:43 PM
            0 responses
            7 views
            0 likes
            Last Post funk10101  
            Started by pkefal, 04-11-2024, 07:39 AM
            11 responses
            37 views
            0 likes
            Last Post jeronymite  
            Started by bill2023, Yesterday, 08:51 AM
            8 responses
            44 views
            0 likes
            Last Post bill2023  
            Working...
            X