Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

setting a trailing stop to lowest low 10 bars back

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

    setting a trailing stop to lowest low 10 bars back

    Hi,


    I tried to set a trailing stop to the lowest low for the previous 10 bars. I did not see that option in the strategy wizard so I set the trailing stop to the available option of Variable0 and unlocked the code to see the following:

    protectedoverridevoid Initialize()
    {
    SetTrailStop("", CalculationMode.Ticks, Variable0, false);

    CalculateOnBarClose = true;
    }

    I then edited the code (and saved the changes) to the following:

    protectedoverridevoid Initialize()
    {
    SetTrailStop(Low[10]);

    CalculateOnBarClose = true;
    }

    When I backtest the strategy on daily bars the trailing stop gets set to the open price of the bar where a long position is taken, essentially entering and exiting at the same price. Where did I go wrong, with my code?

    This is my first attempt at inserting code into the strategies I create so please forgive me if the answer is obvious.

    Thanks in advance for your help.

    Joe

    #2
    Welcome to our forums Joe, for this trailing stop please use SetStopLoss with the mode set to 'Price' in OnBarUpdate(), this would then update the stoploss dynamically on each bar if needed.

    The inbuild SetTrailStop would be used with Ticks and Percent mode, but not with a direct price value coded in.

    Comment


      #3
      Originally posted by NinjaTrader_Bertrand View Post
      Welcome to our forums Joe, for this trailing stop please use SetStopLoss with the mode set to 'Price' in OnBarUpdate(), this would then update the stoploss dynamically on each bar if needed.

      The inbuild SetTrailStop would be used with Ticks and Percent mode, but not with a direct price value coded in.
      Thanks Bertrand,

      I'm not sure if I comprehended your instructions correctly. I changed the code to

      protected override void OnBarUpdate()
      {
      // Setting stop loss to lowest low of last 10 bars
      SetStopLoss(CalculationMode.Price, double Low[10]);
      // Condition set 1
      if (Close[0] > High[20])
      {
      EnterLong(DefaultQuantity, "");
      }
      }

      There were a lot of errors associated with the line of code that begins with SetStopLoss..., did I leave something out?

      Joe

      Comment


        #4
        Joe, Low[10] would already be a double value, so you could directly plug this in as parameter for the SetStopLoss().

        SetStopLoss(CalculationMode.Price, Low[10]);

        Comment


          #5
          Hi ,

          Just to say that Low[10] is not the lowest Low in the last 10 bars like expected in first post.
          Low[10] gives just the Low value 10 bars ago.

          If you want to calculate the lowest low , you should use the method :

          LowestBar(IDataSeries series, int period)

          So for your example :

          int lowestLowBar = LowestBar(Low,10);
          SetStopLoss(CalculationMode.Price, Low[lowestLowBar]);

          And it will gives you wahat you want !

          Comment


            #6
            Hi,
            i tried to set the Trailingstop in a strategy, but it don´t work. Here is the Code:


            {
            #region Variables
            // Wizard generated variables
            private int stopp = 10; // Default setting for Stopp
            private int target = 10; // Default setting for Target
            private int kontraktzahl = 1; // Default setting for Kontraktzahl
            // 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()
            {
            int lowestLowBar = LowestBar(Low,5);
            SetStopLoss(CalculationMode.Price, Low[lowestLowBar]);
            SetProfitTarget("", CalculationMode.Ticks, Target);

            CalculateOnBarClose = true;
            }

            Thanks for your Help.

            Pullbacktrader

            Comment


              #7
              Hello,

              int lowestLowBar = LowestBar(Low,5);

              In intialize there are no chart bars yet. So there is no way to calculate values.

              This needs to go down in OnBarUpdate() to be run by the chart.

              Same with the SetStopLoss() call also needs to be in OnBarUpdate() and not in Initialize().

              -Brett
              BrettNinjaTrader Product Management

              Comment


                #8
                Hi Brett,

                thank you very much! Is it possible to set the Stop one Tick above or under a Bar? Could you tell me how?

                The same question for the Entry: Is it also possible?

                Thanks for the Help!

                Comment


                  #9
                  Pullbacktrader, this would be possible as well - you can simply add / substract a tick (TickSize property) to your bar high / low value reference used in the stop calculation.

                  Comment


                    #10
                    Like this?

                    EnterShortStop(Kontraktzahl, Low[0]-1, "");
                    SetStopLoss (CalculationMode.Tick, High[2]+1);

                    Comment


                      #11
                      More like -

                      EnterShortStop(Kontraktzahl, Low[0] - 1 * TickSize, "");
                      SetStopLoss (CalculationMode.Tick, High[2] + 1 * TickSize);

                      You can of course change the multiplier to your liking, but then it's always tied to multiples of the instrument's ticksize (min move).

                      Comment


                        #12
                        Thanks Bertrand!

                        The Entry works, but the Position close after the Entry. I use this:

                        EnterShortStop(Kontraktzahl, Low[0] - 1 * TickSize, "");
                        SetStopLoss (CalculationMode.Ticks, High[2] + 1 * TickSize);

                        Comment


                          #13
                          Do you see this in backtesting or realtime trading / simulation?

                          Also please try setting the stop loss value before entering the trade with the Enter() method.

                          Comment


                            #14
                            I see it in the Replay-Modus. The stop goes to the last low after a short entry:

                            SetStopLoss (CalculationMode.Ticks, High[2] + 2 * TickSize);
                            EnterShortStop(Kontraktzahl, Low[0] - 1 * TickSize, "");

                            Thanks in advance for your help. :-(

                            Comment


                              #15
                              Sounds like you're missing a reset when being in flat state then - try adding this piece at your OnBarUpdate() start :

                              if (Position.MarketPosition == MarketPosition.Flat)
                              {
                              SetStopLoss(CalculationMode.Ticks, 10);
                              }

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              647 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
                              573 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X