I am trying to get a list of symbols for a universe so I can iterate through each symbol. This is so I dont have to create a symbol list manually. It can update based on the universe requirements.
Does anyone know how to do this?
Thanks,
Tayler Allen
Alexandre Catarino
Once added universes are stored in the IDictionary<string, Universe> UniverseManager.
We can loop through the dictionary entries to find which symbols are part of a given universe:
foreach (var universe in UniverseManager.Values) { // User defined universe has symbols from AddSecurity/AddEquity calls if (universe is UserDefinedUniverse) { continue; } var symbols = universe.Members.Keys; }
Tayler Allen
Thanks Alexandre, I figured out it was a dictionary, but had no idea it's variable name was UniverseManager. It really helped.
Tayler
Tayler Allen
Hi Alexandre, I'm still having trouble figuring this out. I need a list of symbols (from the universe) that I can iterate through in the onData method. When I try the code you posted above, I can't quit get a list I can iterate through.
Sofyan Saputra
@Taylor Just add in a foreach on the symbols and it will work. Attached is a backtest that loops through all the symbols and prints them out.
Tayler Allen
Thank you. That code works great but for some reason, if I put the foreach statement in my code, it won't work at all. There must be something wrong with my code.
Tayler Allen
using System; using System.Collections; using System.Collections.Generic; using QuantConnect.Securities; using QuantConnect.Models; namespace QuantConnect { // Name your algorithm class anything, as long as it inherits QCAlgorithm public class BasicTemplateAlgorithm : QCAlgorithm { //Initialize the data and resolution you require for your strategy: public override void Initialize() { SetStartDate(2013, 1, 1); SetEndDate(DateTime.Now.Date.AddDays(-1)); SetCash(25000); AddEquity("SPY", Resolution.Minute); // AddSecurity(SecurityType.Equity, "AAPL", Resolution.Minute); AddUniverse(Universe.DollarVolume.Top(50)); } } //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol. public override void OnData(Slice data) { foreach (var universe in UniverseManager.Values) { // User defined universe has symbols from AddSecurity/AddEquity calls if (universe is UserDefinedUniverse) { continue; } var symbols = universe.Members.Keys; foreach (Symbol symbol in symbols){ Debug(symbol); } } } } }
Tayler Allen
I found the error. I had an extra } in my code. Sorry to waste your time.
Tayler Allen
One more question. Is there any reason I shouldn't leave out the foreach like this?
It has the same outcome in this situation.
using System; using System.Collections; using System.Collections.Generic; using QuantConnect.Securities; using QuantConnect.Models; namespace QuantConnect { // Name your algorithm class anything, as long as it inherits QCAlgorithm public class BasicTemplateAlgorithm : QCAlgorithm { //Initialize the data and resolution you require for your strategy: public override void Initialize() { SetStartDate(2013, 1, 1); SetEndDate(DateTime.Now.Date.AddDays(-1)); SetCash(25000); AddEquity("SPY", Resolution.Minute); // AddSecurity(SecurityType.Equity, "AAPL", Resolution.Minute); AddUniverse(Universe.DollarVolume.Top(50)); } //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol. public override void OnData(Slice data) { var symbols = UniverseManager.Values.ElementAt(0).Members.Keys; foreach (Symbol symbol in symbols) { Debug(symbol); } } } }
Alexandre Catarino
In theory UniverseManager can have several universes.
How do you know that the universe you want the symbols from are at index 0?
In the particular case, there is only two universes: UserDefinedUniverse and Coarse Fundamental Universe, so you could leave the foreach in that form.
Jared Broad
PS: Sending all those debug messages will be extremely slow. You might want to concatenate them to a line and log:
// untested! :D
var symbolString = string.Join(",", symbols.select(x => x.ToString()));
Log(symbolString);
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.
Tayler Allen
Thanks Alexandre, that makes sense.
Now I have another problem. When I try to add a warm up period, it completely skips the warm up and says "finished warming up" before printing the symbols. Any suggestions?
Jared Broad
Sorry we do not perform universe selection during warm up. For this you'll have to set your start date a little earlier.
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.
Tayler Allen
If I set my start date a little earlier in a live algorthm, I would have to wait that long for it to start. That seems pretty inefficient.
Tayler Allen
I figured out a solution. I first created a list of symbols from the universe within the initialize() method. Then I used that list in the warmup instead.
namespace QuantConnect { /* * Basic Template Algorithm * * The underlying QCAlgorithm class has many methods which enable you to use QuantConnect. * We have explained some of these here, but the full base class can be found at: * https://github.com/QuantConnect/Lean/tree/master/Algorithm */ public class BasicTemplateAlgorithm : QCAlgorithm { List<Symbol> MySymbols = new List<Symbol>(); public override void Initialize() { // backtest parameters SetStartDate(2017, 8, 22); SetEndDate(2017, 8, 23); // cash allocation SetCash(25000); // request specific equities // including forex. Options and futures in beta. AddEquity("SPY", Resolution.Minute); //AddForex("EURUSD", Resolution.Minute); AddUniverse(Universe.DollarVolume.Top(50)); foreach (var universe in UniverseManager.Values) { // User defined universe has symbols from AddSecurity/AddEquity calls if (universe is UserDefinedUniverse) { continue; } List<Symbol> symbols = new List<Symbol> (universe.Members.Keys); foreach (Symbol symbol in symbols) { MySymbols.Add(symbol); } } foreach (Symbol symbol in MySymbols) { AddEquity(symbol, Resolution.Minute); } SetWarmup(10); } /* * New data arrives here. * The "Slice" data represents a slice of time, it has all the data you need for a moment. */ public override void OnData(Slice data) { foreach (string symb in Securities.Keys) { Log(symb); } if (IsWarmingUp) return; } } }
Tayler Allen
Nevermind. It still has problems.
Jared Broad
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.
Tayler Allen
In the warm up, I have to run each symbol through a function that gets history and uses it.
Tayler Allen
Is there a way to convert each symbol from the universe to a string class instead of a Symbol class?
Alexandre Catarino
Symbol and string are implicitly converted into each other:
// In Initialize
var symbol = AddEquity("SPY").Symbol
H1 = History("SPY", 10);
H2 = History(symbol, 10);
// H1 and H2 have the same values
Log(symbol.Value); // prints SPY
Tayler Allen
So how exactly does AddEquity() work? I am having trouble making trades in specific stocks by themselves, such as AAPL. But if I add SPY and then AAPL, AAPL will work.
Tayler Allen
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!