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 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.
    Gaby V.NinjaTrader Customer Service

    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.
        Gaby V.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Balage0922, Today, 07:38 AM
        0 responses
        2 views
        0 likes
        Last Post Balage0922  
        Started by JoMoon2024, Today, 06:56 AM
        0 responses
        6 views
        0 likes
        Last Post JoMoon2024  
        Started by Haiasi, 04-25-2024, 06:53 PM
        2 responses
        19 views
        0 likes
        Last Post Massinisa  
        Started by Creamers, Today, 05:32 AM
        0 responses
        6 views
        0 likes
        Last Post Creamers  
        Started by Segwin, 05-07-2018, 02:15 PM
        12 responses
        1,786 views
        0 likes
        Last Post Leafcutter  
        Working...
        X