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 Hwop38, 05-04-2026, 07:02 PM
            0 responses
            160 views
            0 likes
            Last Post Hwop38
            by Hwop38
             
            Started by CaptainJack, 04-24-2026, 11:07 PM
            0 responses
            308 views
            0 likes
            Last Post CaptainJack  
            Started by Mindset, 04-21-2026, 06:46 AM
            0 responses
            245 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by M4ndoo, 04-20-2026, 05:21 PM
            0 responses
            349 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by M4ndoo, 04-19-2026, 05:54 PM
            0 responses
            179 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Working...
            X