CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 44

Thread: count++

  1. #16
    Join Date
    Aug 2013
    Posts
    25

    Re: count++

    Unless you disagree with my above email and still think I can somehow use dictionary, I am moving forward and attempting to use a list of structs below. But how do I set it up so it has the 3 arguments defined..string, PeriodType, int? Or maybe the below is completely wrong?

    List<Stocks> _stockcount = new List<Stocks>();
    Stocks data = new Stocks();
    {
    Stock1 = "AAPL"
    Stock2 = "MSFT"
    };

    _stockcount.Add(data);

  2. #17
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: count++

    You aren't following. I'm not suggesting that you use 2 params. If you store the vales always as seconds, you can determine the period type and be able to pass 3 params. If you don't see how to do this, then use a struct.

  3. #18
    Join Date
    Aug 2013
    Posts
    25

    Re: count++

    Quote Originally Posted by Arjay View Post
    You aren't following. I'm not suggesting that you use 2 params. If you store the vales always as seconds, you can determine the period type and be able to pass 3 params. If you don't see how to do this, then use a struct.
    I guess I am not following as Dictionary only allows 2 parameters as far as I know, I don't see how I can use 3.

  4. #19
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: count++

    You store the name and seconds in the dictionary. When you call the api which takes 3 params, you have the name (1st param) and the seconds. You use the seconds value to derive the last 2 params.

    For example, if the PeriodType is Hours and the value is 60, then 3600 would be stored in the dictionary. When you call the api, to determine the PeriodType, you divide by 60 multiple times to determing if the PeriodType is hours, minutes or seconds. If this is too complicated, use a struct.

    Code:
    public struct StockEntry
    {
      public string Name { get; set; }
      public PeriodType Period { get; set; }
      public int Value { get; set; }
      public int Count { get; set; }
    }
    
    Dictionary<string, StockEntry> _stocks = new Dictionary<string, StockEntry>();
    
    // example of adding a stock to the dictionary
    _stocks.Add("ABC", new StockEntry { Name = "ABC", ... } );

  5. #20
    Join Date
    Aug 2013
    Posts
    25

    Re: count++

    Quote Originally Posted by Arjay View Post
    You store the name and seconds in the dictionary. When you call the api which takes 3 params, you have the name (1st param) and the seconds. You use the seconds value to derive the last 2 params.

    For example, if the PeriodType is Hours and the value is 60, then 3600 would be stored in the dictionary. When you call the api, to determine the PeriodType, you divide by 60 multiple times to determing if the PeriodType is hours, minutes or seconds. If this is too complicated, use a struct.

    Code:
    public struct StockEntry
    {
      public string Name { get; set; }
      public PeriodType Period { get; set; }
      public int Value { get; set; }
      public int Count { get; set; }
    }
    
    Dictionary<string, StockEntry> _stocks = new Dictionary<string, StockEntry>();
    
    // example of adding a stock to the dictionary
    _stocks.Add("ABC", new StockEntry { Name = "ABC", ... } );
    I think I might be retarded, so if I have Add("ABC", PeriodType.Minute, 5) instead I would do Add("ABC", PeriodType.Second, 300)?? It's not making sense to me because I have to put PeriodType.Second or Min or whatever which is not of type int, it's of type PeriodType. So when it tries to pull in 300 to PeriodType.Sec ill get an error I would think.

    Maybe the struct is the way to go. So you wrote..
    _stocks.Add("ABC", new StockEntry { Name = "ABC", .... } );
    but where you have the ,...... would I fill it in as such...
    { Name = "ABC", Period = Minute, Value = 5, Count = what would I put for Count here because that is yet to be determined I think?

  6. #21
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: count++

    Quote Originally Posted by zirjeo View Post
    what would I put for Count here because that is yet to be determined I think?
    set it to 0 or don't include the count property. By default integer variables are initially set to zero.

  7. #22
    Join Date
    Aug 2013
    Posts
    25

    Re: count++

    Think I'm getting close, but I'm getting the error... "The name '_stocks' does not exist in the current context
    " for the 3 locations highlighted in blue below. Any idea?


    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<string, StockEntry> _stocks = new Dictionary<string, StockEntry>();
    			
    _stocks.Add("ABC", new StockEntry { Name = "ABC", Period = PeriodType.Minute, Value = 5, Count = 0 } );
    _stocks.Add("ACE", new StockEntry { Name = "ACE", Period = PeriodType.Minute, Value = 5, Count = 0 } );
    }
    
          protected override void OnBarUpdate()
           {
                    for (int series = 0; series < 2; series++)
    		if (BarsInProgress == series + 2) //OnBarUpdate called for 1min bars
    		    {	 
    				var singleStockCount = _stocks[series];
    				bool enterTrade = false;
    				
    			    if (singleStockCount < 1)
    				{
    				enterTrade = true;
    				}
    				else
    				{
    				enterTrade = BarsSinceEntry(series, "", 0) > 2; 
    				} 
    
                    if (enterTrade)
    				  
                          {  // Condition set 1 Long Entry, fast MA cross above slow MA & current price > high of bar at cross.
    	                if (SMA(BarsArray[series],Fast)[1] > SMA(BarsArray[series],Slow)[1] && 
    					SMA(BarsArray[series],Fast)[2] < SMA(BarsArray[series],Slow)[2] && Closes[series + 2][0] > Highs[series][1] + distance 
    					&& SMA(BarsArray[series],Slow)[1] > SMA(BarsArray[series],Slow)[2] + .01)
    									
    
    
                        EnterLong(200);
    				{
                                       if(_stocks.ContainsKey(series))
                                        {
                                          _stocks[series]++;
                                         }
    				}				
    		      }
                   }
             }
    Last edited by zirjeo; September 17th, 2013 at 09:34 PM.

  8. #23
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: count++

    You've declared the _stocks dictionary within the Initialize method. This makes the _stocks variable a local variable and only accessible within the Initialize method. You need to declare the _stocks variable as a field of the class (so it can be accessed by any method of the class).

    If you don't know how to do this, consult an online C# tutorial (which can teach you c# basis better than we can here on code guru).

  9. #24
    Join Date
    Aug 2013
    Posts
    25

    Re: count++

    Quote Originally Posted by Arjay View Post
    You've declared the _stocks dictionary within the Initialize method. This makes the _stocks variable a local variable and only accessible within the Initialize method. You need to declare the _stocks variable as a field of the class (so it can be accessed by any method of the class).

    If you don't know how to do this, consult an online C# tutorial (which can teach you c# basis better than we can here on code guru).
    Ok so I have declared it for the class as such..
    private Dictionary<string, StockEntry> _stocks;

    But the problem now is that in my OnBarUpdate() section below it's trying to access a dictionary via an index like normal arrays or lists which I can't do. It has to access via its key but I'm not quite sure how to do this? Do I replace "series" below with KeyValuePair<string, StockEntry> or something to that affect?

    Code:
    protected override void OnBarUpdate()
    
    {  
    //for loop to iterate each instrument through
    for (int series = 0; series < 5; series++)
    if (BarsInProgress == series)
    {  
    var singleStockCount = _stocks[series];
    bool enterTrade = false;
       if (singleStockCount < 1)
    {
    enterTrade = true;
    }
    else
    {
    enterTrade = BarsSinceEntry(series, "", 0) > 2; 
    } 
    
                    if (enterTrade)
     {  // Condition for Long Entry here
    
    
                      EnterLong(200);
    {
     if(_stocks.ContainsKey(series))
    {
    _stocks[series]++;
    }
    }
     }
                } 
    }

  10. #25
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: count++

    Instead of using a string (i.e. stock name) as the dictionary key, use an integer. Then you can access the stock by series index.

    Keep in mind that the following doesn't return a count.

    Code:
    var stockEntry = _stocks[series];
    It returns a StockEntry object. So if you want to increment its count, you'll need to do:

    Code:
    _stocks[series].Count++;
    or

    Code:
    var stockEntry = _stocks[series];
    stockEntry.Count++;

  11. #26
    Join Date
    Aug 2013
    Posts
    25

    Re: count++

    Instead of using a string (i.e. stock name) as the dictionary key, use an integer. Then you can access the stock by series index.
    So where I have.... _
    _stocks.Add("ABC", new StockEntry { Name = "ABC", ... } );

    would I replace "ABC" with 0 as below?...
    _stocks.Add(0, new StockEntry { Name = "ABC", ... } );

    and if so how does "ABC" get assigned to Index 0? Does Name = "ABC" in StockEntry take care of this?

  12. #27
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: count++

    Quote Originally Posted by zirjeo View Post
    So where I have.... _
    _stocks.Add("ABC", new StockEntry { Name = "ABC", ... } );

    would I replace "ABC" with 0 as below?...
    _stocks.Add(0, new StockEntry { Name = "ABC", ... } );

    and if so how does "ABC" get assigned to Index 0? Does Name = "ABC" in StockEntry take care of this?
    You are in control of your code. When you add the entries, is it more convenient to track the entries by index or by name? Only you can answer this.

  13. #28
    Join Date
    Aug 2013
    Posts
    25

    Re: count++

    Quote Originally Posted by Arjay View Post
    You are in control of your code. When you add the entries, is it more convenient to track the entries by index or by name? Only you can answer this.
    With the way I already have it setup with the for loop below and the fact that I have to use the softwares internal command of BarsInProgress which requires an int, I guess my only choice really is to track by index.
    for (int series = 0; series < 5; series++)
    if (BarsInProgress == series)

    But my concern is when I add a stock to the dictionary as below...
    _stocks.Add(0, new StockEntry { Name = "ABC", ... } );

    Will stock ABC be assigned to index 0 because of the first term and because of Name = "ABC" or will it just automatically be assigned to an index 1 because it was the first one to be added?

    Normally when I would just Add a stock using Add("ABC", "", "") the software will assign this to index 1, then the next one I Add would take index 2. The program assigns index 0 to whatever stock chart is open in the software at the time by default.

    I guess I'm not understanding the basics of what putting in the 0 as the first term is doing exactly, and what Name = "ABC" is doing exactly.

  14. #29
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: count++

    Quote Originally Posted by zirjeo View Post
    I guess I'm not understanding the basics of what putting in the 0 as the first term is doing exactly, and what Name = "ABC" is doing exactly.
    It's a common mistake for beginners to dive into a larger program without understanding the building blocks of the language. Fortunately you can step out of the real program and create a test program that helps you understand at any time.

    Whe you say you don't understand the Dictionary class, learn about it by reading an online tutorial and write a test console app where you can try out some dictionary concepts. It might take 30 minutes to an hour to work through the basics, but it will be time well spent. Once you understand the dictionary well enough (can add and access dictionary items), the you can go back and use what you've learned in your real program.

  15. #30
    Join Date
    Aug 2013
    Posts
    25

    Re: count++

    Quote Originally Posted by Arjay View Post
    It's a common mistake for beginners to dive into a larger program without understanding the building blocks of the language. Fortunately you can step out of the real program and create a test program that helps you understand at any time.

    Whe you say you don't understand the Dictionary class, learn about it by reading an online tutorial and write a test console app where you can try out some dictionary concepts. It might take 30 minutes to an hour to work through the basics, but it will be time well spent. Once you understand the dictionary well enough (can add and access dictionary items), the you can go back and use what you've learned in your real program.
    I understand the dictionary class as far as the first argument being the key and the second being the values. I just didn't quite understand what you were trying to accomplish with having "ABC twice in the following... _stocks.Add("ABC", new StockEntry { Name = "ABC",....). Only info I ever see is simple Dictionary's where it's dictionary.Add("CAT", 2) so I'm just getting a bit thrown off when "ABC" is showing up twice.

    Either way I think there might be an issue as tech support for NinjaTrader is saying... "Using a Dictionary collection is not directly supported in NinjaScript".

    Can I use List in the same manner as in the following?....
    List<StockEntry> _stocks = new List<StockEntry>();
    _stocks.Add(new StockEntry { Name = "ABC", Period = PeriodType.Minute, Value = 5, Count = 0 } );
    Last edited by zirjeo; September 22nd, 2013 at 03:11 PM.

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured