Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Iterating through BarsArray error

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

    Iterating through BarsArray error

    Here is the error I am getting:
    Code:
    Error on calling 'OnStateChange' method: 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.
    It's coming from this block:

    Code:
             ...else if (State == State.DataLoaded){
                    int i = 0;
                    while (i < BarsArray[0].Count){
                        Print(i + Times[1][i].ToString());
                        i++;
    
                    }
    
                }...
    It seems to get through one bar and then the error gets thrown.

    #2
    Hello swcooke,

    Times[1] is the second series. BarsArray[0] is the first series.

    You are attempting to loop through the bars of the first series and get the bar times of the second series without checking that series has a bar there.

    Below are public links to the help guide on BarsArray and Times.
    https://ninjatrader.com/support/help.../barsarray.htm
    https://ninjatrader.com/support/help...ries_times.htm

    Also, I recommend you do any bar access in OnBarUpdate().
    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
    607 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    353 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    105 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    560 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    561 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X