Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Can't access Times[0][idx] with idx > 0

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

    Can't access Times[0][idx] with idx > 0

    Hey,

    I am using this code:

    Code:
    var times = Times[0];
    Print($"Printing all times from 0 to {times.Count - 1}:");
    for (int i = 0; i < times.Count; i++) {
        Print($"times[{i}] = {times[i]}");
    }
    But get this exception:
    Code:
    DoRpc: got Exception: System.ArgumentOutOfRangeException: 'barsAgo' needed to be between 0 and 5473 but was 1
    Parameter name: barsAgo
    at NinjaTrader.NinjaScript.TimeSeries.get_Item(Int32 barsAgo)

    What am I doing wrong?

    #2
    Where is this code located?
    Inside OnBarUpdate?

    Your code should probably be using CurrentBar, not Count.

    As you know ...
    OnBarUpdate is called every time each bar closes.

    On the first call to OnBarUpdate, there is only one
    bar to look at -- the first bar.

    On the second call to OnBarUpdate, there are 2 bars,
    etc, etc.

    On the first call to OnBarUpdate, the value of CurrentBar
    is 0 but Count is 5473 (I assume, according to the error
    message).

    That's right, on the very first bar, Count is already some
    huge ass number -- it represents all the historical bars
    that have been loaded. But those historical bars are
    processed later one at a time, one bar for each call
    to OnBarUpdate.

    When using BarsAgo indexing to access previous bars,
    your code must respect the value of CurrentBar. Count
    does not do that, it serves a different purpose, and is
    generally not used with BarsAgo indexes.

    You can't look at all 5473 bars (with BarsAgo indexing)
    until OnBarUpdate has been called 5473 times, once for
    each bar.

    In other words,
    Despite the value of Count, you cannot access a bar using
    BarsAgo indexing until that bar has been processed by the
    call to OnBarUpdate. If you do, you'll get an out of range
    error.

    For an even better explanation, attach your script so that
    we have better context.


    Comment


      #3
      Hello dominikstiffel,

      I think you want to be looping from 0 to CurrentBar.

      Try the following:

      for (int i = 0; i < CurrentBar; i++) {
      Print($"Times[0][{i}] = {Times[0][{i}]");
      }​
      Chelsea B.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      560 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      325 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
      547 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      547 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X