Announcement

Collapse
No announcement yet.

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 sjsj2732, Yesterday, 04:31 AM
            0 responses
            31 views
            0 likes
            Last Post sjsj2732  
            Started by NullPointStrategies, 03-13-2026, 05:17 AM
            0 responses
            286 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            283 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            133 views
            1 like
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            91 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Working...
            X