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 CaptainJack, 05-29-2026, 05:09 AM
            0 responses
            475 views
            0 likes
            Last Post CaptainJack  
            Started by CaptainJack, 05-29-2026, 12:02 AM
            0 responses
            315 views
            0 likes
            Last Post CaptainJack  
            Started by charlesugo_1, 05-26-2026, 05:03 PM
            0 responses
            253 views
            1 like
            Last Post charlesugo_1  
            Started by DannyP96, 05-18-2026, 02:38 PM
            1 response
            340 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 05-11-2026, 05:56 AM
            0 responses
            305 views
            0 likes
            Last Post CarlTrading  
            Working...
            X