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

Dictionary

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

    Dictionary

    In my initialize section I'm trying to use Dictionary. I'm getting compile errors..type or namespace Dictionary not found and _stockCounts does not exist. Something basic I'm doing incorrectly?

    Code:
            protected override void Initialize()
            {
    Dictionary<int, int> _stockCounts = new Dictionary<int, int>();
    			
    #region Primary 5min			
    //Primary 5min
    _stockCounts.Add("ACE",PeriodType.Minute, 5);
    _stockCounts.Add("ACN",PeriodType.Minute, 5);
    _stockCounts.Add("ADT",PeriodType.Minute, 5);
    
        CalculateOnBarClose = true;
    
            }

    #2
    Originally posted by zachj View Post
    In my initialize section I'm trying to use Dictionary. I'm getting compile errors..type or namespace Dictionary not found and _stockCounts does not exist. Something basic I'm doing incorrectly?

    Code:
            protected override void Initialize()
            {
    Dictionary<int, int> _stockCounts = new Dictionary<int, int>();
                
    #region Primary 5min            
    //Primary 5min
    _stockCounts.Add("ACE",PeriodType.Minute, 5);
    _stockCounts.Add("ACN",PeriodType.Minute, 5);
    _stockCounts.Add("ADT",PeriodType.Minute, 5);
    
        CalculateOnBarClose = true;
    
            }
    Have you specified the correct namespace?
    Code:
    [COLOR=Blue]using[/COLOR] System.Collections.Generic;

    Comment


      #3
      zachj, since more general C# not an area we could fully support, can you check into koganams's thought - that at minimum would need to be present.

      Further you could check into the Spearman indicator offered in the sharing section, it uses a SortedDictionary - http://www.ninjatrader.com/support/f...d=4&linkid=442
      BertrandNinjaTrader Customer Service

      Comment


        #4
        That fixed part of it. thanks. But now getting
        "No overload for method "Add" takes "3" arguments. Can I not do as I have below...

        _stockCounts.Add("ACE",PeriodType.Minute, 5);

        Comment


          #5
          I would expect an issue with a dictionary with 3 parameters, you have a key and a value always as pair here - http://www.dotnetperls.com/dictionary
          BertrandNinjaTrader Customer Service

          Comment


            #6
            Ok so if I have as below, is that by chance giving me a 5min or what would the 5 represent here?

            _stockCounts.Add("ACE", 5);

            Comment


              #7
              Originally posted by zachj View Post
              Ok so if I have as below, is that by chance giving me a 5min or what would the 5 represent here?

              _stockCounts.Add("ACE", 5);
              You defined your Dictionary thus:
              Code:
              Dictionary<int, int> _stockCounts = new Dictionary<int, int>();
              That means that the terms must both be integers. Your code, with a string, is not kosher.

              Comment


                #8
                Dictionary<string, int> _stockCounts = new Dictionary<string, int>();

                either way if I fix the first argument to be a string, I still only have two arguments to work with and need 3. I don't think Dictionary will take 3. I can't define the PeriodType.

                Comment


                  #9
                  Originally posted by zachj View Post
                  Dictionary<string, int> _stockCounts = new Dictionary<string, int>();

                  either way if I fix the first argument to be a string, I still only have two arguments to work with and need 3. I don't think Dictionary will take 3. I can't define the PeriodType.
                  No. A Dictionary takes exactly 2 parameters. If you want 3 parameters, you are looking for a different kind of code entity. What you need depends on what you are trying to do. Your code does not show your intent, so there is little that I can say.
                  Last edited by koganam; 09-05-2013, 08:21 AM. Reason: Corrected spelling.

                  Comment


                    #10
                    I was trying to use Dictionary so that I can count and store the # of entries for a multi-instrument script.

                    Comment


                      #11
                      Originally posted by zachj View Post
                      I was trying to use Dictionary so that I can count and store the # of entries for a multi-instrument script.
                      You might want to use a List<> of structs, where the struct contains the information that you want to track.

                      Comment


                        #12
                        I'm not really sure what that is, will have to research that. Thanks

                        Comment


                          #13
                          Originally posted by koganam View Post
                          You might want to use a List<> of structs, where the struct contains the information that you want to track.
                          Would I have something like this...

                          List<Indexes> IndexItems = new List<Indexes>();

                          list.Add("ACE", PeriodType.Minute, 5);
                          list.Add("ACN", PeriodType.Minute, 5);
                          list.Add("ADT", PeriodType.Minute, 5);

                          Then in the OnBarUpdate section have something like..

                          public struct Indexes
                          {
                          public string ACE { get; set; }
                          public string ACN { get; set; }
                          public string ADT { get; set; }
                          }

                          Comment


                            #14
                            Originally posted by koganam View Post
                            You might want to use a List<> of structs, where the struct contains the information that you want to track.
                            Ok giving this another shot koganam, how about this?...

                            public struct StockEntry
                            {
                            public string Name { get; set; }
                            public PeriodType Period { get; set; }
                            public int Value { get; set; }
                            public int Count { get; set; }
                            }

                            protected override void Initialize()
                            {

                            List<StockEntry> _stocks = new List<StockEntry>();

                            //Primary 5min
                            _stocks.Add(new StockEntry { Name = "ABC", Period = PeriodType.Minute, Value = 5, Count = 0 } );
                            _stocks.Add(new StockEntry { Name = "ACE", Period = PeriodType.Minute, Value = 5, Count = 0 } );
                            _stocks.Add(new StockEntry { Name = "ACN", Period = PeriodType.Minute, Value = 5, Count = 0 } );
                            _stocks.Add(new StockEntry { Name = "ADT", Period = PeriodType.Minute, Value = 5, Count = 0 } );
                            _stocks.Add(new StockEntry { Name = "SCTY", Period = PeriodType.Minute, Value = 5, Count = 0 } );

                            //Secondary 1min
                            _stocks.Add(new StockEntry { Name = "ABC", Period = PeriodType.Minute, Value = 1, Count = 0 } );
                            _stocks.Add(new StockEntry { Name = "ACE", Period = PeriodType.Minute, Value = 1, Count = 0 } );
                            _stocks.Add(new StockEntry { Name = "ACN", Period = PeriodType.Minute, Value = 1, Count = 0 } );
                            _stocks.Add(new StockEntry { Name = "ADT", Period = PeriodType.Minute, Value = 1, Count = 0 } );
                            _stocks.Add(new StockEntry { Name = "SCTY", Period = PeriodType.Minute, Value = 1, Count = 0 } );

                            }

                            Comment


                              #15
                              Originally posted by zachj View Post
                              Ok giving this another shot koganam, how about this?...

                              public struct StockEntry
                              {
                              public string Name { get; set; }
                              public PeriodType Period { get; set; }
                              public int Value { get; set; }
                              public int Count { get; set; }
                              }

                              protected override void Initialize()
                              {

                              List<StockEntry> _stocks = new List<StockEntry>();

                              //Primary 5min
                              _stocks.Add(new StockEntry { Name = "ABC", Period = PeriodType.Minute, Value = 5, Count = 0 } );
                              _stocks.Add(new StockEntry { Name = "ACE", Period = PeriodType.Minute, Value = 5, Count = 0 } );
                              _stocks.Add(new StockEntry { Name = "ACN", Period = PeriodType.Minute, Value = 5, Count = 0 } );
                              _stocks.Add(new StockEntry { Name = "ADT", Period = PeriodType.Minute, Value = 5, Count = 0 } );
                              _stocks.Add(new StockEntry { Name = "SCTY", Period = PeriodType.Minute, Value = 5, Count = 0 } );

                              //Secondary 1min
                              _stocks.Add(new StockEntry { Name = "ABC", Period = PeriodType.Minute, Value = 1, Count = 0 } );
                              _stocks.Add(new StockEntry { Name = "ACE", Period = PeriodType.Minute, Value = 1, Count = 0 } );
                              _stocks.Add(new StockEntry { Name = "ACN", Period = PeriodType.Minute, Value = 1, Count = 0 } );
                              _stocks.Add(new StockEntry { Name = "ADT", Period = PeriodType.Minute, Value = 1, Count = 0 } );
                              _stocks.Add(new StockEntry { Name = "SCTY", Period = PeriodType.Minute, Value = 1, Count = 0 } );

                              }
                              The pristine way to define a struct would be as follows:
                              Code:
                              public struct StockEntry
                              {
                              //declare the members
                                public string Name;
                                public PeriodType Period;
                                public int Value;
                                public int Count;
                              
                              //declare the constructor
                              public StockEntry(string strName, PeriodType ptPeriod, int intValue, int intCount)
                              {
                              Name = strName;
                              Period = ptPeriod;
                              Value = intValue;
                              Count = intCount;
                              }
                              }
                              ref: http://msdn.microsoft.com/en-us/libr...=vs.90%29.aspx

                              You use brackets, not braces, when you declare and initialize a struct.

                              Like so:
                              Code:
                              StockEntry ("ABC", PeriodType.Minute, 1, 0)
                              The keyword new is not mandatory.
                              Last edited by koganam; 09-22-2013, 06:22 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by algospoke, Yesterday, 06:40 PM
                              2 responses
                              19 views
                              0 likes
                              Last Post algospoke  
                              Started by ghoul, Today, 06:02 PM
                              3 responses
                              14 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by jeronymite, 04-12-2024, 04:26 PM
                              3 responses
                              45 views
                              0 likes
                              Last Post jeronymite  
                              Started by Barry Milan, Yesterday, 10:35 PM
                              7 responses
                              21 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by AttiM, 02-14-2024, 05:20 PM
                              10 responses
                              181 views
                              0 likes
                              Last Post jeronymite  
                              Working...
                              X