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

multidimensional array - initialize and declare only once

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

    multidimensional array - initialize and declare only once

    I need to use a multidimensional array to store certain values and calculations that are done ONLY ONCE at beginning, when the automated strategy is launched. The challenge is how to initialize and declare the array once without having to repeat it each iteration of the "protected override void OnBarUpdate()" loop. Currently, my strategy does it in every loop through. Since I have "CalculateOnBarClose = false;" that is a lot of extra horsepower wasted. Kindly advise what I should do. Thank you.

    #2
    Hello wolfgang239,

    Thank you for your post.

    If you have something that only needs to be done once on start up you can use the OnStartUp() method: http://www.ninjatrader.com/support/h.../onstartup.htm

    Comment


      #3
      Thank you Patrick, but that will not work because you cannot initialize and declare an Array in the OnStartUp() and have it accessible in the rest of the program. The error message is:

      The name 'theArrayName' does not exist in the current context.

      This error message will be displayed corresponding to a line in the body of loop
      "OnBarUpdate()"

      Comment


        #4
        Have you considered using

        if ( CurrentBar == 1 ) // do stuff

        Comment


          #5
          I have tried:

          if ( Bars.FirstBarOfSession )

          Does not work

          Comment


            #6
            The following snippet shows that you cannot initialize an Array in OnStartUp() and have it accessible in the main section of the program:

            #region Using declarations
            using System;
            using System.ComponentModel;
            using System.Diagnostics;
            using System.Drawing;
            using System.Drawing.Drawing2D;
            using System.Xml.Serialization;
            using NinjaTrader.Cbi;
            using NinjaTrader.Data;
            using NinjaTrader.Indicator;
            using NinjaTrader.Gui.Chart;
            using NinjaTrader.Strategy;
            #endregion

            namespace NinjaTrader.Strategy
            {
            [Description("")]
            public class TestArray2 : Strategy
            {
            #region Variables
            #endregion

            protected override void Initialize()
            { CalculateOnBarClose = true;
            }

            protected override void OnStartUp()
            { ClearOutputWindow();
            int[] theArrayFour = new int[5] { 555, 666, 777, 888, 999 };
            for (int i = 0; i < theArrayFour.Length; i++)
            {
            Print("theArrayFour(" + i + ") " + theArrayFour[i]); //WORKS FINE!
            }
            }

            protected override void OnBarUpdate()
            {
            // the following produces error: The name 'theArrayFour' does not exist in the current context
            // for (int i = 0; i < theArrayFour.Length; i++)
            // {
            // Print("Inside the OnBarUpdate() loop - theArrayFour(" + i + ") " + theArrayFour[i]);
            // }
            }
            }
            }

            Comment


              #7
              Wolfgang,

              Please declare the new array in the variables section

              Code:
              #region variables
              int[] myArray = new int[5];  // simple array
              int[ , , , ] my multidimArray = new int [10, 10, 10, 10]; /four-dimensional array
              #endregion
              then initialize the array in OnStartup().

              Alternatively you can also use lists and add values to the list in OnStartUp() or OnBarUpdate() as needed.

              Comment


                #8
                Brilliant! Thank you Harry. Works great.
                For anyone that may be having a similar problem, here is a simple snippet that shows Harry's solution. You can access the array in the main loop but it needs to be declared in the Variables region and initialized in the OnStartUp()

                #region Using declarations
                using System;
                using System.ComponentModel;
                using NinjaTrader.Cbi;
                using NinjaTrader.Strategy;
                #endregion

                namespace NinjaTrader.Strategy
                {
                [Description("")]
                public class TestArray4 : Strategy
                {
                #region Variables
                int[,] myMultidimArray = new int [10,4]; //two-dimensional array
                #endregion

                protected override void Initialize()
                {
                CalculateOnBarClose = true;
                }
                protected override void OnStartUp()
                { ClearOutputWindow();
                for (int i = 0; i < myMultidimArray.GetLength(0); i++)
                {
                myMultidimArray [i,0] = 100;
                myMultidimArray [i,1] = 200;
                myMultidimArray [i,2] = 300;
                myMultidimArray [i,3] = 400;
                Print("INITIALIZING index: " + i + "\t" + myMultidimArray[i,0] + "\t" + myMultidimArray[i,1] + "\t" + myMultidimArray[i,2] + "\t" + myMultidimArray[i,3]);
                }
                Print("finished INITIALIZING");
                Print("");
                for (int i = 0; i < myMultidimArray.GetLength(0); i++)
                {
                Print("in OnStartUp section index: " + i + "\t" + myMultidimArray[i,0] + "\t" + myMultidimArray[i,1] + "\t" + myMultidimArray[i,2] + "\t" + myMultidimArray[i,3]);
                }
                Print("finished OnStartUp section");
                Print("");
                }

                protected override void OnBarUpdate()
                {
                for (int i = 0; i < myMultidimArray.GetLength(0); i++)
                {
                myMultidimArray[i,0]++;
                myMultidimArray[i,1]++;
                myMultidimArray[i,2]++;
                myMultidimArray[i,3]++;
                Print("in OnBarUpdate() loop: " + i + myMultidimArray[i,0] + "\t" + myMultidimArray[i,1] + "\t" + myMultidimArray[i,2] + "\t" + myMultidimArray[i,3]);
                }
                Print("");Print("");
                }
                }
                }

                Comment


                  #9
                  Originally posted by wolfgang239 View Post
                  The following snippet shows that you cannot initialize an Array in OnStartUp() and have it accessible in the main section of the program:

                  #region Using declarations
                  using System;
                  using System.ComponentModel;
                  using System.Diagnostics;
                  using System.Drawing;
                  using System.Drawing.Drawing2D;
                  using System.Xml.Serialization;
                  using NinjaTrader.Cbi;
                  using NinjaTrader.Data;
                  using NinjaTrader.Indicator;
                  using NinjaTrader.Gui.Chart;
                  using NinjaTrader.Strategy;
                  #endregion

                  namespace NinjaTrader.Strategy
                  {
                  [Description("")]
                  public class TestArray2 : Strategy
                  {
                  #region Variables
                  #endregion

                  protected override void Initialize()
                  { CalculateOnBarClose = true;
                  }

                  protected override void OnStartUp()
                  { ClearOutputWindow();
                  int[] theArrayFour = new int[5] { 555, 666, 777, 888, 999 };
                  for (int i = 0; i < theArrayFour.Length; i++)
                  {
                  Print("theArrayFour(" + i + ") " + theArrayFour[i]); //WORKS FINE!
                  }
                  }

                  protected override void OnBarUpdate()
                  {
                  // the following produces error: The name 'theArrayFour' does not exist in the current context
                  // for (int i = 0; i < theArrayFour.Length; i++)
                  // {
                  // Print("Inside the OnBarUpdate() loop - theArrayFour(" + i + ") " + theArrayFour[i]);
                  // }
                  }
                  }
                  }
                  Correct. That is a scope issue. Declare your Array with class scope.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by fitspressoburnfat, Today, 04:25 AM
                  0 responses
                  2 views
                  0 likes
                  Last Post fitspressoburnfat  
                  Started by Skifree, Today, 03:41 AM
                  1 response
                  4 views
                  0 likes
                  Last Post Skifree
                  by Skifree
                   
                  Started by usazencort, Today, 01:16 AM
                  0 responses
                  1 view
                  0 likes
                  Last Post usazencort  
                  Started by kaywai, 09-01-2023, 08:44 PM
                  5 responses
                  603 views
                  0 likes
                  Last Post NinjaTrader_Jason  
                  Started by xiinteractive, 04-09-2024, 08:08 AM
                  6 responses
                  23 views
                  0 likes
                  Last Post xiinteractive  
                  Working...
                  X