Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ATR Trailing Stop

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

    ATR Trailing Stop

    I'm sorry if this is the wrong forum for this but I was attempting to add a trailing stop loss to my strategy (for stocks) where I would exit a position if the close < (Entry Price - (ATR(14)*2)). Is this possible? I tried using the the ATR Trailing Stop Indicator but I am not familiar with the parameters involved.

    Also, can I use this type of exit strategy, say, exit position if one the following occurs first;
    1) ATR Trailing Stop
    2) Fast SMA crosses below Slow SMA

    Thanks for your help!

    #2
    jaslinger, yes that would be possible generally with NinjaScript - attached is a simple example from our Strategy Wizard point and click interface showing how this could be done. The strategy will enter on a break of the prior day high and then either exit on your SMA cross down, or if the ATR trail would be hit.

    Hope that helps getting started,
    Attached Files

    Comment


      #3
      Thanks for the files. I've been testing with it but can I do the following in the Strategy Wizard?

      *Stop Loss set at 2 times ATR below entry price for Bull entries*

      or do I need to customized my script?

      The reason I ask is that when I plot the ATR line on my chart, it is occasionally above the price action instead of below. (unless this is correct then forgive me for being naive)

      Thanks!
      Last edited by jaslinger; 02-23-2012, 07:43 PM.

      Comment


        #4
        jaslinger, you're welcome - for that concept you would need customize the script through coding to record the 2 * ATR at the time of execution and then to adjust the stop set to this price level (Execution - the ATR value for a long for example). An example how to work with SetStopLoss could be seen here -

        Comment


          #5
          It's been a while as I've been reading the manual and trying to slowly build my own custom strategy which I've attached here. This strategy is for stocks and performs the following:

          1) The close must be > 200 SMA to enter a trade
          2) Once the 5 SMA crosses above the 20 ema - enter a trade
          Set Stop Loss as Entry Price - (2*ATR(14))
          3) Exit the position if one of the following occurs:
          a) Close < (Entry Price - (2*ATR(14)))
          b) 10 SMA crosses below the 20 ema

          I am able to compile my script which seems to be a good sign but once I back test it and I can look at the charts for one of the instruments (e.g. XLE), I noticed that it is not calculating it correctly. More specifically, I noticed that trades are entered below the 200SMA and the stop loss based on 2*ATR is calculated incorrectly.

          What am I doing wrong? Do I need to reorder my steps?

          Thanks!
          Attached Files
          Last edited by jaslinger; 03-14-2012, 06:47 AM.

          Comment


            #6
            jaslinger, I don't follow why you return of any OnBarUpdate() if the Close > FilterSMA -

            if (Close[0] > SMA(FilterSMA)[0])
            return;

            Isn't this the inverse of what you wanted?

            Comment


              #7
              You are right. I wonder if this might be a better way to accomplish this (I added the &&). I also added rules on setting and resetting my ATR stop loss based on the MarketPosition. What do you think?

              Code:
               
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
              //Reset Stop loss to original value when position is closed
              if (Position.MarketPosition == MarketPosition.Flat)
              {
              SetStopLoss(CalculationMode.Ticks, SLATR)
              }
               
              //If Long Position is open, Set up calculation for Stop Loss ATR Variable (SLATR)
              if (Position.MarketPosition == MarketPosition.Long)
              {
              SLATR=((double)ATR(14)[0]*2);
              }
               
              //Enter Position Condition
              if (Position.MarketPosition == MarketPosition.Flat)
              {
              if (Close[0] > SMA(FilterSMA)[0]
              && CrossAbove(SMA(EnterSMA), EMA(SignalEMA),1))
              {
              EnterLong("EntrySignal");
              }
               
              //Exit Condition 1 Set Stop Loss based on entry price - SLATR
              SetStopLoss(CalculationMode.Ticks,SLATR);
               
              //Exit Condition 2 Set Exit based on crossover of the 10 sma below the 20 ema
              if (CrossBelow(SMA(ExitSMA), EMA(SignalEMA),1))
              {
              ExitLong("ExitLongXover");
              }
               
              }
              I also thought of adding/replacing the following under the Exit Condition1 ATR Stop Loss code:

              Code:
               
              if (Position.MarketPosition == MarketPosition.Long
                 && CrossBelow(Close, SLATR, 1))
                 {
                        ExitLong("ExitATR");
                 }
              Last edited by jaslinger; 03-16-2012, 08:03 AM.

              Comment


                #8
                Here is the latest code though it appears my stop loss is the same as my entry price thus my trades are a wash. Can you help? Thanks!


                Code:
                    protected override void Initialize()
                        {
                            Add(SMA(EnterSMA));
                            Add(SMA(ExitSMA));
                            Add(EMA(SignalEMA));
                            Add(SMA(FilterSMA));
                            
                            
                            CalculateOnBarClose = true;
                        }
                
                        /// <summary>
                        /// Called on each bar update event (incoming tick)
                        /// </summary>
                        protected override void OnBarUpdate()
                        {
                            //Enter Long Position Condition
                            if(Close[0] > SMA(FilterSMA)[0]
                            && CrossAbove(SMA(EnterSMA), EMA(SignalEMA),1))
                                {
                                    EnterLong(DefaultQuantity, "EnterLong");
                                }    
                            //If long position is open, set Stop Loss
                            if (Position.MarketPosition == MarketPosition.Long)
                            {
                                SetTrailStop(CalculationMode.Ticks, (double)(ATR(14)[1]*2));
                            }
                                    
                            //Exit Condition #2 Set Stop Loss based on Cross
                            if (Position.MarketPosition == MarketPosition.Long
                                && CrossBelow(SMA(ExitSMA), EMA(SignalEMA),1));
                            {
                                ExitLong("ExitXover");
                            }
                            
                                    
                            
                        }

                Comment


                  #9
                  The issue likely is here -

                  SetTrailStop(CalculationMode.Ticks, (double)(ATR(14)[1]*2));

                  The trailstop amount would be expressed in ticks, so by setting it to 2 times the ATR that's likely way to tight and it would trigger immediately.

                  Just print the ATR value you calculate to see which value gets passed to the SetTrailStop call, then change for example to a trail of 10 ticks and it should work better.

                  Comment


                    #10
                    Thanks for your help. I tried your suggestion and I think another problem is that I am using this strategy on equities and the CalculationMode is in Ticks: Am I off base? Do I need to use the following to get a more accurate calculation:
                    TickSize * Instrument.MasterInstrument.PointValue;

                    Here is my code now:

                    Code:
                    protected override void OnBarUpdate()
                            {
                                // Enter Long Condition
                                if (Close[0] > SMA(SMA200)[0]
                                    && CrossAbove(SMA(SMA5), EMA(EMA20), 1))
                                {
                                    EnterLong(DefaultQuantity, "EnterLong");
                                }
                    
                                // Exit Condition 1 - Crossover
                                if (CrossBelow(SMA(SMA10), EMA(EMA20), 1))
                                {
                                    ExitLong("ExitLongXover", "");
                                }
                                if (Position.MarketPosition == MarketPosition.Long)
                                {
                                //Print ATRStopLoss
                                double value = Position.AvgPrice-(ATR(14)[1]*2);
                                Print ("The Current value is " + value.ToString());
                                SetStopLoss(CalculationMode.Ticks, (Position.AvgPrice-(ATR(14)[1]*2)));
                                }
                                
                            }

                    Comment


                      #11
                      jaslinger, you calculate the trail stop double value now directly of the avg price and the atr value - to trail by this price based value, use the price mode of the stop and not the ticks mode then.

                      Comment

                      Latest Posts

                      Collapse

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