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

Thread: count++

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

    Re: count++

    You can use a List. As far as the documentation saying a dictionary isn't supported, I call B.S. If you are able to write supporting code, you can choose any storage method you want and it the program won't be impacted per se.

    I recommended a dictionary because early on it wasn't clear how the entries were accessed, but use a List if it makes more sense. Remember, you are in charge of the code.

  2. #32
    Join Date
    Aug 2013
    Posts
    25

    Re: count++

    Quote Originally Posted by Arjay View Post
    You can use a List. As far as the documentation saying a dictionary isn't supported, I call B.S. If you are able to write supporting code, you can choose any storage method you want and it the program won't be impacted per se.

    I recommended a dictionary because early on it wasn't clear how the entries were accessed, but use a List if it makes more sense. Remember, you are in charge of the code.
    I sure don't feel like I'm in charge of my code..lol. I'm not getting any compile errors but it doesn't seem to be working correctly. When I add a stock to the List as below is this becoming Index 1 or just the 1st item in a list?
    _stocks.Add(new StockEntry { Name = "ABC", Period = PeriodType.Minute, Value = 5, Count = 0 } );

    Because later on in the script I have the for loop which is supposed to be pushing through index 1 or stock 1, then Index 2 or stock 2 and so on.
    for (int series = 0; series < 5; series++)
    if (BarsInProgress == series)
    //conditions for stock entry

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

    Re: count++

    You've reached the point where you need to read up on List<> in msdn and debug you code by stepping through it. To set a breakpoint, use the mouse to click on the
    Code:
    for(int series = 0,...)
    line and press F9. You'll see a little red dot appear to the left. When the program runs (press F5 to start the program in debug mode), it will stop on this breakpoint and allow you to look at the values of the variables. This will answer you questions (along with reading the msdn documentation) on whether the first item added to a list is added at index 0 or 1.

    To learn how to program, you'll need to spend some time doing tutorials, reading msdn documentation and debugging (i.e. stepping through your code).

  4. #34
    Join Date
    Aug 2013
    Posts
    25

    Re: count++

    Ok I will run some breakpoints. It's sounds like you are sure though the items added are taking on Index #'s for sure, I wasn't sure if they were taking on any Index at all and just going straight to a List or something. Just one question before I go off on a venture on msdn. Seeing I switched from a dictionary to a list does the Count code change from this....

    EnterLong(200);
    {
    if(_stocks.ContainsKey(series))
    {
    _stocks[series].Count++;
    }
    }

    To just this?...

    EnterLong(200);
    _stocks[series].Count++;

  5. #35
    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
    Ok I will run some breakpoints. It's sounds like you are sure though the items added are taking on Index #'s for sure, I wasn't sure if they were taking on any Index at all and just going straight to a List or something. Just one question before I go off on a venture on msdn. Seeing I switched from a dictionary to a list does the Count code change from this....

    EnterLong(200);
    {
    if(_stocks.ContainsKey(series))
    {
    _stocks[series].Count++;
    }
    }

    To just this?...

    EnterLong(200);
    _stocks[series].Count++;
    I could answer this, but I'm sure we'd keep going and I'm running out of time. Not to mention that I've been answering frome my smart phone so formatting code responses is time consuming.

    You really shouldn't think of looking into msdn as venturing off. It should be you first place to find basic syntax answers and general .net class answers. If you enter "List<> class msdn" into a bing or google search, you'll find some links that take you to the msdn docs.

    You can also search for "C# List<> tutorial" which should answer your question of whether an index gets created when you insert an item into a list. Actually, I've already told you how to find that answer when I said to create a test project and try things out.

    You can continue to ask questions on a forum, but if you want to really learn, you'll need to roll your sleeves up, set some breakpoints, debug and understand the code you are writing.

  6. #36
    Join Date
    Aug 2013
    Posts
    25

    Re: count++

    Quote Originally Posted by Arjay View Post
    I could answer this, but I'm sure we'd keep going and I'm running out of time. Not to mention that I've been answering frome my smart phone so formatting code responses is time consuming.

    You really shouldn't think of looking into msdn as venturing off. It should be you first place to find basic syntax answers and general .net class answers. If you enter "List<> class msdn" into a bing or google search, you'll find some links that take you to the msdn docs.

    You can also search for "C# List<> tutorial" which should answer your question of whether an index gets created when you insert an item into a list. Actually, I've already told you how to find that answer when I said to create a test project and try things out.

    You can continue to ask questions on a forum, but if you want to really learn, you'll need to roll your sleeves up, set some breakpoints, debug and understand the code you are writing.
    I've studied the msdn docs. It seems like the text on lists and count is just not advanced enough to tackle what I'm trying to accomplish. I've tried running breakpoints but it doesn't give the information I'm seeking. The only place where I see the breakpoints useful is where I have the indexes or series looping through, I can see the index 1,2, 3 etc. looping through. But when I try to set a breakpoint on _stocks[series].Count++ or on if (_stocks[series].Count < 1) to see which series is routing through it doesn't run or I am unable to pin it as I can in other areas where series is in the script. I must be missing something?

  7. #37
    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've studied the msdn docs. It seems like the text on lists and count is just not advanced enough to tackle what I'm trying to accomplish. I've tried running breakpoints but it doesn't give the information I'm seeking. The only place where I see the breakpoints useful is where I have the indexes or series looping through, I can see the index 1,2, 3 etc. looping through.
    Text on lists? This doesn't make much sense because a list can store any data type. It can be as complex or advanced as you need it to be. When your code hits a breakpoint you can inspect any variable in scope including lists, the contents of lists and so on. You can also open the watch and locals windows while debugging. The watch windows allows you to add the variables to inspect while the locals window displays the variables that are currently in scope. If you want additional help, pm me.

  8. #38
    Join Date
    Aug 2013
    Posts
    25

    Re: count++

    Quote Originally Posted by Arjay View Post
    Text on lists? This doesn't make much sense because a list can store any data type.
    Oh I just meant the documentation regarding Lists. I'll PM after I look into the breakpoints a bit more. thanks

  9. #39
    Join Date
    Aug 2013
    Posts
    25

    Re: count++

    Quote Originally Posted by Arjay View Post
    You can use a List. As far as the documentation saying a dictionary isn't supported, I call B.S. If you are able to write supporting code, you can choose any storage method you want and it the program won't be impacted per se.

    I recommended a dictionary because early on it wasn't clear how the entries were accessed, but use a List if it makes more sense. Remember, you are in charge of the code.

    Received some further info regarding the Add method. It's not that List is not supported in the Ninja Trader Software, it's the fact that in order for a stocks data(such as open, close, high, low data within a 5min period) to be pulled from the local database into the script the stock needs to be added in this way in Initialize()... Add("ABC", PeriodType.Minute, 5); This added stock then takes on Index 1 and this Index now carries all the stocks data throughout the script to make further decisions.

    When it's setup as _stocks.Add(new StockEntry("ABC", PeriodType.Minute, 5)); All this is doing is adding this stock to the internal list, it's not pulling in the stock data for stock "ABC" from the local database.

    So is there a way I can add all the stocks as ... Add("ABC", PeriodType.Minute, 5); where it takes on Index 1 and have it Count how many times Index 1 passes through the method of EnterLong() that's further along in the script? EnterLong is basically a method that executes a trade after it passes all conditions in the script.

  10. #40
    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
    Received some further info regarding the Add method. It's not that List is not supported in the Ninja Trader Software, it's the fact that in order for a stocks data(such as open, close, high, low data within a 5min period) to be pulled from the local database into the script the stock needs to be added in this way in Initialize()... Add("ABC", PeriodType.Minute, 5); This added stock then takes on Index 1 and this Index now carries all the stocks data throughout the script to make further decisions.
    Not quite. Whenever you call the Add(...) method, it is going to add an entry and its index will start at 0 (zero-based index). So if you have two stock entries, the first will be accessed at index 0 and the second one will be index 1.
    Quote Originally Posted by zirjeo View Post
    When it's setup as _stocks.Add(new StockEntry("ABC", PeriodType.Minute, 5)); All this is doing is adding this stock to the internal list, it's not pulling in the stock data for stock "ABC" from the local database.
    I realize how it works. I'm trying to educate you on how it works. When you add to _stocks, it's going to add any info you tell it to add initially. If you need to gather additional info, then it's up to you to add it and up to you to update it later in the program.
    Quote Originally Posted by zirjeo View Post
    So is there a way I can add all the stocks as ... Add("ABC", PeriodType.Minute, 5); where it takes on Index 1 and have it Count how many times Index 1 passes through the method of EnterLong() that's further along in the script? EnterLong is basically a method that executes a trade after it passes all conditions in the script.
    You seem to misunderstand how the index works. If you add multiple stocks, not every one is going to be at index 1. If you want more help, you are going to have to post the complete code. I understand that you may not want to publicly post the code, so that is why I have suggested for you to pm me. We're at 40 posts right now, it's going to be much simpler for me to modify your code rather than keep going round and round explaining the same things over again in conversation.

  11. #41
    Join Date
    Aug 2013
    Posts
    25

    Re: count++

    Quote Originally Posted by Arjay View Post
    Not quite. Whenever you call the Add(...) method, it is going to add an entry and its index will start at 0 (zero-based index). So if you have two stock entries, the first will be accessed at index 0 and the second one will be index 1.
    I realize that my coding is very beginner but I know for a fact it doesn't work like this with this software, or at least not exactly. The default stock or 0 index is the stock that is highlighted in the software's strategy or backtesting analyzer window, then stocks Added to Initialize(), are just that, they are Added stocks. So the first Added stock takes on Index 1. But if one was to have the same stock that is already highlighted and defaulted in the software also as the first stock Added under Initialize() it will still take on Index 0. So the default Index 0 stock doesn't even have to be Added under Initialize(). I realize though that you may still need it in the Add list so it can be "Counted" or tracked.

    Quote Originally Posted by Arjay View Post
    You seem to misunderstand how the index works. If you add multiple stocks, not every one is going to be at index 1.
    I was never under the impression in any way that they are all at index 1. I realize the Index increases, that's why I had the for loop in the code, as you will see below, to iterate through all the different Indexes(stocks).

    Quote Originally Posted by Arjay View Post
    When you add to _stocks, it's going to add any info you tell it to add initially.
    Bottom line here is, for it to pull info in from the local database it has to be in this format under Initialize() Add("ABC"....), putting _stocks. in front of Add is not allowing this data to come through as far as I have seen so far. Putting _stocks in front of it seems to simply be just adding it to the List directory.

    Quote Originally Posted by Arjay View Post
    If you want more help, you are going to have to post the complete code. I understand that you may not want to publicly post the code, so that is why I have suggested for you to pm me.
    I don't mind at all posting the code. The main purpose of this conquest is to be able to Count the # of trades that have taken place per Index or Stock. The bool statement in the script below is looking for that Count...

    Code:
    namespace NinjaTrader.Strategy
    {
    
    public class MAcrossLongExperimentMultiTESTKono : Strategy
        {
    #region Variables
    private int fast = 3; 
    private int slow = 50;
    private double distance = .03;
    
    public class StockEntry //maybe this should be a struct?
    {
      public string Name;
      public PeriodType Period;
      public int Value;
      public int Count;
    
    public StockEntry(string Name, PeriodType Period, int Value, int Count)
    {
    	this.Name = Name;
    	this.Period = Period;
    	this.Value = Value;
    	this.Count = Count;
    }	
    }
    List<StockEntry> _stocks = new List<StockEntry>();
    
     protected override void Initialize()
            {						
    
    //5min bars
    _stocks.Add(new StockEntry("ABC", PeriodType.Minute, 5, 0)); //Index 0 seeing stock "ABC" is the default stock highlighted in the software			
    _stocks.Add(new StockEntry("ACE", PeriodType.Minute, 5, 0));
    _stocks.Add(new StockEntry("ACN", PeriodType.Minute, 5, 0));
    _stocks.Add(new StockEntry("ADT", PeriodType.Minute, 5, 0));
    _stocks.Add(new StockEntry("SCTY", PeriodType.Minute, 5, 0));
    
    //1min bars
     _stocks.Add(new StockEntry("ABC", PeriodType.Minute, 1, 0));	//Index 5.  In for loop I have as series +5, where series=0		
    _stocks.Add(new StockEntry("ACE", PeriodType.Minute, 1, 0));
    _stocks.Add(new StockEntry("ACN", PeriodType.Minute, 1, 0));
    _stocks.Add(new StockEntry("ADT", PeriodType.Minute, 1, 0));
    _stocks.Add(new StockEntry("SCTY", PeriodType.Minute, 1, 0));
    
    Add(SMA(Fast));
    Add(SMA(Slow));
    			
    			EntriesPerDirection = 1;
    			EntryHandling = EntryHandling.UniqueEntries;
    							
                CalculateOnBarClose = true;
    }
    
    protected override void OnBarUpdate()
            {
    for (int series = 0; series < 5; series++)
    			if (BarsInProgress == series + 5) //OnBarUpdate called for 1min bars
    			{
                           bool enterTrade = false;
                           if (_stocks[series].Count < 1) // if particular stock has 0 count enterTrade
    				{
    				enterTrade = true;
    				}
    				else
    				{
    				enterTrade = BarsSinceEntry(series, "", 0) > 2;  //otherwise make sure at least two 5min bars have elapsed ie. 10 min
    				} 
    
                    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 + 5][0] > Highs[series][1] + distance 
    					&& SMA(BarsArray[series],Slow)[1] > SMA(BarsArray[series],Slow)[2] + .01)
    
                                                     EnterLong(200); // buy 200 shares
    					 
    						_stocks[series].Count++;  // if a particular stock is bought Count and store it for use in the bool statement above.
    
    }
    }
    }
    }
    Last edited by zirjeo; October 4th, 2013 at 03:49 PM.

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

    Re: count++

    Zip up the complete solution and pm it to me. I can't tell from the code you posted what is exposed by the Strategy class and I need to know this in order to find out how to access the internal collection.

  13. #43
    Join Date
    Aug 2013
    Posts
    25

    Re: count++

    I can't seem to send an attachment through the PM, here it is. Thank You for taken a look.
    Attached Files Attached Files

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

    Re: count++

    There is a problem with sending attachments here. PM me and I will give you my email to send the attachment to.

Page 3 of 3 FirstFirst 123

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