Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

You are accessing an index with a value that is invalid since it is out-of-range

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

    You are accessing an index with a value that is invalid since it is out-of-range

    Hello!

    I am facing a recurrent problem again. This issue occures in my case mostly when I use the loop.
    This is the contect this time:

    I set my period at 100.

    protected override void OnBarUpdate()
    {
    if (CurrentBar < Period)
    return;

    List<double> myArray = new List<double> ();

    for(int i = Period; i > 0; i--)
    {
    myArray.Add(High[i]);

    }
    for(int j = 0; j < myArray.Count; j++)
    {

    Print("***** Bar " + myArray[j] + " Values: *****");
    }
    }

    The output prints me the error message " "You are accessing an index with a value that is invalid since it is out-of-range."

    I changed the condition (filter to : if (CurrentBar < 0) return. The same error message appears.
    Even by setting the period to 1000 I can not solve the problem.

    What could be the reason of the failure?

    Thanks

    #2
    Hello, thanks for writing in.

    I plugged in the code to my test script and it is working fine on my end, see the attached file. If you would like to test it, place the file within Documents\NinjaTrader 8\bin\Custom\Indicators and compile. Another way to solve the issue would be to add Prints to see what FOR loop its failing on e.g.

    Code:
    int Period = 10;
    protected override void OnBarUpdate()
    {
        if (CurrentBar < Period)
            return;
    
        List<double> myArray = new List<double>();
    
        for(int i = Period; i > 0; i--)
        {
            Print("Adding to myArray");
            myArray.Add(High[i]);
        }
        Print("First loop done");
        for(int j = 0; j < myArray.Count; j++)
        {
            Print("Printing Value");
            Print("***** Bar " + myArray[j] + " Values: *****");
        }
        Print("Second Loop Done");
    }
    You may also use Visual Studio to attach to the NinjaTrader Process and see exactly where it fails:
    https://ninjatrader.com/support/help..._debugging.htm

    Please let me know if I can assist any further.
    Attached Files

    Comment


      #3
      Thanks ChrisL

      It is working fine!

      Many thanks again ChrisL

      Comment


        #4
        Hello ChrisL
        The output is giving me the value of the bar Nr. 2 while counting from the bar in progress.
        Is there a way to have the value of the bar Nr. 1? Period - 1 does not solve the problem.

        Thanks!

        Comment


          #5
          Hello, thanks for your reply.

          You would need to modify the loop to go through Period-1 to 0 e.g. for(int i = Period-1; i >= 0; i--) That will fill in Period bars, including the latest bar.

          Best regards.

          Comment


            #6
            Many thanks ChrisL!

            Great!

            Comment

            Latest Posts

            Collapse

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