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 Mindset, 04-21-2026, 06:46 AM
            0 responses
            63 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by M4ndoo, 04-20-2026, 05:21 PM
            0 responses
            89 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by M4ndoo, 04-19-2026, 05:54 PM
            0 responses
            47 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by cmoran13, 04-16-2026, 01:02 PM
            0 responses
            105 views
            0 likes
            Last Post cmoran13  
            Started by PaulMohn, 04-10-2026, 11:11 AM
            0 responses
            63 views
            0 likes
            Last Post PaulMohn  
            Working...
            X