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 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 wzgy0920, 04-20-2024, 06:09 PM
        2 responses
        26 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, Yesterday, 09:53 PM
        2 responses
        49 views
        0 likes
        Last Post wzgy0920  
        Started by Kensonprib, 04-28-2021, 10:11 AM
        5 responses
        192 views
        0 likes
        Last Post Hasadafa  
        Started by GussJ, 03-04-2020, 03:11 PM
        11 responses
        3,234 views
        0 likes
        Last Post xiinteractive  
        Working...
        X