Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ATR Atr.ATR()[] Accessing index with value that is invalid

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

    ATR Atr.ATR()[] Accessing index with value that is invalid

    Hi All,
    I have a class that I need to pass an ATR to. In that class I am working with the ATR value from 0 bars ago, and also with one that aligns with my entry which occurred x bars ago.

    I get an error when I try and pass this, so I've stripped everything down, and Printing in OnBarUpdate debug. Code looks like this:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    class ArgyleSim : Strategy
    {
    private ATR Atr = new ATR();

    protected override void OnBarUpdate()
    {
    if (CurrentBar > 14) // <-- presence/absence of this does not affect output (apart from CurrentBar print)
    {
    Print("CurBar: " + CurrentBar);
    Print("ATR[0]: " + ATR(14)[0]);
    Print("ATR[0]: " + Atr.ATR(14)[0]);
    Print("62");
    }
    }
    }
    }

    I get the following output:
    CurBar: 15
    ATR[0]: 1.08263483965015
    Strategy 'ArgyleSim': 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.

    I am confident I'm lacking an understanding of how that ATR() (and the Indicator()) classes work. Just ATR(14)[0] yields a result, while Atr.ATR(14)]0] creates above error. Can someone please point me in the direction of how to better understand this so I can correctly pass this into my class. Thank you very much for your help in advance.

    Kind Regards,
    Nathan.

    #2
    Hello NateG0310,

    Thanks for your post.

    private ATR Atr = new ATR();

    Indicators cannot be instantiated here. You must instantiate indicators in OnStateChange() when the State is State.DataLoaded. Also, the new keyword cannot be used.

    Something you could do is use the Strategy Builder to set up a condition using the ATR indicator and click the 'View code' button to look at the generated code.

    See this forum thread for more information about learning NinjaScript and using the Strategy Builder: https://ninjatrader.com/support/foru...040#post786040

    Also, see the sample code below for how to instantiate an indicator in State.DataLoaded.

    Code:
    private ATR Atr;
    
    protected override void OnStateChange()
    {
    else if (State == State.DataLoaded)
    {
    Atr = ATR(14);
    }
    
    protected override void OnBarUpdate()
    {
    Print(Atr[0]);
    }
    Let us know if we may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Something you could do is use the Strategy Builder to set up a condition using the ATR indicator and click the 'View code' button to look at the generated code.
      ^ Brandon, this is brilliant!!

      So from your sample code above, it sounds like I simply declare the variable where I am currently trying to instantiate it. Would it be correct verbiage to say that takes place "at the class level?" Largely self-taught, so I'm a little clueless on lingo and best-practices. Anway, next I would go down into OnStateChange() and that's where the actual instantiation takes place.

      One more thing: I clicked the link you provided, and it looks like there are some resources. Do you have any other recommendation for tutorials I should go through to learn in's and out's like this? I find myself doing a lot of googling and forum stalking to learn. It would be nice if there was almost a classroom or video learning structure I could listen to while I drive. Just curious.

      Thank you again for your help. Saved me hours of wracking my brain as I never would have figured that one out.

      Nathan.

      Comment


        #4
        Hello NateG0310,

        Thanks for your note.

        Would it be correct verbiage to say that takes place "at the class level?"

        Yes, it would be correct to say that the ATR variable is declared at the class level.

        Do you have any other recommendation for tutorials I should go through to learn in's and out's like this?

        You could find tutorials about developing indicators and strategies in the Educational Resources section of the help guide linked below. Note that most of the supported NinjaScript code could be found in the help guide.

        Educational Resources: https://ninjatrader.com/support/help..._resources.htm

        Here is a link to our publicly available training videos, 'Strategy Builder 301' and 'NinjaScript Editor 401', for you to view at your own convenience.

        Strategy Builder 301 — https://www.youtube.com/watch?v=_KQF2Sv27oE&t=13s

        NinjaScript Editor 401 - https://youtu.be/H7aDpWoWUQs?list=PL...We0Nf&index=14

        If you are new to C#, to get a basic foundation for the concepts and syntax used in NinjaScript I would recommend this section of articles in our help guide first:
        https://ninjatrader.com/support/help...g_concepts.htm

        And the MSDN (Microsft Developers Network) C# Language Reference.
        https://ninjatrader.com/support/help...erence_wip.htm

        Let us know if we may assist further.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        54 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        130 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        72 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        44 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        49 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X