• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Setting an internal Array as variable

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

    Setting an internal Array as variable

    Hi Ninjas,

    I have the need to set up an internal "Array" as variable to store certain Values. But it's not the typical DataSeries Class which is always linked exactly to the numbers of Bars. What I need it's variable to store internal calculations to access later in other loops, so since I can't find anything regards to this in the NT Help Guide, I was search in the NET and I find this, so I'd like to know if this could work correctly ( not like NT DataSeries ) or either there's a better way to do it.

    First declaring the variable:

    double[] balance;

    Second Initialize the Array:

    double[] balance = new double[10];


    Hoping to get any hint here

    Thanks

    #2
    Hello,

    Thank you for the question.

    NinjaTrader does not include this type of information in the help guide because this is just standard C# syntax, what you have shown would be a double array which would be a valid type in C# and can be used in NinjaScript.

    Arrays are good for things that you know the number of, or the amount of index locations to create.

    Another type would be the List type, this does not need a set index and performs very similar to a DataSeries except this can have any number of items and is not synced with the Bars on the chart.

    I would recommend reviewing this page as it has a lot of great information. http://www.techotopia.com/index.php/C_Sharp_Essentials

    I am unsure on your C# experience, but this page has some information for everyone. If you are a new C# coder, I would recommend reading sections 5 - 9 and then 14 - 15, otherwise if you are already up to speed with the lower level syntax, go ahead and check out sections 14 and 15. These would be the best methods to store lists or arrays of data in C#, I highly recommend using Lists as you do not need to specify a length before hand.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks a lot Jesse, I'll try all of them to see which is the best. By the way, thanks a lot for that helpful guide.

      Best regards
      Last edited by pstrusi; 04-16-2015, 12:02 PM.

      Comment


        #4
        Jesse, a technical question:

        List Types variables performance faster than a typical array variable?

        I´ll always know the length of the array, so I can use both of them, but I have still that doubt

        Thanks

        Comment


          #5
          Hello,

          Both should be nearly identical in efficiency as far as what you would be able to see.

          Lists in C# have basically taken the place of Array because of their multiple uses along with being able to use LINQ to query the list much like it is a database. This means you can do Find, Contains and other search operations on the list removing the need for looping, in this sense they are more efficient.

          While arrays still serve a purpose and will work, you need to know the exact index count before hand and are extremely specific on their use.

          In Object oriented programming, lists can be a list of specific objects like a string, int, double etc. or they can also be a list of a class meaning that it can have sub objects.

          Also lists do not need a index defined, you can add as many entries as you need or remove specific entries. You can also add to a specific index in the middle of the list rather than always appending to the end of the array.

          Lists are by far more flexible, if you plan on working with C# or NinjaScript i would highly recommend learning lists as they are much more flexible and absolutely necessary for higher level programming when you start working with objects, IOrders for example.

          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Yes, Jesse, one little thing more:

            I know how to initialize an Arraylist, but I don't know how to declare it as variable, is there any special place or "#region Variables" is ok?

            #region Variables
            ?????????

            Initialize:
            ArrayList Balance1 = new ArrayList();

            Comment


              #7
              Hello,

              you have the correct syntax you just need to move it into the correct scope.

              If you are looking at the script I believe you would have this based on the last message:

              Code:
               public class TestStrat : Strategy
                  {
                      protected override void Initialize()
                      {
              			ArrayList Balance1 = new ArrayList();
                      }
              }
              To make the variable Balance1 into a variable that can be used in OnBarUpdate and Initialize you would just need to move it out of Initialize or:

              Code:
               public class TestStrat : Strategy
                  {
                      ArrayList Balance1 = new ArrayList();
              
                      protected override void Initialize()
                      {
              			
                      }
              }
              I bolded the Class, anything inside of the Class will be visible by the other methods inside the { and }

              If you declare a variable inside of the Initialize method, it can only be used inside of Initialize.

              Also don't forget the using statement if you have not already added it, ArrayList is found in System.Collections, or you would need to add to the top of the script:

              Code:
              using System.Collections;
              I look forward of being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Jesse, I appreciate a lot all of your help but I have still some issues.

                1. I put "ArrayList Balance1 = new ArrayList();" out of Initialize to put it before like you suggest:

                public class TestStrat : Strategy
                {
                ArrayList Balance1 = new ArrayList();

                protected override void Initialize()
                {

                }
                }
                but it doesn't work like that. What I need is where and how to declare it as any other variable, generally this must be done within #region variables

                Comment


                  #9
                  Ok, I've made a progress, I wasn't using correctly "using System.Collections;", now it's done and at least it's compiling, now I have 2 questions:

                  1. Why did you say that I must not use that Listarray variable "Balance1" within Initialize, when all of the rest of variables are always "initialized" for later use OnBarUpdate?
                  As you say well, that array variable "Balance1" must be used within OnBarUpdate, thus it have to be properly declared. Having in mind that any variable that I've seen in different scripts are always initialized, now I have a big confusion.

                  2. Can I assign directly any variable type to this array variable within a loop in a reliable way?

                  Thanks a lot, looking forward and hopefully not to bother anymore :-)
                  Last edited by pstrusi; 04-16-2015, 12:56 PM.

                  Comment


                    #10
                    Originally posted by pstrusi View Post
                    Ok, I've made a progress, I wasn't using correctly "using System.Collections;", now it's done and at least it's compiling, now I have 2 questions:

                    1. Why did you say that I must not use that List within Initialize, when all of the rest of variables are always "initialized" for later use OnBarUpdate?

                    2. Can I assign directly any variable type to this array variable within a loop in a reliable way?

                    Thanks a lot, looking forward and hopefully not to bother anymore :-)
                    1. He means don't use the Initialize() method for this. That method should be best reserved for UI items in the properties dialog. What you are doing is both declaring the variable name and initializing it for an ArrayList() object

                    2. Yes, but I wouldn't recommend that. You generally want to have one Array per type that you are building for. This help prevents mismatch types down the road in your script

                    Comment


                      #11
                      Thanks a lot Calonious for your explanation, now I understand better the right use of it

                      Comment


                        #12
                        Hello,

                        Initialize is a NinjaTrader method meaning that it is not absolutely where you have to initialize variables. Initialize is called when the script is starting, this is called when you open a indicator menu or when you add it to a chart.

                        What it means to Initialize a variable is simply to create the variable so it can be used later in the script, this can be done in most of the scopes in the script.

                        if you create a variable inside of the Initialize method, that variable can only be used inside of Initialize. If you instead create the variable inside of the class, it can be used by Initialize if needed, and also OnBarUpdate.

                        For ArrayLists, this is similar to Array in the sense that it is not commonly used any longer, it is similar to array but ultimately List replaces both of these.

                        You can find more information on ArrayList specifically here: http://www.dotnetperls.com/arraylist

                        this will take base types like string or int, more advanced objects or NinjaScript specific objects may need casted to work with this, another reason Lists are a better alternative.

                        I look forward to being of further assistance.
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Once again, I'm very grateful for your help. I have to work this out. Should i stuck in something regarding to this, I'll let you know

                          Best regards
                          Last edited by pstrusi; 04-16-2015, 01:38 PM.

                          Comment


                            #14
                            I have another technical consult. As you've previously said well, using "List" is even better than ArrayList in performance, so I decide to use "List". But now I read that above the top you must put this first:
                            using System.Collections.Generic; so my doubt is, since it goes from the top, could it interfere with the usual behavior of a NinjaScript variables? I imagine NOT, but all this area of COLLECTIONS is quite new for me

                            Comment


                              #15
                              You won't have any issues. Its just telling the script to use this library so that you can use the List<> class and so forth.

                              Go ahead and add using System.Collections.Generic; to the "Using Declarations" region

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by joemiller, Today, 07:33 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post joemiller  
                              Started by isnercrypto, Today, 06:42 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post isnercrypto  
                              Started by Coursocied67, Today, 04:02 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post Coursocied67  
                              Started by TheGameOfProbabilities, 07-18-2019, 06:56 AM
                              7 responses
                              37 views
                              0 likes
                              Last Post DaveS
                              by DaveS
                               
                              Started by claxxical, Yesterday, 10:00 PM
                              0 responses
                              11 views
                              0 likes
                              Last Post claxxical  
                              Working...
                              X