Back

Working With Nested Class List Objects C#

Ok, my brain is about to explode. I'm sure someone with a little more knowledge than me can help. Most of my experience writing C# has been for small programs where I don't need to worry about inheritance and such. Fairly basic stuff.

So I have a few classes set up. One class has two values, the SymbolName and the data for that Symbol.

I have a sublist of that setup to house two different data sets of type SharePrices.

I am trying to figure out a way to add TradeBar data to the list of completeData for each symbolData for each corresponding SymbolName.

Basically I need to populate SharePrices, each minute, store that in list completeData which will happen for each symbolData corresponding to each SymbolName. I hope I explained that right. Any help is appreciated.

I have it set up to AddSecurity() correctly at initialization. But I can't seem to figure out how to add data more than one level deep.

public void AddSecurities()
{
for(int i = 0; i < symbolsList.Count; i++)
{
AddSecurity(SecurityType.Equity, symbolsList.ElementAt(i).ToString(), Resolution.Minute);
}
}
public void AddSymbols_toList()
{
for(int i = 0; i < SymbolsForAlgorithm.Length; i++)
{
symbolsList.Add(new SymbolList { SymbolName = SymbolsForAlgorithm[i]});
}
}
public class SymbolList
{
public string SymbolName { get; set; }
public List symbolData { get; set; } = new List();

public override string ToString()
{
return String.Format("{0}", SymbolName);
}
}
public class SymbolData
{
public List completeData { get; set; } = new List();
public List windowData { get; set; } = new List();
}
public class SharePrices
{
public DateTime Date { get; set; }
public decimal Price { get; set; }

public override string ToString()
{
return String.Format("{0}", "{1}", Date, Price);
}
}
Update Backtest








Hi Jack, what you are working with there is an object hierarchy, and all you need to do is remember that as you get an object, it has properties and methods that you can access.

So what you'd be wanting to do, in you're OnData method is something like this.

public void OnData(TradeBars data)
{
foreach (var bar in data)
{
symbolsList.Single(x => SymbolName == bar.Symbol).symbolData.completeData.Add(new SharePrices{Date = bar.Time, Price = bar.Close});
}
}


The first part is a bit of Linq to get the correct item from SymbolList, and may require a using System.Linq; at the top of your file.
0

Sorry, rubbish explanation really. I've got a bit more time to explain now.

Each time you call a method or property, you get back an object. Some times this is a native type like an int or a bool, other times, it's an instance of some other class. So "symbolList" is a List of type SymbolList. When you query that with Linq's "Single", it returns a single object matching the criteria set out inside the brackets, and this is of type SymbolList, so you can then query that object, and ask for the "symbolData" property, and you can query that object and ask for "completeData" and since that is a list, you can call the "Add" method.

Possibly a better way of visualising that workflow, is to write it out a little differently, like so

public void OnData(TradeBars data)
{
foreach (var bar in data)
{
symbolsList.Single(x => SymbolName == bar.Symbol)
.symbolData
.completeData
.Add(new SharePrices{Date = bar.Time, Price = bar.Close});
}
}
1

Paul, thank you so much for the help. I think one of the biggest issues I have is I haven't worked with LINQ in the past. I found a little LINQ cheat sheet on the web, but for some reason it still doesn't compute in my head.

I think your answer makes it clear. I guess the only area I might still not be clear on, is when I do and don't need to instance a new List when setting the data. I think I was trying to stack a bunch of list.Add(new TYPES, and then declaring another new TYPE within the {}.
0

I think as you've done, the best way to instantiate the lists is at object creation time. It helps if you know what some of the syntactic sugar is doing for you in the background.

when you do something like this: public List Test { get; set; } = new List();
what is really happening in the background is this:

private List _test = new List();
public List Test
{
get { return _test; }
set { _test = value; }
}


which in turn is this:

private List _test;
public List Test
{
get { return _test; }
set { _test = value; }
}

private ClassConstructor()
{
_test = new List();
}


And all that is really saying is: When I create an object, make sure that my "Test" property is instantiated with a new List of type int. This prevents you having to bother with tests for null all through the rest of your code.

You may already have known that, but it may be helpful to someone else who's starting out.
1

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed