-
September 12th, 2013, 09:41 PM
#16
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);
-
September 16th, 2013, 01:19 AM
#17
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.
-
September 16th, 2013, 10:28 AM
#18
Re: count++
Originally Posted by Arjay
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.
-
September 16th, 2013, 05:15 PM
#19
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", ... } );
-
September 16th, 2013, 08:27 PM
#20
Re: count++
Originally Posted by Arjay
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?
-
September 17th, 2013, 12:42 AM
#21
Re: count++
Originally Posted by zirjeo
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.
-
September 17th, 2013, 07:40 PM
#22
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.
-
September 18th, 2013, 12:02 AM
#23
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).
-
September 19th, 2013, 08:23 PM
#24
Re: count++
Originally Posted by Arjay
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]++;
}
}
}
}
}
-
September 19th, 2013, 11:13 PM
#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.
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++;
-
September 20th, 2013, 08:02 AM
#26
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?
-
September 21st, 2013, 03:09 AM
#27
Re: count++
Originally Posted by zirjeo
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.
-
September 21st, 2013, 01:43 PM
#28
Re: count++
Originally Posted by Arjay
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.
-
September 21st, 2013, 02:15 PM
#29
Re: count++
Originally Posted by zirjeo
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.
-
September 22nd, 2013, 02:27 PM
#30
Re: count++
Originally Posted by Arjay
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|