Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 andrewtrades, Today, 04:57 PM
            1 response
            5 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by chbruno, Today, 04:10 PM
            0 responses
            3 views
            0 likes
            Last Post chbruno
            by chbruno
             
            Started by josh18955, 03-25-2023, 11:16 AM
            6 responses
            436 views
            0 likes
            Last Post Delerium  
            Started by FAQtrader, Today, 03:35 PM
            0 responses
            7 views
            0 likes
            Last Post FAQtrader  
            Started by rocketman7, Today, 09:41 AM
            5 responses
            19 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Working...
            X