Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ATR on older bars

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

    ATR on older bars

    I want to calculate the ATR and store it in a double in OnBarUpdate()

    let's say period of 20

    I want this to ignore let's say the 10 most recent bars.. i.e. calculate the ATR on 20 bars before the latest 10.. so bar 31 to 11 (counting from the rightmost side of the screen)

    How can I do this?

    #2
    Hello NinjaCustomer,

    Thanks for your post.

    Typically when accessing an indicator's current value you would specify the index of [0]. In your example, you want the indicator's value from 10 bars ago.

    So to access a 20 period ATR from 10 bars ago you would use ATR(20)[10].

    Comment


      #3
      that's what I was thinking but my indicator only works when I use [0] if I use another value then it won't display any data, I get this error:
      Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

      Comment


        #4
        Hello NinjaCustomer,

        Thanks for your reply.

        The error is likely caused by trying to access the ATR value 10 bars ago when the indicator has not processed enough bars. You would use a check of the CurrentBar number to verify you have enough bars processed before trying to access 10 ago.

        As the first line in the OnBarUpdate() you would add something like:

        if (CurrentBar < 10) return; // do not proceed until at least 10 bars into the data.

        Reference: https://ninjatrader.com/support/help...currentbar.htm

        Comment


          #5
          yup that solved it, thanks

          Comment

          Latest Posts

          Collapse

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