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 CarlTrading, 03-31-2026, 09:41 PM
            1 response
            142 views
            1 like
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 04-01-2026, 02:41 AM
            0 responses
            81 views
            1 like
            Last Post CarlTrading  
            Started by CaptainJack, 03-31-2026, 11:44 PM
            0 responses
            125 views
            2 likes
            Last Post CaptainJack  
            Started by CarlTrading, 03-30-2026, 11:51 AM
            0 responses
            119 views
            1 like
            Last Post CarlTrading  
            Started by CarlTrading, 03-30-2026, 11:48 AM
            0 responses
            98 views
            0 likes
            Last Post CarlTrading  
            Working...
            X