Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ATR indicator prior values not accessible

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

    ATR indicator prior values not accessible

    i am trying to access prior ATR values based on a given period but the code errors out after bar 15 even though there are 1870 bars in the ATR series. below is the code and output window results. what does not make sense is the loop outputs 16 elements but it has a problem at bar 15. very confused.

    protected override void OnBarUpdate() {
    if(CurrentBar > 14)
    return;
    var count = ATR(14).Count;
    Print(string.Format("ATR(14).Count: {0}", count));
    for (int i = 0; i < count; i++) {
    Print(string.Format("ATR[{0}]: {1}", i, ATR(14)[i]));
    }
    }

    ATR(14).Count: 1870
    ATR[0]: 3.54136297376093
    ATR[1]: 3.56377551020408
    ATR[2]: 3.60714285714286
    ATR[3]: 3.55769230769231
    ATR[4]: 3.58333333333333
    ATR[5]: 3.68181818181818
    ATR[6]: 3.85
    ATR[7]: 3.91666666666667
    ATR[8]: 4.09375
    ATR[9]: 4.28571428571429
    ATR[10]: 4.20833333333333
    ATR[11]: 4.2
    ATR[12]: 4.625
    ATR[13]: 4.83333333333333
    ATR[14]: 5.625
    ATR[15]: 6.25

    Error on calling 'OnBarUpdate' method on bar 15: 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.
    ​​

    #2
    Hello love2code2trade,

    Thank you for your post.

    Your CurrentBar check is causing the script to stop after the 14th bar.

    Code:
    if(CurrentBar > 14)
    
    return;

    This says "if CurrentBar is greater than 14, then return". To resolve, you can change this to:

    Code:
    if(CurrentBar < 14)
    
    return;
    Please let me know if you have any other questions.

    Comment


      #3
      hi gaby,
      thankyou for your reply. i have tried different permutations of your suggestion without success. i still get the same error. what i am trying to achieve is to be able to iterate over the the prior ATR values for each call to OnBarUpdate.
      Last edited by love2code2trade; 11-02-2023, 09:55 AM.

      Comment


        #4
        Hello,

        Thank you for your response.

        Instead of looping to 'count', try looping to CurrentBar.

        CurrentBar - https://ninjatrader.com/support/help...currentbar.htm


        Code:
        for (int i = 0; i < CurrentBar; i++) {
        
        Print(string.Format("ATR[{0}]: {1}", i, ATR(14)[i]));
        
        }

        Please let me know if I can assist further.

        Comment

        Latest Posts

        Collapse

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