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/Trailing Stops aren't working in BackTesting

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

    Stop Loss/Trailing Stops aren't working in BackTesting

    SetStopLoss("", CalculationMode.Percent, 1, false);
    SetTrailStop("", CalculationMode.Price, 0.01, false);

    I have both of these in my Initalize() function, but when I run a backrest using this strategy neither of them kicks in. Any suggestions?

    #2
    milemike,

    Could you show me more of your code perhaps? It may be something elsewhere.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      PHP Code:
      protected override void Initialize()
              {
                  
      SetStopLoss(""CalculationMode.Percent1false);
                  
      SetTrailStop(""CalculationMode.Price0.01false);

                  
      CalculateOnBarClose true;
              }

              
      /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              
      protected override void OnBarUpdate()
              {
                  
                  if (
      Close[0] > Close[1]
                      && 
      Close[1] > Close[2]
                      && 
      GrowthRatio().GRPlot[0] < 0.2
                      
      && GrowthRatio().GRPlot[0] > 0.0
                  {
                      
      EnterShort(1000"EnterShort");
                  }
                  
                  
      // Condition set 1
                  
      if (Close[0] < Close[1]
                      && 
      Close[1] < Close[2]
                      && 
      GrowthRatio().GRPlot[0] < 0.2
                      
      && GrowthRatio().GRPlot[0] > 0.0
                  {
                      
      EnterLong(1000"EnterLong");
                  }
                  
                  
                  
           } 

      Here's the code for my Initialize() and OnBarUPdate
      Last edited by milemke08; 06-10-2012, 01:33 PM.

      Comment


        #4
        Milemike,

        I believe the issue here might be that you have set up an entrance signal for your two orders, but have not tied it to the stop loss / profit target.

        EnterShort(1000, "EnterShort"); // EnterShort is the signal name

        SetStopLoss("", CalculationMode.Percent, 1, false); /There is no signal name.

        Please let me know if I may assist further.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Okay, so I put

          PHP Code:
          SetStopLoss("EnterLong"CalculationMode.Percent1false);
          SetTrailStop("EnterLong"CalculationMode.Price0.01false); 
          in my Initialize(), and

          PHP Code:
          if (Close[0] < Close[1]
                          && 
          Close[1] < Close[2]
                          && 
          GrowthRatio().GRPlot[0] < 0.2
                          
          && GrowthRatio().GRPlot[0] > 0.0
                      {
                          
          EnterLong(1000"EnterLong");
                      } 
          in the OnBarUpdate(), but when I run this strategy in the Strategy Analyzer it still doesn't exit my positions when the stop loss/trailing stop should kick in.

          Comment


            #6
            milemike,

            SetTrailStop cannot use CalculationMode.Price is one thing I missed before, apologies about that.

            Adam P.NinjaTrader Customer Service

            Comment


              #7
              As you can see, from the time it entered the long position, there was several occasions when the trailing stop or stop loss should have been triggered.
              Attached Files

              Comment


                #8
                milemike,

                SetTrailStop cannot use CalculationMode.Price is one thing I missed before, apologies about that.

                Last edited by NinjaTrader_AdamP; 06-10-2012, 02:30 PM.
                Adam P.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_AdamP View Post
                  This link doesn't go anywhere for me

                  Comment


                    #10
                    MileMike,

                    I have modified the post, but here it is again : http://www.ninjatrader.com/support/h...ttrailstop.htm
                    Adam P.NinjaTrader Customer Service

                    Comment


                      #11
                      Strategy Wizard trailing stops don't work

                      My Stragegy Wizard generated stops don't work. Here's my Ninjascript:

                      {
                      #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()
                      {
                      Add(SampleGetHighLowByTimeRange(7, 35, 7, 30));
                      Add(SampleGetHighLowByTimeRange(7, 35, 7, 30));
                      SetTrailStop("EnterLong", CalculationMode.Ticks, 4, false);

                      CalculateOnBarClose = true;
                      }

                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {
                      // Condition set 1
                      if (CrossAbove(Low, SampleGetHighLowByTimeRange(7, 35, 7, 30).HighestHigh, 1)
                      && Low[3] > SampleGetHighLowByTimeRange(7, 35, 7, 30).HighestHigh[0])
                      {
                      EnterLong(DefaultQuantity, "ACDlong");
                      }
                      }

                      What am I doing wrong?

                      Comment


                        #12
                        Hello MindfulTrader,

                        The From entry signal of the Stop Loss does not match the Signal name of the entry order.

                        The entry order signal name is ACDlong.

                        The From entry signal of the Stop Loss needs to be the same.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_ChelseaB View Post
                          Hello MindfulTrader,

                          The From entry signal of the Stop Loss does not match the Signal name of the entry order.

                          The entry order signal name is ACDlong.

                          The From entry signal of the Stop Loss needs to be the same.
                          That did it! Thank you!

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Barry Milan, Today, 10:35 PM
                          1 response
                          8 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by WeyldFalcon, 12-10-2020, 06:48 PM
                          14 responses
                          1,428 views
                          0 likes
                          Last Post Handclap0241  
                          Started by DJ888, Yesterday, 06:09 PM
                          2 responses
                          9 views
                          0 likes
                          Last Post DJ888
                          by DJ888
                           
                          Started by jeronymite, 04-12-2024, 04:26 PM
                          3 responses
                          40 views
                          0 likes
                          Last Post jeronymite  
                          Started by bill2023, Today, 08:51 AM
                          2 responses
                          16 views
                          0 likes
                          Last Post bill2023  
                          Working...
                          X