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

Trailing stop not working on backtest

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

    Trailing stop not working on backtest

    I have a strategy that includes
    SetProfitTarget
    SetStopLoss
    SetTrailStop
    set in the "Stops and targets" page of the Strategy Wizzard

    In backtesting this strategy, the Trailing Stop does not cause an exit, but rather the initial stop always triggers if the target is not hit.

    What am I doing wrong?

    I have not had this strategyh trigger Real-Time yet, so I cannot say if it works live.

    #2
    eDon1,

    You cannot use a SetStopLoss in conjunction with a SetTrailStop for the same order. You will need to pick and choose one that you want to use.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      OK - I cannot set both an Initial Stop and a Trailing Stop in the Strategy Wizzard - seems to me that this would be a basic requirement, but guess not.

      Is there anyway to do this programatically?

      Comment


        #4
        eDon1,

        No, you cannot have simultaneous stop loss and trailing stop. You can have one or the other, it does not make sense to have both. A trailing stop always starts out as a stop loss, then just moves up as price moves up. A stop loss just stays at the submitted price. There should never be a need to have both at the same time.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Sorry Josh - I must vehemently dissagree that there is no need for both an initial fixed stop and a trailing stop. I think that most folks who trade futures for a living would agree.

          The ATM strategies provide for this, with up to 3 differing trailing values, so someone at NT thought it was a good idea.

          My strategy calls for a very tight stop when the order is placed and for the stop to start trailing when the price moves away from entry. In this way my risk is small and I can capture a profit even if I do not reach my target.

          Perhaps I was not clear in my question.
          I need to set an initial stop of 3 ticks and then have my stop start moving up as price moves to greater than +7 ticks then trail -10 ticks from there - up to my profit target of +20 ticks. In this way, I lock in a profit of 1 tick if the price moves to +11 and this locked in profit increases tick by tick until either my target or the trailing stop is hit.

          Comment


            #6
            eDon1,

            There is a misunderstanding. You should only attach one SetStopLoss/TrailStop() to one entry order. If you want a multi-step, you need to use multiple entries.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              What we've got here is a failure to communicate. (Cool Hand Luke)

              Josh: It's apparent that the NT Script Wizzard cannot do what I want - I get it. Thanks for trying.

              Have any of my fellow Ninja Traders solved this challange with a simple bit of code? I have looked back a hundred or more posts without finding a solution - does anyone have one they can point me to?

              I simply want to have an initial stop that becomes a trailing stop at the appropriate level.
              Not multi-step trailing
              Not multiple targets

              This is what the simplest ATM strategy does with a single target and single trailing stop.

              Wouldn't it be great if a Script could just trigger an ATM strategy!
              How about it NT development - can you implement this as a feature?

              Comment


                #8
                eDon1, NinjaTrader is fully capable of triggering an ATM strategy. Please see the reference that comes installed with NinjaTrader at Tools -> Edit NinjaScript -> Strategy -> SampleAtmStrategy.
                AustinNinjaTrader Customer Service

                Comment


                  #9
                  Hi

                  I also am interested in having a stop loss and a trailing stop in a strategy.

                  I checked out SampleAtmStrategy (which is a nightmare) and
                  SamplePriceModification. In SamplePriceModification it seems like
                  setting the stop loss to breakeven isn't too hard after x ticks of profit.

                  I might have to go this route, if SampleAtmStrategy in the only way to
                  get a trailing stop to work within a strategy.

                  When I read page 468 (below) I got the impression that adding a
                  trailing stop only took a few lines of code.


                  The Pdf says this on pg.468:

                  The SetTrailStop() method can NOT be used concurrently with the SetStopLoss() method
                  for the same position, if both methods are called for the same position (fromEntrySignal)
                  the SetStopLoss() will always take precedence. You can however, use both methods in the
                  same strategy if they reference different signal names.


                  QUESTION: To have a strategy that goes long(or short) with a stoploss
                  and trailing stop do I have to do it similar to the SampleAtmStrategy
                  coding or am I just mistaken?

                  - Stephen

                  Comment


                    #10
                    Hi,
                    As Ninja support stated you don't need both. I have in my strategies stop loss, break even and trailing stop, but I do it on the same order.
                    First I put a stop loss, then if price moves in my direction X tick I change the stop loss order to break even price (the emphasis is on change), then after it goes more in my favor I change the stop order to trail the price on each bar. All is done with the same stop order.

                    Baruch

                    Comment


                      #11
                      Baruch

                      Thanks. In SamplePriceModification I sort of understand how to move the
                      stoploss up to breakeven. I am trying to do that in my own strategy now.


                      This is their code below. The code is about setting a breakeven stop (from stoploss)
                      and a target stop. I would be interested now in setting a breakeven stop (from stoploss)
                      which changes into a trailing stop.

                      It seems I need to have a loop of some sort once the breakeven stop is first set
                      (from stoploss)
                      so that if the price moves up (ie. if long) the strategy thinks the breakeven is now
                      at a higher level. I guess this is what you are saying.

                      If you (or support) has any hints that would be nice. I will re-post if I come
                      up with anything ahead of anyone.

                      -Stephen


                      ================================================== ==================
                      ================================================== ==================
                      ================================================== ==================
                      ================================================== ==================
                      protected override void Initialize()
                      {
                      /* There are several ways you can use SetStopLoss and SetProfitTarget. You can have them set to a currency value
                      or some sort of calculation mode. Calculation modes available are by percent, price, and ticks. SetStopLoss and
                      SetProfitTarget will submit real working orders unless you decide to simulate the orders. */
                      SetStopLoss(CalculationMode.Ticks, stoplossticks);
                      SetProfitTarget(CalculationMode.Ticks, profittargetticks);

                      CalculateOnBarClose = true;
                      }

                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {
                      // Resets the stop loss to the original value when all positions are closed
                      if (Position.MarketPosition == MarketPosition.Flat)
                      {
                      SetStopLoss(CalculationMode.Ticks, stoplossticks);
                      }

                      // If a long position is open, allow for stop loss modification to breakeven
                      else if (Position.MarketPosition == MarketPosition.Long)
                      {
                      // Once the price is greater than entry price+50 ticks, set stop loss to breakeven
                      if (Close[0] > Position.AvgPrice + 50 * TickSize)
                      {
                      SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                      }
                      }

                      // Entry Condition: Increasing price along with RSI oversold condition
                      if (Close[0] > Close[1] && RSI(14, 3)[0] <= 30)
                      {
                      EnterLong();
                      }
                      }

                      #region Properties
                      /// <summary>
                      /// </summary>
                      [Description("Numbers of ticks away from entry price for the Stop Loss order")]
                      [Category("Parameters")]
                      public int StopLossTicks
                      {
                      get { return stoplossticks; }
                      set { stoplossticks = Math.Max(0, value); }
                      }

                      /// <summary>
                      /// </summary>
                      [Description("Number of ticks away from entry price for the Profit Target order")]
                      [Category("Parameters")]
                      public int ProfitTargetTicks
                      {
                      get { return profittargetticks; }
                      set { profittargetticks = Math.Max(0, value); }
                      }
                      #endregion
                      }
                      }

                      Comment


                        #12
                        Stephen,
                        Why loop and why a strategy has to think that break even moved?
                        Just call this order (in your mind) Stoploss/Breakeven/TrailingStop.
                        Do something like:
                        1. In variable section: private double SLPrice = 0;
                        2. When you enter a trade: SLPrice = Position.AvgPrice - 20; what ever is your stop loss
                        3.
                        if (Close[0] > Position.AvgPrice + 150 * TickSize && SLPrice < Close[0] - 100 * TickSize )
                        {
                        SLPrice = Close[0] - 100;
                        SetStopLoss(CalculationMode.Price, SLPrice);
                        }

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

                        Thats all,
                        Baruch

                        p.s. I don't use SetStopLoss, but it should work.

                        Comment


                          #13
                          I am reading your post now. Thanks again. Well I was just going to
                          post the code I wrote. It compiled so it has to work (hee hee). It didn't
                          do anything different. I will reply shortly.

                          Comment


                            #14
                            I did this below, but it's probably not right.

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


                            #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

                            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;
                            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;
                            SetStopLoss(CalculationMode.Price,SLPrice);
                            }
                            else if (Close[0]<Position.AvgPrice-5*TickSize&&SLPrice>Position.AvgPrice)
                            {
                            SLPrice=Position.AvgPrice;
                            SetStopLoss(CalculationMode.Price,SLPrice);
                            }


                            }


                            }
                            Last edited by stephenszpak; 12-14-2009, 05:14 AM.

                            Comment


                              #15
                              Actually I did forget an 'else if'.

                              Trying this all again in my NT software.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by wzgy0920, 04-20-2024, 06:09 PM
                              2 responses
                              27 views
                              0 likes
                              Last Post wzgy0920  
                              Started by wzgy0920, 02-22-2024, 01:11 AM
                              5 responses
                              32 views
                              0 likes
                              Last Post wzgy0920  
                              Started by wzgy0920, 04-23-2024, 09:53 PM
                              2 responses
                              49 views
                              0 likes
                              Last Post wzgy0920  
                              Started by Kensonprib, 04-28-2021, 10:11 AM
                              5 responses
                              193 views
                              0 likes
                              Last Post Hasadafa  
                              Started by GussJ, 03-04-2020, 03:11 PM
                              11 responses
                              3,235 views
                              0 likes
                              Last Post xiinteractive  
                              Working...
                              X