Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Generic Multi-dimensional List

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

    #16
    Ralph,

    The output of the below code:
    [0][0] = System.Collections.Generic.List`1[System.Int32]
    [1][0] = System.Collections.Generic.List`1[System.Int32]
    [2][0] = System.Collections.Generic.List`1[System.Int32]
    [3][0] = System.Collections.Generic.List`1[System.Int32]

    The code:
    Code:
    #region Using declarations
    using System.Collections.Generic;
    #endregion
    
    namespace NinjaTrader.Indicator
    {
        public class TEST2 : Indicator
        {
      #region Variables
          private bool FirstRun = true;
          private int i, j;
          private string sOutput;    
          private List<List<int>> SubList = new List<List<int>>() ;    
          private List<List<List<int>>> MDList = new List<List<List<int>>>();
        #endregion
    
      protected override void OnBarUpdate()
      {
          if (FirstRun) {
            FirstRun = false;
            
            SubList.Add (new List<int>() {1, 2, 3, 4});
            for (i = 0; i < 4; i++) {
              MDList.Add (new List<List<int>>(SubList));
            }
            
            Print ("Initialized Output");
            sOutput = "";
            for (i = 0; i < MDList.Count; i++) {
              for (j = 0; j < MDList[i].Count; j++) {
                sOutput = sOutput + "[" + i + "]" + "[" + j + "] = " + MDList[i][j] + "\t"; 
              }
              sOutput = sOutput + Environment.NewLine;
            }        
            Print (sOutput);        
          }      
       }
    }
    Any advice or suggested sources of education would be greatly appreciated.

    As always, thanks and regards
    Shannon

    Comment


      #17
      Hello Shannon,

      I do not use specific sources of informations but I like the link NT_AdamP provided in post #4.

      I think your code example operates properly. SubList contains a list with 4 elements. MDList contains the link to SubList four times. Your print statements show that MDList contains 4 elements (SubList) and every element (SubList) contains just 1 element, which is the list with the 4 integer values.

      Regards
      Ralph

      Comment


        #18
        Ralph & Adam,

        Thanks for all your help.

        I've reviewed Adam's link from post#4, I've done my best to make it work... and have come up short. The code is wrong and can be seen with the toggling to two different items:
        - changing if(RunCount < 1) to "< 2" or higher
        - while lower level lists should be added by "D2List.Add (D1List)" this yields incorrect data.
        The below code produces the below output which looks good, until the above changes are implemented:
        RunCount = 0
        D3top = 4, D2top = 3, D1top = 2
        D3List.Count 4
        D3List.Count[0] 3
        D3List.Count[0][0] 2
        000 001
        010 011
        020 021

        100 101
        110 111
        120 121

        200 201
        210 211
        220 221

        300 301
        310 311
        320 321

        Code:
        #region Using declarations
        using System.Collections.Generic;
        #endregion
        
        // This namespace holds all indicators and is required. Do not change it.
        namespace NinjaTrader.Indicator
        {
            [Description("Enter the description of your new custom indicator here")]
            public class TEST2 : Indicator
            {
            #region Variables
              private int RunCount = 0;
              private int i, j, k, l, D1top, D2top, D3top;
              private string sOutput;
              private List<string> D1List = new List<string>() ;  
              private List<List<string>> D2List = new List<List<string>>() ;    
              private List<List<List<string>>> D3List = new List<List<List<string>>>();
            #endregion
        
              protected override void Initialize()
              {
              }
        
              protected override void OnBarUpdate()
              {
              if (RunCount < 1) {
                Print ("RunCount = " + RunCount);
                RunCount =+ 1;
                
                var rand = new Random();
                D3top = rand.Next(2, 5);
                D2top = rand.Next(2, 5);
                D1top = rand.Next(2, 5);        
                
                Print ("D3top = " + D3top + ", D2top = " + D2top + ", D1top = " + D1top);
                
                D3List.Clear();
                for (i = 0; i < D3top; i++) {
                  D2List.Clear();
                  for (j = 0; j < D2top; j++) {
                    D1List.Clear();
                    for (k = 0; k < D1top; k++) {
                      D1List.Add (i.ToString()+j.ToString()+k.ToString());
        //              Print (D1List[k].ToString() + " = " + i.ToString()+j.ToString()+k.ToString() );
                    }
                    D2List.Add (new List<string>(D1List)); // D2List.Add (D1List) should be correct but yields incorrect data
                  }
                  D3List.Add (new List<List<string>> (D2List)); // D3List.Add (D2List)  should be correct but yields incorrect data
                }    
                
                Print ("D3List.Count " + D3List.Count);
                Print ("D3List.Count[0] " + D3List[0].Count);    
                Print ("D3List.Count[0][0] " + D3List[0][0].Count);  
                
                sOutput = "";
                for (i = 0; i < D3List.Count; i++) {
                  for (j = 0; j < D3List[i].Count; j++) {
                    for (k = 0; k < D3List[i][j].Count; k++) {
                      sOutput = sOutput + D3List[i][j][k] + "\t"; 
                    }
                    sOutput = sOutput + Environment.NewLine;
                  }
                  sOutput = sOutput + Environment.NewLine;
                }        
                Print (sOutput);  
              } // end RunCount      
            } // end OnBarUpdate
        Any last words of wisdom would be welcome. The world of multi-dimensional lists is a little too much for me.

        Shannon

        Comment


          #19
          And what is the output when using "<2" or higher?

          Regards
          Ralph

          Comment


            #20
            Ralph,

            Thank you for the reply. The "<2" or higher resulted in an infinite loop. The code "RunCount =+ 1;" was incorrect, the correct code should have been "RunCount += 1;".

            I've been working with the example from http://www.dotnetperls.com/nested-list in Visual Studio and have successfully created a three-dimensional(3D) list. The code was subsequently composed in NT7. It is now at the stage where it will successfully build and display a 3DList on OnBarUpdate. But fails to progressively build and display the 3DList where a 2DList is added with each sequential OnBarUpdate.

            When building the 3DList with each sequential OnBarUpdate, the first 2DList is repeated as the 2nd and 3rd element of the 3DList.

            Please find the TEST3 code attached.

            As always any input would be welcome.
            Regards
            Shannon
            Attached Files

            Comment


              #21
              Ralph & the NT team,

              Looking into this issue further using Visual Studio, the "TEST3"code which builds a 3DList on a single OnBarUpdate works as expected. The issue of building a 3DList over sequential OnBarUpdates persists, the 1st list is repeated as MDList element 1, 2, & 3.

              Perplexingly, inserting a breakpoint on line54 "MDlist.Add(list);" and progressing through the code shows MDList calculate correctly and results in a correct output!! Remove the breakpoint and the issue where the 1st list is repeated as MDList element 1, 2, & 3 presents again!!

              Noting I am clearly a novice, respectfully, is there a larger problem I am not aware of with NT7?

              Regards
              Shannon

              Comment


                #22
                Hello Shannon,

                you re-instantiate your random variable with everey invocation of OnBarUpdate() and therefore the creation of the random numbers repeats identically. You should declare the random variable in the indicator's variables section and instantiate it in Initialize(). (protected override void Initialize() {rand = new Random()})

                Regards
                Ralph

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                574 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                333 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                553 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                551 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X