Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trailing stop using ATR

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

    Trailing stop using ATR

    Hi,

    I have been modifying the 20/50 SMA cross over so I can use the ATR or ATR multiple as the distance for my stop loss. It works fine apart from the ATR variable doesnt read into the script.

    When I hard code a value in the Trailing stop method it works fine but when I use the ATR forumula and assign it to a variable and put that variable into the Trailing Stop method it doesnt work at all - the trailing stop method reads it as 1 or 0.

    Heres my code. I have tried putting the code in both the initialize and onBarUpdate method but neither work.

    Code:
            protected override void Initialize()
            {
                SMA(Fast).Plots[0].Pen.Color = Color.Red;
                SMA(Slow).Plots[0].Pen.Color = Color.Green;
    
                Add(SMA(Fast));
                Add(SMA(Slow));
    
                CalculateOnBarClose    = true;
            //Trailing stop and ATR code    
            double value = ATR(14)[0];
            double TrailingSL = High[0] - value;
                
            SetTrailStop(CalculationMode.Ticks, TrailingSL); 
                
             }
    Any help would be greatly appreciated.

    Thanks

    Mike

    #2
    Mike,

    You will have to migrate that code block into OnBarUpdate(). In Initialize(), it does not have any concept of bars and does not know the value of ATR from there.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Hi,

      I've put it in the OnBarUpdate() method now but 99% of all my trades are being entered and exited on the same day (when back testing). I have some logic error I think. The following is what I want to do:

      I am using a 20/50SMA cross over to enter long or short then I want to set a trailing stop loss with the amount being equal to the 14day ATR or a multiple of it and from the high of the current bar - eg High[0]. So for example if the stock trends up each time it makes a new high my stop should move up and be equal to Today's High - 1*ATR(14)

      I think my code is almost right but its not working. Here is the method:

      Code:
      /// <summary>
              /// Called on each bar update event (incoming tick).
              /// </summary>
              protected override void OnBarUpdate()
              {
                  //ENTRY
                  if (CrossAbove(SMA(Fast), SMA(Slow), 1))
                      EnterLong();
                  else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
                      EnterShort();
                  
              //ATR code - value variable is = 14 day ATR value    
              double value = ATR(14)[0];
             
             //Trailing stop is equal to high of the current day 
             //(i.e. BAR 0 days ago) - 14 day ATR value
      
              double TrailingSL = High[0] - value;
                  
              SetTrailStop(CalculationMode.Ticks, TrailingSL); 
                  
               }
      Does anyone see any mistake that jumps out at them - sure has me puzzled.

      Thanks in advance for any help!!!

      Mike

      Comment


        #4
        You are submitting exact price levels for your stop loss. As such, you should use CalculationMode.Price not .Ticks. Once submitted, you should not amend the order any further. You need to add logic to prevent that while you are already trading.
        Josh P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

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