Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trailing Stops don't work in my strategy

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

    Trailing Stops don't work in my strategy

    Since I'm very new to NT and strategy wizard this question may be a bit simplistic. I've created a very simple Fast crosses Slow MA and am attempting to put a profit target % and trailing stop % via the strategy wizard. It maybe working but I cannot see the target exits and trailing stop exits on the charts but I think the strategy analyzer is taking these two conditions into account when it does the backtest/optimizer/walk forward. Below is the output from the wizard. Can anyone offer a suggestion that will allow the target exit and trailing stop to show up on the charts? Thanks in advance, John

    /// <summary> /// FastSMAaboveSlowSMAProfitTargetwithTrailingStop /// </summary> [Description("FastSMAaboveSlowSMAProfitTargetwithTr ailingStop")] public class HudsonFastAboveSlowExit : Strategy { #region Variables // Wizard generated variables private int fast = 10; // Default setting for Fast private int slow = 30; // Default setting for Slow // 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() { Add(SMA(Fast)); Add(SMA(Slow)); SetProfitTarget("", CalculationMode.Percent, 20); SetTrailStop("", CalculationMode.Percent, 8, false); CalculateOnBarClose = true; } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { // Condition set 1 if (CrossAbove(SMA(Fast), SMA(Slow), 1)) { EnterLong(DefaultQuantity, ""); }

    #2
    jwhtrades, welcome to NT and NinjaScript - your stop and target % values are likely too high to trigger here, for example 10% would be expressed as 0.1

    Comment


      #3
      Thanks Bertrand. I agree with the profit target being too high. Actually, I'd like to be able to make that variable or in a range that I might change in the strategy for both profit target and trailing stop %. Not a programmer so I'm not sure about how to do that. Is that a range that I select when I set up the Target Profit and Trailing stop criteria in the strategy wizard? Also, I'd like to have a trailing stop tied to the ATR or the lowest low over N periods but not sure how to do that also. Any advice?

      Comment


        #4
        You could add user inputs to your strategy so you can then select the values on the fly and also optimize them - http://www.ninjatrader.com/support/h...rd_screens.htm

        For the ATR trailing stop it would be easiest to use an indicator tracking the stop value and then exiting as this is breached by price, an indicator is located in our sharing that would do this calculation for you.

        Comment


          #5
          Thanks again Bertrand, this should keep me busy today.

          Comment


            #6
            I have struck out trying to get the indicator you mentioned to trigger an exit. I first defined an input "TrailingStop" int default 1 min. 1 Description "ATRTrailing Indicator. Then used my CrossAbove(SMA(Fast),SMA(Slow),1) to EnterLong(100,"") That was fine as it entered the trade as the fast sma crossed above the Slow sma. In Set 2 of Condition builder I used the following:
            ATRTailing(4, 10, 0.005).Lower[0]==ATRTrailing(4, 10, 0.005).Lower[0] I then used the following to exit the trade on what I thought would be triggered by the above condition:
            ExitLong)"TrailingStop", "long Entry") This did not work. Any suggestions?

            Comment


              #7
              I was able to get the ATRTrailing to work in a sense, but it's selling when the price is above my entry my last buy point. I'm still trying to get the ATRTrailing to be a exit trigger if 3 ATRs from last entry. Can't seem to find that toggle that links the ATRTrailing to my last entry point to exit the trade.

              Comment


                #8
                jwhtrades,

                It would be easier for us to help you if you posted what exactly you have for your strategy right now. Can you please export your script and upload it here so we can look at it in full instead of snippets? Thank you.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  ATRTrailing Indicator as Trailing Stop doesn't work

                  Thanks Josh,

                  / This namespace holds all strategies and is required. Do not change it.
                  namespace NinjaTrader.Strategy
                  {
                  /// <summary>
                  /// Fast SMA crosses above Slow SMA with ATRTrailing Indicator as trailing stop
                  /// </summary>
                  [Description("Fast SMA crosses above Slow SMA with ATRTrailing Indicator as trailing stop")]
                  public class SMAcrossAbove : Strategy
                  {
                  #region Variables
                  // Wizard generated variables
                  private int fast = 1; // Default setting for Fast
                  private int slow = 1; // Default setting for Slow
                  private int aTRTrail = 1; // Default setting for ATRTrail
                  // 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()
                  {

                  CalculateOnBarClose = true;
                  }

                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                  // Condition set 1
                  if (CrossAbove(SMA(Fast), SMA(Slow), 1))
                  {
                  EnterLong(100, "");
                  }

                  // Condition set 2
                  if (ATRTrail == ATRTrailing(4, 10, 0.005).Lower[0])
                  {
                  ExitLong("", "");

                  Comment


                    #10
                    jwhtrades,

                    It is very unlikely that ATRTrailing() would actually yield a value exactly equivalent to 1. You should open up the condition to be a range of acceptable ATRTrailing() values.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Thanks

                      Josh,

                      Alas I'm lost. Sorry but no programming experience has me baffled. I've spent the better part of 8 hours experiment over again to arrive here.

                      Bertrand suggest using the downloadable ATRTrailing Indicator which I did. I attempted to make a user input that I used in the condition definition as you see. I'm not sure just how to "open up the condition" to be a range of acceptable ATRTraining values. Is that done at the wizard stage? or is this done when I open the Strategy in NT and set the variables in the strategy there? The wizard doesn't allow for a trailing stop using an indicator as far as I know so that is why I used the ATRTrailing indicator. When during one of my iterinations I did get it to work, it would stop me out ABOVE the buy entry not below as I had hoped.

                      Any suggestions would be greatly appreciated.

                      Thanks,
                      John

                      UOTE=NinjaTrader_Josh;225193]jwhtrades,

                      It is very unlikely that ATRTrailing() would actually yield a value exactly equivalent to 1. You should open up the condition to be a range of acceptable ATRTrailing() values.[/QUOTE]

                      Comment


                        #12
                        John, the exit rule you've setup can't trigger, you compare here the value of 1 to a price based value (i.e. your trailing stop, to visualize what you're working with please just put the indicator on the chart). You would for example check if the Close price crossed the stop, so the position would be exited then.

                        Comment

                        Latest Posts

                        Collapse

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