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

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].
    Paul H.NinjaTrader Customer Service

    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
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          yup that solved it, thanks

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by bmartz, 03-12-2024, 06:12 AM
          4 responses
          32 views
          0 likes
          Last Post bmartz
          by bmartz
           
          Started by Aviram Y, Today, 05:29 AM
          4 responses
          12 views
          0 likes
          Last Post Aviram Y  
          Started by algospoke, 04-17-2024, 06:40 PM
          3 responses
          28 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by gentlebenthebear, Today, 01:30 AM
          1 response
          8 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by cls71, Today, 04:45 AM
          1 response
          7 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Working...
          X