Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using indicator in Trailing stop

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

    Using indicator in Trailing stop

    Hi,
    As a newbie, I am trying to build my backtesting strategy using Strategy Builder. I struggle with a specific setup of trailing stop in Stops and Targets section. Unfortunately, I did not find an answer here when using the Builder, so I am trying to ask for help with a new question.

    My trailing stop works perfectly well when using a specific number of ticks or a specific percentage from the Close price. However, when I try to use an indicator (for instance) the strategy does not work.

    The Rule is something like this:
    Set TrailStop(“”,CalculationMode.Ticks, (Default input[1]-((ATR(Close, 14)[1]*3) )), false)

    The code looks perfectly alright to me. Any idea why it does not work?

    Thanks

    #2
    Hello Domek_69,

    Thanks for your post and welcome to the NinjaTrader forums!

    Do you see any error messages in the "log" tab of the control center when you apply the strategy?

    The calculationMode of ticks requires that the calculation return an integer value that would be used for the number of ticks to trail by. You may want to print the value of Default input[1]-((ATR(Close, 14)[1]*3 to see what is actually being generated.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hello NinjaTrader_Paul,
      Good point! I did not check the log. The message there was: "Error on calling 'OnStateChange' method: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."

      And I did not realize that the output of Default input[1]-((ATR(Close, 14)[1]*3 is not an integer. So I quickly changed the code to mode.Price. This time the code went through but with another message in the log: "Trailing stops cannot be calculated with calculation mode 'Price'. Please use one of the other modes." :-(

      At the moment I do not know how to display the resulting number, I am afraid.

      Comment


        #4
        Hello Domek_69,

        Thanks for your reply.

        Anytime something is not functioning as expected the "log" tab is a good place to start.

        The issue here though is that the Strategy Builder does not work with a dynamic way in which to set the values for the Set methods (SetTrailStop, SetStopLoss and SetProfitTarget). We do have a feature request to add this capability so I will add your vote to that feature and will update this thread with that feature request number for your reference.

        To use these methods dynamically (reacting to price or indicator) you would need to unlock the strategy and work directly in the Ninjascript coding. You would move the SetStopLoss() to just above where you place the entry order and set the value at that time (using either calculationMode.Price or CalculationMode.ticks if you convert to ticks).

        An alternative would be to not use the Set methods and create a set that checks for being in a market position (long or short) and then compares the price to an indicator or some other dynamic value and if the condition that you set is true, then the action is to ExitLong (or ExitShort). For example (long), if the close price crosses below a 13 period EMA ExitLong().
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hello Domek_69,

          We have added your vote to SFT-3342, "Strategy Builder to allow "dynamic" Targets and Stops"
          This feature request does not mean it will or will not be implemented and is a means for program management to review open feature requests. If implemented, it would be identified in the release notes of the release version as found in the helpguide, here: https://ninjatrader.com/support/help...ease_notes.htm
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Hi,
            Thank you for your explanation. Maybe I misunderstood - the suggested alternative can be achieved in the builder? It sounded like it could be set in Condition and Action Section. Is that what you meant?

            Petr

            Comment


              #7
              Hello Petr,

              Thanks for your reply.

              Yes, that is correct, you can do the alternative in the strategy builder.
              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_Paul View Post
                Hello Domek_69,

                Thanks for your reply.

                Anytime something is not functioning as expected the "log" tab is a good place to start.

                The issue here though is that the Strategy Builder does not work with a dynamic way in which to set the values for the Set methods (SetTrailStop, SetStopLoss and SetProfitTarget). We do have a feature request to add this capability so I will add your vote to that feature and will update this thread with that feature request number for your reference.

                To use these methods dynamically (reacting to price or indicator) you would need to unlock the strategy and work directly in the Ninjascript coding. You would move the SetStopLoss() to just above where you place the entry order and set the value at that time (using either calculationMode.Price or CalculationMode.ticks if you convert to ticks).

                An alternative would be to not use the Set methods and create a set that checks for being in a market position (long or short) and then compares the price to an indicator or some other dynamic value and if the condition that you set is true, then the action is to ExitLong (or ExitShort). For example (long), if the close price crosses below a 13 period EMA ExitLong().
                I just realized the problem of using the suggested alternative. True trailing stop loss would follow the price in the direction of trade but never reverse. For instance, for Long trade, it would go up when the Close price moves up and stays when the Close price goes down. But the suggested alternative does not provide this feature. Or am I wrong?

                Petr

                Comment


                  #9
                  Hello Petr,

                  Thanks for your reply.

                  For a trailing type stop, you are correct.

                  Please see the example in NinjaTrader Chelsea's examples here: https://ninjatrader.com/support/foru...27&postcount=2
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    Thank you Paul,
                    I download the trail builder example and look at it. I think I rather try to unlock the code and move the SetStopLoss() code as you suggested earlier. Is there any code example for that, please?

                    Thanks

                    Petr

                    Comment


                      #11
                      Hello Domek_69,

                      Thanks for your reply.

                      Can you clarify which of the two types of "trailing" stop you are interested in?

                      SetTrailStop() will start at what ever level you specify, then as price moves into profit, the stop is automatically adjusted that same number of ticks towards price and continues in that manner but never goes backwards.

                      SetStopLoss(), when tied to an indicator, will adjust to the value of the indicator, both up and down and it would trail as much as the indicator price trails the current price. It can also set the stop just once if you prefer.
                      Paul H.NinjaTrader Customer Service

                      Comment


                        #12
                        Hi Paul,
                        it is SetTrailStop() what I am really interested in. That is the behavior I am looking for.


                        Thanks


                        Petr

                        Comment


                          #13
                          Hello Petr,

                          Thanks for your reply.

                          Here is an example:

                          if (Your conditions for entry)
                          {
                          SetTrailStop(CalculationMode.Ticks, (ATR(14)[0] * 3) / TickSize);
                          // your entry order here
                          }


                          Once the entry order is filled the trail stop will start at the value in ticks specified. You may want to add a Print line so that you can see what value is being determined by (ATR(14)[0] * 3) / TickSize) such as: Print ("trailstop set at: "+((ATR(14)[0] * 3) / TickSize).ToString());

                          The key point is that the trail stop value must be set before the entry order is filled and by coding it prior to placing the entry order meets that requirement.

                          The TickSize contains the minimum movement for the instrument. Dividing the ATR value *3 by the ticksize will provide a value in ticks. reference: https://ninjatrader.com/support/help.../?ticksize.htm

                          Please be sure to review all of the notes/tips here: https://ninjatrader.com/support/help...ttrailstop.htm
                          Paul H.NinjaTrader Customer Service

                          Comment


                            #14
                            can any sample show me either close by trailingstop or by indicator, once one of them get done ,the other would not take effect, now I always face the problem that when the trailingstop met and done, the indicator also takes effect and made an extra order (when i am getting long, it would make extra short postiion.

                            Comment


                              #15
                              Hello stantenlee,

                              If you have existing conditions that happen at the same time that would generally be a situation where you need an else statement or to use a variable to control the logic. Building off the last posts example that would look like the following:


                              Code:
                              if (Your conditions for entry)
                              {
                              SetTrailStop(CalculationMode.Ticks, (ATR(14)[0] * 3) / TickSize);
                              // your entry order here
                              }
                              else if(some other condition)
                              {
                                  //alternative order action
                              }
                              JesseNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by alifarahani, Today, 09:40 AM
                              3 responses
                              15 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by RookieTrader, Today, 09:37 AM
                              4 responses
                              18 views
                              0 likes
                              Last Post RookieTrader  
                              Started by PaulMohn, Today, 12:36 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post PaulMohn  
                              Started by love2code2trade, 04-17-2024, 01:45 PM
                              4 responses
                              40 views
                              0 likes
                              Last Post love2code2trade  
                              Started by junkone, Today, 11:37 AM
                              3 responses
                              25 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X