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 CarlTrading, 03-31-2026, 09:41 PM
            1 response
            67 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 04-01-2026, 02:41 AM
            0 responses
            36 views
            0 likes
            Last Post CarlTrading  
            Started by CaptainJack, 03-31-2026, 11:44 PM
            0 responses
            60 views
            1 like
            Last Post CaptainJack  
            Started by CarlTrading, 03-30-2026, 11:51 AM
            0 responses
            62 views
            0 likes
            Last Post CarlTrading  
            Started by CarlTrading, 03-30-2026, 11:48 AM
            0 responses
            53 views
            0 likes
            Last Post CarlTrading  
            Working...
            X