• 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

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:

    https://docs.microsoft.com/en-us/dot...tframework-4.5

    and for Lists:

    https://docs.microsoft.com/en-us/dot...tframework-4.5

    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.

    https://docs.microsoft.com/en-us/dot...tframework-4.5

    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 rblunsom, Today, 11:19 AM
          2 responses
          20 views
          0 likes
          Last Post guidoisot  
          Started by drewski1980, Today, 02:38 PM
          1 response
          11 views
          0 likes
          Last Post NinjaTrader_ChrisL  
          Started by jfep1977, Today, 02:31 PM
          2 responses
          10 views
          0 likes
          Last Post jfep1977  
          Started by dmking, Today, 12:39 PM
          6 responses
          22 views
          0 likes
          Last Post dmking
          by dmking
           
          Started by marioresa, Today, 10:15 AM
          4 responses
          26 views
          0 likes
          Last Post marioresa  
          Working...
          X