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

Array creation for indicators

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

    Array creation for indicators

    Hello, I am trying to find a way to use Arrays or Series to create multiple bollingers, Keltner, etc... For example, I want to create 3 or 4 different Keltner on one indicator and would like to find a way to automate the variable name creation. This way I can use 1 array to call out the properties for each of the Keltners, such as Offset and multiplier.

    Same goes for other indicators like Bollinger, I would like to have multiple bollingers and run calculations for them, but would like to create a For loop, with the array and call the properties of each bollingers.

    Basically, if I was to create a variable name called boll1, I was wondering if there is a way to make boll2, boll3, boll4, etc... or Kelt1, Kelt2, Kelt3, etc... automatically assign a new variable name.

    #2
    Hello bpatel20,

    Thank you for the post.

    This is going to be a good question to also do some general internet searches about in contrast to just C# as this is an C#/OOP concept. The reason I say this is that it depends on how you want to later access the data, that would decide what type of collection to use. An indicator is just an object, so accessing it will just require the correct syntax for the collection type being used.

    If you are just trying to automate a lot of indicators but don't necessarily need to associate a specific indicator to a specific value, a List would likely be one of the easiest ways to do this because a list does not require a specific size like an array so you can add as many items to it as you wanted or loop over it later to collect values.

    If you need to know a specific indicators value while looping through, a list, array or a dictionary could be used for this purpose. Now comes the question of how you intend to use the data or access it, if you wanted a specific name like bol2, a dictionary would be best. Otherwise if using an integer as an index value works, the order which the indicator is added to the collection would become its index, so you could use an index to know a specific indicators value in a list or array.

    Here is one example of adding an indicator to a list and later using its value. An array would essentially be the same process however you would define the initial array differently. I will provide the C# documentation for both list and array below.

    Code:
    List<KeltnerChannel> myKeltners = new List<KeltnerChannel>();
    
    for(int i = 0; i < 10; i++)
    {
          myKeltners.Add(KeltnerChannel(1.5, 10 + i));
    }
    
                //later
    
    for(int i = 0; i < 10; i++)
    {
           Print(string.Format("keltner {0} value {1}", i, myKeltners[i].Upper[0]));
    }
    An array would be nearly similar, except with an array you would need to create an array of a specific size. You can find a simple example of an array in the following link:

    Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the base class for all arrays in the common language runtime.


    and for Lists:

    Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.


    Dictionaries are also a good option for this use case as noted you can give each instance a new name to delegate later logic. I will just provide the general documentation for this so you can research this if you would like.



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

    Comment


      #3
      Hello Jesse, Thank you for that, it worked perfectly.

      One more thing if you don't mind, I was trying to output the lines that I have created for Keltner, using an Array. I was wondering if you could help me with that, if there is a way to create an array for outputting all lines and plot them, or if you have to use different variables. Please see my code below.

      if(See_Kel1 == true && See_Bol1 == true)
      {
      for(int i = 0; i < 3; i++)
      {
      if(KelArray[i].Upper[0] > BelArray[i].Upper[0]) //Checks if Keltner is farther out then Bollinger
      {
      KU[i] = KelArray[i].Upper[0]; //Assigns value of Keltner to KU Array for PLOTTING.
      }
      if(BelArray[i].Upper[0] > KelArray[i].Upper[0]) // Checks if Bollinger is further out then Keltner
      {
      KU[i] = BelArray[i].Upper[0]; //Assigns value of Bollinger to KU Array for PLOTTING.
      }
      }
      }

      //In the properties section, I was trying to do this, but it isn't working. This gives me an error, and will not output the KU Array. I was hoping that would work, so I can just set the values of each upper and lower boundary, then just run an array and have it display different lines, but just use 1 array do display 3 lines on bar update.

      for(int j = 0; j < 3; j++)
      {
      [Browsable(false)]
      [XmlIgnore]
      public Series<double> KU[j]
      {
      get { return Values[j]; }
      }
      }

      //NORMALLY THE BELOW CODE IS WHAT I HAVE TO DO FOR EACH INDIVIDUAL OUTPUT, but I was hoping to do an Array and go through all values.

      [Browsable(false)]
      [XmlIgnore]
      public Series<double> K2U
      {
      get { return Values[3]; }
      }

      Comment


        #4
        Hello bpatel20,

        For plotting, you can just use the Values collection in your loop so the actual plotting of data would not be a problem.

        Code:
        Values[index][0] = BelArray[i].Upper[0]; //Assigns value of Bollinger to KU Array for PLOTTING.
        Making the public properties as you have shown would be the problem as you cannot automate that part. Each public property would need to be hardcoded as shown in your second example.

        This brings up a deciding question, do the plots need to have a public property? Public properties are generally used to expose the value of the plot for other scripts to access. Do other scripts need to access these plots or are they strictly visual?

        If they do not need to be accessed outside of this script, just use the Values collection to set the plots and ignore making public properties.

        If you do need to access this outside of this script, you would need to retain what you created so far, but manually type out each public property for each exposed plot you need. The remainder of your logic could remain looped as you are now, just the public properties would need to be hand coded so you can specify the index Values returns in the property along with a property name.

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

        Comment


          #5
          Hi Jesse,

          Thank you that makes sense.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by WeyldFalcon, 12-10-2020, 06:48 PM
          14 responses
          1,427 views
          0 likes
          Last Post Handclap0241  
          Started by Barry Milan, Today, 10:35 PM
          0 responses
          6 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by DJ888, Yesterday, 06:09 PM
          2 responses
          9 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by jeronymite, 04-12-2024, 04:26 PM
          3 responses
          40 views
          0 likes
          Last Post jeronymite  
          Started by bill2023, Today, 08:51 AM
          2 responses
          16 views
          0 likes
          Last Post bill2023  
          Working...
          X