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

Relation between key and index using Dictionary and Struct

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

    Relation between key and index using Dictionary and Struct

    I'm a bit mixed up on the relation between the Key in a Dictionary and the Index#. When I am adding stocks to the dictionary as below, does the first stock added get an Index of 1 and also a Key of 1? Are they one in the same. Does it look like I have done this correctly?

    Code:
    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()
            {						
    Dictionary<int, StockEntry> stocks = new Dictionary<int, StockEntry>();
    					
    //5min bars
    stocks.Add(1, new StockEntry { Name = "ABC", Period = PeriodType.Minute, Value = 5, Count = 0 } );
    stocks.Add(2, new StockEntry { Name = "ACE", Period = PeriodType.Minute, Value = 5, Count = 0 } );
    }
    
    protected override void OnBarUpdate()
    {
    for (int series = 0; series < 3; series++)
    if (BarsInProgress == series)
    //Condtions for entry here..
    var stockEntry = stocks[series];
    
                                         EnterLong(200);
    					 {
    					  if(stocks.ContainsKey(series))
    						{
    						stockEntry.Count++;
    						}
    					 }				
    }

    #2
    Hello zachj,

    Thank you for your post.

    The BarsInProgress is referenced in starting with 0. 0 (zero) applies to the bar series the indicator or strategy is applied to on the chart or Strategies tab of the NinjaTrader Control Center. Any additional bar series added in your code is added with 1, 2, 3, etc. consecutively.

    For information on multiple bar series in your code please visit the following link: http://www.ninjatrader.com/support/h..._threading.htm

    Using a Dictionary collection is not directly supported in NinjaScript, this is a general C# programming item. Count will begin with 1 and continue from there. For information on Count in C# please visit the following link: http://msdn.microsoft.com/en-us/library/27b47ht3.aspx

    Please let me know if I may be of further assistance.

    Comment


      #3
      So basically you're saying that when using Dictionary it will skip the 0 index? Will using a List start the count at 0?

      Comment


        #4
        Hello zachj,

        Thank you for your response.

        I am not sure that is completely correct for the Dictionary in C#, but with count it will provide values starting with 1 instead of 0 like an index.

        Comment


          #5
          I'm not exactly sure what you are getting at. I'm just using Count to count the # of trades that have been entered I don't believe that will interfere with the indexing at all or the addition of stocks. If there have been 0 trades Count = 0, if 1 trade Count = 1. Am I missing something?

          Comment


            #6
            Hello zachj,

            Thank you for your response.

            Unfortunately I am not aware of what the index will be when adding item to the Dictionary Collection. This is a general C# item and I recommend reviewing the information available on the MSDN site: http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by DJ888, Today, 10:57 PM
            0 responses
            2 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by MacDad, 02-25-2024, 11:48 PM
            7 responses
            158 views
            0 likes
            Last Post loganjarosz123  
            Started by Belfortbucks, Today, 09:29 PM
            0 responses
            7 views
            0 likes
            Last Post Belfortbucks  
            Started by zstheorist, Today, 07:52 PM
            0 responses
            7 views
            0 likes
            Last Post zstheorist  
            Started by pmachiraju, 11-01-2023, 04:46 AM
            8 responses
            151 views
            0 likes
            Last Post rehmans
            by rehmans
             
            Working...
            X