Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Jagged array

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

    Jagged array

    Hello!
    I am receiving the following error message:
    Error on calling 'OnBarUpdate' method on bar 500: Index was outside the bounds of the array.
    I have the following code:
    HTML Code:
    #region Variables
     private double [][] myArray = new double[3][];  // Declaration of the jagged array
    #endregion
    
    protected override void OnBarUpdate()
    {
       if(CurrentBar < 500) return;
    
       for(int i = 0; i < myArray.Length; i++)
       {
            for(int j = 0; j < 25; j++)
            {
                myArray[i] = new double[j];     // set size of jagged array elements
                myArray[i][j] = Close[j];            // initialize the arrays
            }
       }
    }
    I have a jagged array with three simple dimensional arrays. I set the length of each simple dimesional array to 25. Even considering 75 (3*25) items, I do not approch 500. Why am I receiving the above error message?

    Many thanks in advance!
    Last edited by Stanfillirenfro; 06-13-2022, 01:24 PM.

    #2
    Hello Stanfillirenfro,

    Thank you for your note.

    This is more of a general C# question than a NinjaScript question, and somewhat out of the scope of our support, but with jagged arrays you have to specify the size of the jagged array elements and you are giving that a 0 size on the first iteration through the inner loop. I'd recommend taking a look at this publicly available page from Microsoft's help guide:

    Store multiple variables of the same type in an array data structure in C#. Declare an array by specifying a type or specify Object to store any type.


    Please let us know if we may be of further assistance to you.

    Comment


      #3
      Stanfillirenfro You may want to add some Print statements to trace the loop execution, noting the indices and bar numbers. This should at least identify the exact point in the loop where the error is occurring. Also, if jagged arrays are not required for a specific reason, you may want to consider multi-dimensional arrays instead?

      Thanks.
      Multi-Dimensional Managed Trading
      jeronymite
      NinjaTrader Ecosystem Vendor - Mizpah Software

      Comment


        #4
        Many thanks Jeronymite and Kate for your answers.

        I can see the error messge because I have added the print function to the code. Only what I have from the output window is the error message above. Is there a way to see the exact point where the error occurs?

        From what I know, the multidimesional array sets the length of the array already at the time it is declared. I am using the jagged array to control the lentgh myself.

        @Kate, thanks for the link. I had this documentation already and as you sent it to me, I was reading it already, but I do not solve that problem yet.

        I am using the print function as followed:
        HTML Code:
        Print("myArray" + " : " + myArray[i][j] + " : " + Time[0]);
        No amelioration until now.

        Many thanks!

        Comment


          #5
          Stanfillirenfro I suspect the Print statement you have will not help much as it still references the actual array. May I suggest something like the following:
          Code:
          protected override void OnBarUpdate()
          {
             if(CurrentBar < 500) return;
             Print("myArray.Length = " + myArray.Length + ", CurrentBar = " + CurrentBar);
          
             for(int i = 0; i < myArray.Length; i++)
             {
                  for(int j = 0; j < 25; j++)
                  {
                      Print("i,j = " + i + "," + j);
                      myArray[i] = new double[j];     // set size of jagged array elements
                      myArray[i][j] = Close[j];            // initialize the arrays
                  }
             }
          }
          Thanks.
          Multi-Dimensional Managed Trading
          jeronymite
          NinjaTrader Ecosystem Vendor - Mizpah Software

          Comment


            #6
            Hello Stanfillirenfro,

            Thank you for your reply.

            As for your question regarding how you can see the exact point where the error occurs, this is where debugging using Visual Studio would come in handy. Here's a link to our help guide explaining how to set it up for debugging:



            If you attach the debugger and then run the script, it should pop up showing exactly where in the code the error occurs.

            Aside from that, I'd try jeronymite's suggestion above!

            Please let us know if we may be of further assistance to you.

            Comment


              #7
              Hello @Kate, Hello jeronymite,

              many thanks for your help and advices.

              @Kate, I have failed to replicate the complete debugging process as specified in the link. The step 4 could not be completed although I have repeated several times. The procees took sometimes more than two hours and finally, I realized that the versions of Visual Studio I have downloaded (free download) were not compatible with Ninjatrader. I finally gave up.

              Nevertheless, I could resolve the problem using Series<T>.

              jeronymite, thanks gain for the tip on using the Print() function. This was new for me.

              Many thanks again.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              661 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              375 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              110 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              574 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              580 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X