Back

List of symbols from universe

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

Update Backtest








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;
}

 

1

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.


Thanks Alexandre, I figured out it was a dictionary, but had no idea it's variable name was UniverseManager. It really helped.

Tayler

0

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.

0

@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.

1


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.

0

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);
}
}

}
}
}

 

0

I found the error. I had an extra } in my code. Sorry to waste your time.

0

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);
}

}
}
}

 

0

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.

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.


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);

 

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.


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?

0


Sorry we do not perform universe selection during warm up. For this you'll have to set your start date a little earlier.

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.


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.

0

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;


}
}
}

 

0

Nevermind. It still has problems.

0

What are you warming up? Can you just use history?
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.


In the warm up, I have to run each symbol through a function that gets history and uses it.

0

Is there a way to convert each symbol from the universe to a string class instead of a Symbol class?

0

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

 

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.


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.

0

AddEquity creates a Security object and subcribes to data updates. In the process of creating a Security, a Symbol object is also created, it is added to a cache so that we can use either the Symbol object or the string (implicit conversion) in all the methods that required a Symbol object as input.
Please add a concrete example of the trouble you are having. Let's make it work!

1

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.


Hi 

How about python? Is there a equivalent for "userdefineduniverse" for python? Or is there a "finefiltereduniverse forpython"?

0

https://www.quantconnect.com/forum/discussion/3682/algo-framework-etf-momentum-rebalancing-using-mean-variance-optimizationhttps://github.com/QuantConnect/Lean/blob/56bbc4c3da747dcf021c8221eb54b43ad075b3d7/Algorithm.Python/ConstituentsQC500GeneratorAlgorithm.pyhttps://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/UniverseSelectionRegressionAlgorithm.pyhttps://github.com/QuantConnect/Lean/blob/cbd953437f987392f2c2e154adcb7d43d684b7f1/Algorithm.Python/CoarseFineFundamentalComboAlgorithm.pyhttps://github.com/QuantConnect/Lean/blob/cbd953437f987392f2c2e154adcb7d43d684b7f1/Algorithm.Python/EmaCrossUniverseSelectionAlgorithm.py
0

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