Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trailing stop not working on backtest

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

    #31
    Please run with Baruch's suggestion then.
    Josh P.NinjaTrader Customer Service

    Comment


      #32
      Originally posted by Baruch View Post
      Stephen,
      Its simple in one line. The CalculationMode.Ticks is "The value the stop loss order is offset from the position entry price ". So on each close[0] you have to calculate what is the value for ticks.
      If you are long: SLTickValue = (Position.AvgPrice - Close[0] ) / TickSize + 10.
      It will be a negative number and I hope NT accepts it, if not you have to change to CalculationMode.Price or even better not to use SetStopLoss at all and use stopIorder = ExitLongStop().
      Please don't think in terms of 50 lines of SetStopLoss.

      Baruch
      I tried this. (I'm not sure if certain code is important or not so I included it just
      in case.) The idea was to take the absolute value of SLPrice.

      bool enteredLong=false;
      bool enteredShort=false;
      private double SLPrice=0;

      CalculateOnBarClose = true;

      if (Close[0]>Close[1]&& (Median [0]>Median[1])&& enteredLong==false)
      {
      EnterLong();
      SLPrice=Math.Abs((Position.AvgPrice - Close[0] ) / TickSize + 10);



      SetStopLoss(CalculationMode.Price,SLPrice);

      if (Close[0]<Close[1]&& (Median[0]<Median[1])&& enteredShort==false)
      {
      EnterShort();
      SLPrice=Math.Abs((Position.AvgPrice - Close[0] ) / TickSize + 10);

      SetStopLoss(CalculationMode.Price,SLPrice);


      #region Properties
      [Description("")]
      [Category("Parameters")]
      public int MyInput0
      {
      get { return myInput0; }
      set { myInput0 = Math.Max(1, value); }
      }
      #endregion


      The error that comes up is:
      ================================================== =
      Buy stop or buy stop limit orders can't be placed below the market.
      Affected Order: BuyToCover 1 Stop @ 10349
      ================================================== =
      I've seen this before in this strategy. Obviously I'm missing (or adding)
      something I shouldn't.

      Comment


        #33
        Just tried this. No orders execute at all.


        if (Close[0]>Close[1]&& (Median [0]>Median[1])&& enteredLong==false)
        {
        EnterLong();
        SLPrice=Math.Abs((Position.AvgPrice - Close[0] ) / TickSize + 10);



        ExitLongStop(SLPrice);
        }

        if (Close[0]<Close[1]&& (Median[0]<Median[1])&& enteredShort==false)
        {
        EnterShort();
        SLPrice=Math.Abs((Position.AvgPrice - Close[0] ) / TickSize + 10);

        ExitShortStop(SLPrice);
        }

        Comment


          #34
          SLPrice=Math.Abs((Position.AvgPrice - Close[0] ) / TickSize + 10);

          SLPrice=Math.Abs((Position.AvgPrice - Close[0] ) / TickSize + 10);

          Removed 'TickSize' from code. The strategy runs, but not
          correctly.

          Comment


            #35
            That's it for a day or two (at least).
            I think part of what I'm missing is the reason to exit the stop.

            if SLPrice such and such, then,

            ExitLongStop(SLPrice);

            if SLPrice such and such, then,

            ExitShortStop(SLPrice);

            Comment


              #36
              Hello,

              I will have someone reply to you on Monday. Thank you for your patience.
              DenNinjaTrader Customer Service

              Comment


                #37
                Stephen,

                You can't just go + 10 to your value. You should only adjust your values based on TickSize intervals.

                For instance if you want AvgPrice+10 ticks
                Position.AvgPrice + 10 * TickSize

                You should always be mindful of what your actual calculated stop price comes out to be. It will not submit if it is an invalid order. You should not try and place orders so close to the inside market.
                Josh P.NinjaTrader Customer Service

                Comment


                  #38
                  Originally posted by NinjaTrader_Josh View Post
                  Stephen,

                  You can't just go + 10 to your value. You should only adjust your values based on TickSize intervals.

                  For instance if you want AvgPrice+10 ticks
                  Position.AvgPrice + 10 * TickSize

                  You should always be mindful of what your actual calculated stop price comes out to be. It will not submit if it is an invalid order. You should not try and place orders so close to the inside market.
                  Thanks Josh. I'm spending a few hours on this tonight. I thought I tried this:



                  Is this post the way to go in this matter? I know all this can't be this hard.

                  - Stephen

                  Comment


                    #39
                    This is what I've just done. It doesn't work of course.
                    ================================================== ==

                    protected override void OnBarUpdate()
                    {

                    if (Close[0]>Close[1]&& (Median [0]>Median[1])&& enteredLong==false)
                    {
                    EnterLong();
                    SLPrice=Position.AvgPrice - 5 *TickSize;
                    SetStopLoss(CalculationMode.Price,SLPrice);

                    }
                    else if (Close[0]>Position.AvgPrice+10*TickSize&&SLPrice<Position.A vgPrice)
                    {
                    SLPrice=Position.AvgPrice;
                    SetStopLoss(CalculationMode.Price,SLPrice);
                    }
                    else if(Close[0]>Position.AvgPrice+11*TickSize&&SLPrice<Close[0]-10*TickSize)
                    {
                    SLPrice=Close[0]-10*TickSize;
                    SetStopLoss(CalculationMode.Price,SLPrice);
                    }





                    if (Close[0]<Close[1]&& (Median [0]<Median[1])&& enteredShort==false)
                    {
                    EnterShort();
                    SLPrice=Position.AvgPrice + 5 *TickSize;
                    SetStopLoss(CalculationMode.Price,SLPrice);

                    }
                    else if (Close[0]<Position.AvgPrice-10*TickSize&&SLPrice>Position.AvgPrice)
                    {
                    SLPrice=Position.AvgPrice;
                    SetStopLoss(CalculationMode.Price,SLPrice);
                    }
                    else if(Close[0]<Position.AvgPrice-11*TickSize&&SLPrice>Close[0]+10*TickSize)
                    {
                    SLPrice=Close[0]+10*TickSize;
                    SetStopLoss(CalculationMode.Price,SLPrice);
                    }







                    }

                    Comment


                      #40
                      Changed code to what is below:

                      ERROR: Sell stop or sell stop limit orders can't be placed above the market.
                      Affected Order: Sell 1 Stop @ 10392
                      ================================================== =======

                      protected override void OnBarUpdate()
                      {

                      if (Close[0]>Close[1]&& (Median [0]>Median[1])&& enteredLong==false)
                      {
                      EnterLong();
                      SLPrice=Position.AvgPrice - 5 *TickSize;
                      SetStopLoss(CalculationMode.Price,SLPrice);

                      }
                      else if (Close[0]>Position.AvgPrice+10*TickSize&&SLPrice<Position.A vgPrice)
                      {
                      SLPrice=Position.AvgPrice;
                      SetStopLoss(CalculationMode.Price,SLPrice);
                      }
                      else if(Close[0]>Position.AvgPrice+11*TickSize&&SLPrice>=Position. AvgPrice);
                      {
                      SLPrice=Close[0]-10*TickSize;
                      SetStopLoss(CalculationMode.Price,SLPrice);
                      }





                      if (Close[0]<Close[1]&& (Median [0]<Median[1])&& enteredShort==false)
                      {
                      EnterShort();
                      SLPrice=Position.AvgPrice + 5 *TickSize;
                      SetStopLoss(CalculationMode.Price,SLPrice);

                      }
                      else if (Close[0]<Position.AvgPrice-10*TickSize&&SLPrice>Position.AvgPrice)
                      {
                      SLPrice=Position.AvgPrice;
                      SetStopLoss(CalculationMode.Price,SLPrice);
                      }
                      else if(Close[0]<Position.AvgPrice-11*TickSize&&SLPrice<=Position.AvgPrice);
                      {
                      SLPrice=Close[0]+10*TickSize;
                      SetStopLoss(CalculationMode.Price,SLPrice);
                      }







                      }

                      Comment


                        #41
                        Even just using this code does not result in the stop loss being seen in Market Replay.
                        Totally confused now.


                        if (Close[0]>Close[1]&& (Median [0]>Median[1])&& enteredLong==false)
                        {
                        EnterLong();

                        SLPrice=Position.AvgPrice - 5 *TickSize;

                        SetStopLoss(CalculationMode.Price,SLPrice);

                        Comment


                          #42
                          The problem is that you are not in a position, so you don't have Position.AvgPrice.
                          You execute EnterLong(), but it takes time to enter a position and its not on OnBarUpdate() of a current bar.
                          As I said before, I don't use SetStoploss, and I advise you the same.
                          Use ExitLong() instead.

                          Baruch

                          Comment


                            #43
                            Baruch

                            Hi again. Merry Christmas!

                            I'll try to read and understand your reply right now. (Many times I assume.)

                            - Stephen

                            Comment


                              #44
                              This is what I have here:
                              ================================================== =
                              #region Variables
                              // Wizard generated variables
                              private int myInput0 = 1; // Default setting for MyInput0
                              // User defined variables (add any user defined variables below)

                              bool enteredLong=false;
                              bool enteredShort=false;
                              private double SLPrice=0;


                              #endregion
                              ================================================== =

                              Is this correct so far? There is stuff in the Pdf like:

                              private IOrder etc etc

                              I just can't tell if "private IOrder" and such is necessary or not.

                              Comment


                                #45
                                Stephen,
                                Do you write in NT script or in wizard?
                                If you write in Wizard, I can't help.
                                If in script then you need private IOrder, that way this variable will hold your entry it you will be able to query it.
                                Also you need VS. Thats the olny way I see how to debug!

                                Baruch

                                Comment

                                Latest Posts

                                Collapse

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