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

nested lists example

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

    nested lists example

    hello everyone,

    would anyone be kind enough to help me on this ? real good exercice on lists.
    I was not able to find such example to copy anywhere...

    I have this class inside of a strategy :
    Code:
    List<ex> exempleList = new List<ex>();
    public class ex
    {
    private string s;
    
    public ex( string s )
    {
    this.s = s;
    }
    
    public string S
    {
    get { return s; }
    set { s = value; }
    }
    }
    // we would use this syntax to add values to ex from its parent : exempleList.Add(new ex(somevaluea));
    I want to create a list f inside the above list ex.
    f will contain multiple dictionaries <int, string>, (or any kind of structure with one int associated to one string, if there is more appropriate than dictionaries for this specific case).

    later I want to query through f per ex object.

    How would we create f inside ex ?
    How would we create the dictionnaries/other lists inside f ?
    how would we add values inside f from the parent of ex ?
    how would we then query inside f from the parent of ex ?

    #2
    Hello Amedeus,

    I think you might be wanting a linq select or where?

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      hey Chelsea, thank you very much,

      Are you suggesting that we do not need to create a nested list to "query through f per ex object" ?
      instead we just add more values to ex, beeing careful of how we name them in order to Select them easily later in our query.
      do you confirm that ?

      This could work. I like the avoidance of multiple dictionaries very much.
      it would look like this, following #post1 axample :
      Code:
      List<ex> exempleList = new List<ex>();
      public class ex
      {
      private string s;
      private int f_str_a;
      private int f_str_b;
      
      public ex(
      string s
      int f_str_a
      int f_str_b
      )
      {
      this.s = s;
      this.f_str_a = f_str_a;
      this.f_str_b = f_str_b;
      }
      
      public string S
      {
      get { return s; }
      set { s = value; }
      }
      public int f_str_a
      {
      get { return f_str_a; }
      set { f_str_a = value; }
      }
      public int f_str_b
      {
      get { return f_str_b; }
      set { f_str_b = value; }
      }
      }
      // we would use this syntax to add values to ex :
      // exempleList.Add(new ex(
      // somevaluea
      // f_int_a
      // f_int_b
      // ));
      
      // we would use this syntax to query our f_ values :
      var compare_f_values = from a in exempleList
      where a.Contains("f_str_")
      select a;
      but this a.Contains("f_str_") syntax cannot compile.

      doing this, I realize I should have been a little more precise :
      with <int,string> (from my #post1 dictionary suggestion), ( it could definitely be <string,int> instead)
      int would be a Bar number flag, not an index starting at zero (unlike the string[] fruit array from msdn.)
      string would be the name of a flag list (another already existing list : List<int> str_a = new List<int>(); )

      the goal would be to compare those flags per <ex> object.
      How could we add multiple flags<string,int> to <ex> ?
      if we can add them like above, how could we select the f_str_ items only ?
      Last edited by Amedeus; 06-06-2022, 07:02 AM.

      Comment


        #4
        Hello Amedeus,

        Attached is an example of Linq with classes that contain strings.

        Note, that support for advanced C# that is not documented NinjaScript is limited and your milage may vary.
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          hi Chelsea,

          LINQExample will definitely help with my syntax and queries, thank you very much.
          thanks also for the tutos link #post2.

          in the meantime this is what I came up with in order to compare flags :
          - implemetning <ex> with each flagList,
          - then use iEnumerables tuples, instead of dictionaries or sortedLists, because the key (bar number or flag name) would cause errors when multiple flags in the same bar,
          - then create a big chronological Enumerable FlagSequence tupleList to Linq from.

          it works, we can loop with name, bar number and bars ago, but I suspect there is a simpler way to get to FlagSequence (without the so many lists inbetween)
          I attach a .txt to illustrate this.

          my 1st idea was to populate and add tupleList directly to <ex>, but no success adding it.

          have a good one.
          Attached Files
          Last edited by Amedeus; 06-11-2022, 09:56 AM.

          Comment


            #6
            hi Chelsea, hi everyone,

            I understand this is more a c# problem than NinjaScript, and thank you very much for the help so far.

            I am not satisfied with the tuplelist from last post, too much complexity to get to the final FlagSequence tupleList.
            as in #post1, I try to nest a list class of objects <flag>, per <ex> object, containing all elements of multiple existing lists<int>.

            I believe the attached classes are almost what we should have per class.
            How could we add each elements of the Lists<int> inside List<flag> ?
            I get all kind of compiling errors trying it.


            in such way, for example, that we would later be able to query as following :
            foreach (ex.flag thisObject in exempleList.Where(obj => obj.Name.Contains("flagListA")))
            Attached Files

            Comment


              #7
              Hello Amedeus,

              "How could we add each elements of the Lists<int> inside List<flag>"

              I'm not sure what you are asking..

              You want each Flag object to have its own list?

              Make that an object in the Flag class..

              public class Flag
              {
              private string name;
              private int barNb;

              public List<int> MyList;

              public Flag()
              {
              MyList = new List<int>();
              }
              }

              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                hi Chelsea, thank you,

                "You want each Flag object to have its own list?"
                Not exactly, I want each int of each lists to be assigned a flagname, and be an item of <Flag>, with 2 properties (at least),
                flag beeing a property of ex.
                (later we want to query them using ex.Flag.Name or ex.Flag.BarNb properties)

                is it not possible to directly add a flag object from the main program (strategy),
                without copying each list<int> inside of <ex> or <Flag>?

                this is the closest i've come to what i attempt to do :
                Code:
                var flagsA = myIntListA .Select(c => new {Name = "flagsA", BarNb = c});
                var flagsB = myIntListB .Select(c => new {Name = "flagsB", BarNb = c});
                var f_sequenceContainingAllTheIntsOfAllLists = flagsA .Concat(flagsB)
                .Select(c => new {Name = c.Name, c.BarNb})
                .OrderBy(c=>c.BarNb); // .ToList(); did nothing for us...
                myExempleList.Add( new ex(f_sequenceContainingAllTheIntsOfAllLists)); // but we cannot convert an anonymous type to <Flag>
                (just for precision sake : the above example is having the nested list as only parameter. And indeed we are trying to add from the strategy to the nested class)
                Last edited by Amedeus; 06-14-2022, 09:48 AM.

                Comment


                  #9
                  Hello Amedeus,

                  You want the List<int> to have a flag property?

                  The object type in the list is int. Int cannot have a Flag property.
                  But you can make a custom class that has both an int property value and a Flag property value.. This would be instead of using the List<int> you would use List<CustomClass> which has both CustomClass.MyIntValue and CustomClass.MyFlagValue which can be searched together in linq.

                  "is it not possible to directly add a flag object from the main program (strategy),
                  without copying each list<int> inside of <ex> or <Flag>?"

                  I'm sorry I'm not sure what you are asking.
                  You want to know if you can instantiate a Flag instance without a list?

                  Flag myFlag = new Flag();
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    hey Chelsea, thank you,

                    This would be instead of using the List<int> you would use List<CustomClass> which has both CustomClass.MyIntValue and CustomClass.MyFlagValue which can be searched together in linq.
                    yes I want to do that, but with CustomClass beeing children (nested in) of myExampleList.
                    Also, CustomClass is just a aggregation of all the List<int>, which are given a name.
                    Also, CustomClass would have its values populated when myExempleList have its values populated. ( this is what I meant in #post6 : "to nest a list class of objects <flag>, per <ex> object, containing all elements of multiple existing lists<int>", and in the #post8 piece of erroneous code wich i find very illustrative of the intend).
                    => CustomClass is a property of myExempleList, this is why I spontaneously would use myExempleList.Add(), to pupulate it.
                    => every time a myExempleList<ex> object is added, a certain amount of ints and names should be added to List<CustomClass> too.

                    do you get it now ?
                    How could we populate CustomClass (with all those ints and names) the simplest way ?


                    "is it not possible to directly add a flag object from the main program (strategy), without copying each list<int> inside of <ex> or <Flag>?"
                    I said that because we could add all the list<int> to myExempleList and use a var IEnumerable later, similar to the var in #post8. But I find the Enumerable CustomClass to be a better choice.
                    Last edited by Amedeus; 06-14-2022, 12:13 PM.

                    Comment


                      #11
                      Hello Amedeus,

                      I am still not understanding.

                      "yes I want to do that, but with CustomClass beeing children (nested in) of myExampleList."

                      List<CustomClass> would be a list of a class that include but an int list and a Flag. This would be the parent structure.

                      public class CustomClass
                      {
                      public List<int> MyIntList;
                      public List<Flag> MyFlagList;
                      }

                      I'm not understanding why this would need to be nested in MyExampleList.

                      You can call the list MyExampleList..

                      List<CustomClass> MyExampleList = new List<CustomClass>();

                      "Also, CustomClass is just a aggregation of all the List<int>, which are given a name."

                      CustomClass would be an object that can hold the entire List<int> as well as a List<Flag> if you are wanting.
                      A List<CustomClass> is a collection of each of the CustomClass objects.

                      Aggregating all of the int lists together could be done with a loop or with List.Concat() or .AddRange().
                      Use the Concat extension method from System.Linq. Concat combines 2 IEnumerables.



                      "How could we populate CustomClass (with all those ints and names) the simplest way ?"

                      A List<T> can be populated when declared or with .Add() as you are doing. The LINQExample demonstrates both. There wouldn't be another way.

                      "I said that because we could add all the list<int> to myExempleList and use a var IEnumerable later, similar to the var in #post8. But I find the Enumerable CustomClass to be a better choice."

                      I'm still not understanding what you are meaning.

                      The main program? As in can you add a list variable to the window class or the strategy class?
                      Yes, you can declare a list in the scope of a window or strategy class..

                      I'm not understanding what you are copying. What are you are copying and why?
                      Why not make whatever information it is, a property of the object you intend it to be in?

                      List is an enumerable object type.. so the properties of enumerable types are available...

                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        hey Chelsea thank you,

                        From what I see the confusion comes from how we name all these lists and classes. And probably how much more accurate you are than I am.
                        But I see my problem in what you write :

                        not sure I can present it clearer,
                        using your latest post nomenclature, we have inside a strategy :

                        public class StrategyName: Strategy
                        {
                        List<CustomClass> MyExampleList = new List<CustomClass>();
                        public List<int> MyIntListA;
                        public List<int> MyIntListB; // I put the List<int> here for now, not inside CustomClass, because that's where they are in my strategy.

                        // we will add() to MyExampleList somewhere around here, in one of the Update() voids.

                        public class CustomClass
                        {
                        public List<Flag> MyFlagList;
                        }

                        }

                        Does public List<Flag> MyFlagList needs its own class two ? If not, pls let me know. (we want it to have 2 properties Name and BarNb).
                        To me MyFlagList is nested, cildren of MyExampleList. Is this wrong ?
                        How would you make a Flag object (a concat of both lists<int>) every time you Add(new CustomClass()) to MyExampleList ?
                        Can this be done ?

                        I think that the last questions summarize it.
                        Does it not clarify all posts above ?
                        Last edited by Amedeus; 06-14-2022, 01:41 PM.

                        Comment


                          #13
                          Hello Amedeus,

                          If MyIntListA and MyIntListB are meant to contain information for each CustomClass, that should be declared as a property of CustomClass.

                          List<Flag> is a list of Flag objects. Flag is already a class. List is already class. List<Flag> is a list of a class object. This itself cannot be a class.

                          You can have a list of lists.
                          List<List<MyObjectType>> <-- this is a nested list

                          However, are you sure what you want is a nested list?

                          I think you want each flag object to have a list. Which means declare the list in the Flag class.

                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            hi Chelsea, thank you very much.

                            However, are you sure what you want is a nested list?
                            not anymore, since it can contain only one argument.

                            I think you want each flag object to have a list.
                            i am not sure : I would have said : a list of flag objects with 2 parameters.
                            each flag object would have a string Name and an int BarNb. (from the concat of both lists<int>.
                            But it also sounds true because I want the list to be one parameter of CustomClass, I try to Add() to MyExampleList as one argument like in #post8.

                            does the query from the strategy not help figuring out what I attempt to do ?
                            foreach (CustomClass.Flag thisObject in MyExampleList.Where(obj => obj.Name.Contains("flagListA") || obj.BarNb > someInt))
                            it may need more reference :
                            foreach (CustomClass.Flag thisObject in MyExampleList.MyFlagList.Where(obj => obj.Name.Contains("flagListA") || obj.BarNb > someInt))


                            I can"t picture the macanism of what you say...

                            If MyIntListA and MyIntListB are meant to contain information for each CustomClass, that should be declared as a property of CustomClass.
                            there is no choice but to add both List<int> to CustomClass ? then to do something with them from the Flag class ?
                            or did I not understand this ?

                            Which means declare the list in the Flag class.
                            I dont see how to do that ?
                            I had in mind a class with the looks of CustomClass, with a similar "structure", same as in #post1...
                            (2 variables, one Flag(string flagName, int barNb){this.flagName = flagName; etc} method, and 2 properties with get; set)
                            should the class not look like that ?
                            I bet a foreach should be involved, where in Flag class is that ?
                            Last edited by Amedeus; 06-15-2022, 02:01 PM.

                            Comment


                              #15
                              hi Chelsea,

                              could this be correct c# ?
                              CustomClass<MyFlagList<Flag>>

                              if not, why ?
                              if yes, how to populate every flag with a name and barNb from the List<int>, directly from an Update void in the strategy?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by rocketman7, Today, 02:12 AM
                              7 responses
                              28 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by guillembm, Yesterday, 11:25 AM
                              3 responses
                              16 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by junkone, 04-21-2024, 07:17 AM
                              10 responses
                              148 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by tsantospinto, 04-12-2024, 07:04 PM
                              6 responses
                              101 views
                              0 likes
                              Last Post tsantospinto  
                              Started by trilliantrader, 04-18-2024, 08:16 AM
                              7 responses
                              28 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Working...
                              X