Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Declaring a simple Array ( Length )

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

    Declaring a simple Array ( Length )

    Hi Ninjas,

    I want to declare a simple array length, through the standard syntax:

    double[] myfirstarray = new double[10];

    This declaration goes under Variable region, but here the question:

    How can I declare that array length with an integer number not just changing the code internally, but rather when I run it, let's say running the Strategy Analyzer for a backtest. When I try to declare this array with a previous integer variable above, I always get the obvious error.

    What I'd like is declaring the array length from outside of the code.

    Thanks for your help

    #2
    Hi pstrusi,

    If you are wanting to start with a new array you can change the size at that time.

    In #region Variables
    double[] myfirstarray = new double[10];
    int arraySizeVariable = 20;

    In OnBarUpdate() or OnStartUp() (or wherever you want to set the array size)
    if (/* conditions to set array size */)
    myfirstarray = new double[arraySizeVariable];


    Or you can use a list that is dynamically expanding.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you so much Chelsea, I didn't think the possibility of OnStarUp()


      Have a nice weekend and best regards

      Comment


        #4
        Chelsea, I've been trying your suggestion but is not allowed to change the initial declaration of the array, this is the error message:

        Error on calling 'OnBarUpdate' method for strategy 'FFFS/c8e4482eaaaa49238463f1e35b50f195': Index was outside the bounds of the array.


        When I let just the initial declaration, all runs ok

        Comment


          #5
          Hi pstrusi,

          Your error will be due to some other logic in your code.

          Attached is an example to demonstrate that this works.
          Attached Files
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Thanks for the example, despite the same you told before it's very helpful. I've found the problem, yes it was in the logic, apparently I had to add 2 to the variable "myarrayinteger" , a little part in the logic requires this, I had overlooked it.

            Thanks again

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by kinfxhk, 07-14-2026, 09:39 AM
            0 responses
            125 views
            0 likes
            Last Post kinfxhk
            by kinfxhk
             
            Started by kinfxhk, 07-13-2026, 10:18 AM
            0 responses
            105 views
            0 likes
            Last Post kinfxhk
            by kinfxhk
             
            Started by kinfxhk, 07-13-2026, 09:50 AM
            0 responses
            84 views
            0 likes
            Last Post kinfxhk
            by kinfxhk
             
            Started by kinfxhk, 07-13-2026, 07:21 AM
            0 responses
            104 views
            0 likes
            Last Post kinfxhk
            by kinfxhk
             
            Started by kinfxhk, 07-11-2026, 02:11 AM
            0 responses
            83 views
            0 likes
            Last Post kinfxhk
            by kinfxhk
             
            Working...
            X