Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error on implementing ATR Trailing Stop

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

    Error on implementing ATR Trailing Stop

    Here is the code (attached). Where did I make mistake ... argggh .... and I am new to this ..... help please

    Basically, I want ATR Trailing stop with size of 2 times ATR
    Attached Files

    #2
    cowcool, please try moving your Math.Ceiling calculations to the OnBarUpdate() section.

    Comment


      #3
      Thx but the problem is if it is moved to onBarUpdate, the ATR value is constantly changing. I want it to be static , i.e. the ATR value just before a trade is entered.

      Originally posted by NinjaTrader_Bertrand View Post
      cowcool, please try moving your Math.Ceiling calculations to the OnBarUpdate() section.

      Comment


        #4
        Well, then set this variable with the ATR value at the point your trade entry triggers, then it's using this value forward as stop amount.

        Comment


          #5
          Tried to move all the variables to onbarupdate but got error (please see the attached image ) thx


          Originally posted by NinjaTrader_Bertrand View Post
          Well, then set this variable with the ATR value at the point your trade entry triggers, then it's using this value forward as stop amount.
          Attached Files

          Comment


            #6
            This is most likely because you miss the matching curly bracket to end the OnBarUpdate(). However, you can leave the Variables declaration where you had them previously. Then just set the value for your stop amount when your trade triggers -

            Code:
             
            if (Close[0] > Close[5])
            {
            EnterLong(1, "");
            myStopValue = Close[0] - 2 * ATR(10)[0];
            }
            
            if (Position.MarketPosition == MarketPosition.Long)
            SetStopLoss(CalculationMode.Price, myStopValue);
            You can also check into this reference sample -

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by charlesugo_1, 05-26-2026, 05:03 PM
            0 responses
            60 views
            0 likes
            Last Post charlesugo_1  
            Started by DannyP96, 05-18-2026, 02:38 PM
            1 response
            147 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 05-11-2026, 05:56 AM
            0 responses
            162 views
            0 likes
            Last Post CarlTrading  
            Started by CarlTrading, 05-10-2026, 08:12 PM
            0 responses
            97 views
            0 likes
            Last Post CarlTrading  
            Started by Hwop38, 05-04-2026, 07:02 PM
            0 responses
            284 views
            0 likes
            Last Post Hwop38
            by Hwop38
             
            Working...
            X