Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trailing stop not working on backtest

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

    #16
    My mistake. It should be:
    SLPrice=Close[0]-10 * Ticksize;
    Don't put the breakeven and trailing stop in the same "if" with entry.
    And you didn't put breakeven. You changed the SLPrice but didn't fire the SetStopLoss.
    Baruch

    Comment


      #17
      Just changed my reply via edit.

      Now looking at your 11:13 AM reply.

      Comment


        #18
        I'm going over the code now. I made a mistake somewhere. (Not understanding it
        fully helps me in that.) It compiled and ran for a while then I got the error included
        in the attachment. - Stephen

        ================================================== ===================

        protected override void OnBarUpdate()
        {





        if (Close[0]>Close[1]&& (Median [0]>Median[1])&& enteredLong==false)
        {
        EnterLong();
        SLPrice=Position.AvgPrice-5;
        if(Close[0]>Position.AvgPrice+15*TickSize&&SLPrice<Close[0]-10*TickSize)

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


        if (Close[0]<Close[1]&& (Median[0]<Median[1])&& enteredShort==false)
        {
        EnterShort();
        SLPrice=Position.AvgPrice+5;
        if(Close[0]<Position.AvgPrice-15*TickSize&&SLPrice>Close[0]+10*TickSize)

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





        }
        Attached Files

        Comment


          #19
          The next error message after I click "OK" is:

          Buy stop or buy stop limit orders can't be placed below the market.
          Affected Order:BuyToCover 1 Stop @5

          I did move a couple lines of code. To fix:

          """Don't put the breakeven and trailing stop in the same "if" with entry."""

          It didn't help. I think it is close to right though.

          Comment


            #20
            These lines of code seem incorrect:

            SLPrice=Position.AvgPrice-5;

            Also:


            SLPrice=Position.AvgPrice+5;

            I'll be changing them when I can. I am recording for MarketReplay now,
            so I can't go back to Dec. 11th's chart using MarketReplay simultaneously
            (as far as I can tell).

            This is confusing too:
            ================================================== ===========
            p477
            © NinjaTrader, LLC, 2005
            NinjaTrader offers exclusive software for futures trading. With our modern trading platform, you will control every step of your trading journey. Open account to trade futures with us!


            Position Methods and Properties

            AvgPrice
            Definition

            Gets the average price of a strategy position.

            Property Value

            A double value representing the position's average price per unit.

            Syntax
            Position.AvgPrice
            ================================================== =========

            Apparently Positon.AvgPrice is the ORIGINAL PRICE of the future (ie. YM)
            when the strategy went long (or short). Though I'm still not certain of this
            as I read the Pdf. If it's NOT the price when the strategy was first enacted
            it must be the current value of the YM, which doesn't make sense to me as
            I look at the coding.

            Comment


              #21
              Position.AvgPrice is the average entry filled price for your currently held strategy position. This is not the price of the instrument per se. It is the price you got your order filled at.
              Josh P.NinjaTrader Customer Service

              Comment


                #22
                Thanks Josh. That makes sense. Well I changed my code
                and it still doesn't work.

                SLPrice=Position.AvgPrice-5*TickSize;

                AND

                SLPrice=Position.AvgPrice+5*TickSize;

                One idea was to start with a 5 point stoploss and when the YM went
                up 15 points (or whatever) to make that breakeven and so on.

                Baruch said at one point:

                """Don't put the breakeven and trailing stop in the same "if" with entry.
                And you didn't put breakeven. You changed the SLPrice but didn't fire the SetStopLoss."""

                I'll be looking at the code some tonight, but I'm not sure what the code is doing or
                what Baruch meant. If I accomplish anything I'll re-post about it.

                - Stephen

                Comment


                  #23
                  I tired to change the code according to the basic pattern that Baruch
                  gave me. The more I look at it the less it makes sense, and of course
                  I keep getting that same error as I posted already. One of the "+"
                  or "-" is wrong I believe.

                  I simplified the program to just try to get a single breakeven stop.
                  There was indeed a "+" that should have been a "-" so the error did not appear.

                  The program still DIDN'T work properly as far as I can tell.

                  The program below seems to work fine, but it just moves the stop loss to
                  breakeven and can't turn that stop into a trailing stop.

                  So anyway, I'd still like to get a stoploss that moves up to breakeven and then
                  becomes a trailing stop. After a number of hours at this I don't know how many
                  new things I can come up with.



                  if (Close[0]>Close[1]&& (Median [0]>Median[1])&& enteredLong==false)
                  {
                  EnterLong();
                  SetStopLoss(CalculationMode.Ticks,5);
                  if(Close[0]>Position.AvgPrice+10*TickSize)
                  SetStopLoss(CalculationMode.Ticks,0);
                  }

                  if (Close[0]<Close[1]&& (Median[0]<Median[1])&& enteredShort==false)
                  {
                  EnterShort();
                  SetStopLoss(CalculationMode.Ticks,5);
                  if(Close[0]<Position.AvgPrice-10*TickSize)
                  SetStopLoss(CalculationMode.Ticks,0);
                  }

                  Comment


                    #24
                    I've given up for the short term. I haven't been able to change
                    the SLPrice after the YM moves up. (I seem to be able to get
                    the stop loss to change to a breakeven, but that's it.)

                    I didn't see how at this point to expand on Baruch's code here:



                    I thought about writing a few lines of code everytime the YM moves
                    up 1 point to change the SLPrice. I wouldn't think this is necessary
                    since there would be about 500 lines of code for a 100 point move up
                    and of course another 500 lines of code for a 100 point move down.

                    So once again the basic idea is this...

                    ================================================== =========

                    Enter YM long with a 5 tick stop. When/If YM moves up 10 ticks make
                    stop a breakeven, when/if 11 ticks make stop 10 ticks back for a 1 tick
                    profit, when/if 12 ticks make stop 10 ticks back for a 2 tick profit.
                    In other words have a trailing stop of 10 ticks.
                    (Of course the same idea in a downtrend.)

                    I'm using equal range bars set to 15 ticks, but I don't think that would matter
                    to the calculations. Also Calculate on bar close= true

                    NoGapRange bars download link is here if anyone is curious about
                    using them in their trading:




                    I'll be back Wednesday at the earliest in case anyone feels
                    ambitious.


                    Also couldn't figure out if plus or minus would be used here or
                    if it was even allowed to use minus numbers in the SetStopLoss
                    format I wrote below. ( I realize this isn't the correct format for
                    using SLPrice, but I had to keep trying, so I went to "Ticks" and
                    spent an hour or so there.)


                    else if(Close[0]>Position.AvgPrice+11*TickSize)
                    {
                    SetStopLoss(CalculationMode.Ticks,-1);
                    }
                    else if(Close[0]>Position.AvgPrice+12*TickSize)
                    {
                    SetStopLoss(CalculationMode.Ticks,-2);
                    }
                    else if(Close[0]>Position.AvgPrice+13*TickSize)
                    {
                    SetStopLoss(CalculationMode.Ticks,-3);

                    etc. etc. etc. etc. for 100 ticks or so

                    Also couldn't figure out if plus or minus would be used here either in a downtrend:

                    else if(Close[0]<Position.AvgPrice-11*TickSize)
                    {
                    SetStopLoss(CalculationMode.Ticks,-1);
                    }
                    else if(Close[0]<Position.AvgPrice-12*TickSize)
                    {
                    SetStopLoss(CalculationMode.Ticks,-2);
                    }
                    else if(Close[0]<Position.AvgPrice-13*TickSize)
                    {
                    SetStopLoss(CalculationMode.Ticks,-3);
                    }
                    else if(Close[0]<Position.AvgPrice-14*TickSize)
                    {

                    etc. etc. etc. for 100 ticks or so

                    Comment


                      #25
                      Stephen,

                      You should not use CalculationMode.Ticks and try to use negative values for the ticks. You should be using CalculationMode.Price and give the exact price you want the stop loss to be at.

                      If you want it at break even the price you should submit at is Position.AvgPrice. If you want it higher than your entry price then you would do Position.AvgPrice + 2 * TickSize for instance.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #26
                        Thanks Josh. Yes I understand what you said. I still don't have
                        any idea how to increase the stop one tick at a time and still have
                        it stay 10 points (let's say) behind the YM as it moves up, without
                        writing at least one line of code for each tick up in the YM.

                        Obviously if we assume that there will be some days when the YM
                        makes a steady up (or down) move of 50 points I would want to have
                        the code track it. To me, at this point, it means writing 50 or more lines
                        for the bull side of the coding and 50 or more for the bear side.

                        If you have any hints at this point I'd like to hear them. Of course I
                        will not be idle. I intend to research the matter today and tomorrow
                        here and at Mikes trading for help.

                        - Stephen

                        Comment


                          #27
                          Stephen,

                          Have you considered just using a trailing stop?
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #28
                            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

                            Comment


                              #29
                              Originally posted by NinjaTrader_Josh View Post
                              Stephen,

                              Have you considered just using a trailing stop?
                              Josh

                              Well, if a trailing stop can be: a stop loss, which turns into a breakeven,
                              which turns into a trailing, I will consider it. I don't think that's doable in NT.

                              Comment


                                #30
                                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
                                Thanks Baruch. I expect to reply one way or the other Sat. or Sun.
                                regarding progress, or the lack. I almost understand you in this matter.

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                600 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                347 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by Mindset, 02-09-2026, 11:44 AM
                                0 responses
                                103 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                                0 responses
                                558 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                558 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X