Back

Working with a universe

As most of the code examples I see here relate to single stocks, I'm not sure how to apply an algorithm on individual stocks taken out of the selected universe. 

In the most basic level: how does the algorithm for logging out the stocks contained in a universe look like?

Update Backtest








Hi Eyal, this example calls the securities changed event which contains all the Added and Removed assets from the universe.

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 but this example shows (as you said) how to address securities that are in the added or removed lists of the universe. But what if I want to handle all the securities that ARE in the universe, not just those added in the last update? or maybe there's something fundamental that I don't understand aboutthis object?

0

I am looking for how to do this as well. I tried integrating into single-stock code hoping it would run like a matrix of values, but I get an error that Symbol is a "method group".

0

The UniverseManager holds the universes collections and allows you to access the "members" of a universe:

var universe = UniverseManager["my-universe-symbol"].Members;

The universe symbol/name depends on what universe you're using (custom, or coarse etc).

 Coarse universes are called: "qc-universe-coarse-usa".



var universe in the above example is a type: Dictionary<Symbol, Security Members>

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.


I'm not sure if it's my unfimiliarity with quantconnect or my noob c# knowledge, but when i build on what you just wrote to say:

var u=UniverseManager["qc-universe-coarse-usa"].Members;
foreach Symbol in u {
Log ("blip");
}

I get:

 

Cannot convert type 'System.Collections.Generic.KeyValuePair' to 'QuantConnect.Symbol'

0

Hi Eyal, 

Please try:

var members = UniverseManager["qc-universe-coarse-usa"].Members;
foreach(var member in members)
{
Log(member + " is qc-universe-coarse-usa member.");
}
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.


Members is a dictionary, you'll need to use the Key-Value-Pair. 

foreach(var security in members.Values)
{
//Security object
security.Symbol; // & Holdings etc.
​​​​​​​}

 

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.


Thank you so much, Jared and Alexandre. If I understand you both correctly, I should be using:
 

var uMembers=UniverseManager["qc-universe-coarse-usa"].Members;
foreach (var member in uMembers) {
Log (member.Symbol+" blip");
}

for which I get:
'KeyValuePair' does not contain a definition for 'Symbol' and no extension method 'Symbol' accepting a first argument of type 'KeyValuePair' could be found (are you missing a using directive or an assembly reference?)

So did I have to import anything to do that?
Thanks again for your patience as I'm making my first steps in QuantConnect.

0

Eyal,

You can use:

var uMembers=UniverseManager["qc-universe-coarse-usa"].Members;
foreach (var member in uMembers)
{
Log (member.Key + " blip");
}

or

var uMembers=UniverseManager["qc-universe-coarse-usa"].Members;
foreach (var member in uMembers.Values)
{
Log (member.Symbol + " blip");
}

if you just want to print out the member's symbol value.

The second option (Jared's input) lets you access all the security properties: Symbol, Price, Holdings, etc. 
 

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.


How would I get something like this to work:

UniverseSettings.Resolution = Resolution.Daily;
AddUniverse(coarse => {
return (from c in coarse
where c.Price > 15 & c.Volume > 1000000
orderby c.Symbol descending
select c.Symbol).Take(5);
});

var universe = UniverseManager["qc-universe-coarse-usa"].Members;

foreach(var security in universe)
{

// create a 15 day exponential moving average
fast = EMA(security.Symbol +, 15, Resolution.Daily);

// create a 30 day exponential moving average
slow = EMA(security.Symbol +, 30, Resolution.Daily);

}

I essentially want to run the algorithm for all stocks meeting Universe criteria.

0

Hi Gage,

I am attaching an algorithm that selects stocks from a universe based in moving averages cross. You can adapt it to include a filter for price and volume:

AddUniverse(coarse =>
{
return (from cf in coarse

// Price and volume filter
where c.Price > 15 & c.Volume > 1000000

// grab th SelectionData instance for this symbol
let avg = _averages
.GetOrAdd(cf.Symbol, sym => new SelectionData())
// Update returns true when the indicators are ready, so don't accept until they are
where avg.Update(cf.EndTime, cf.Price)
// only pick symbols who have their 50 day ema over their 100 day ema
where avg.Fast > avg.Slow*(1 + Tolerance)
// prefer symbols with a larger delta by percentage between the two averages
orderby avg.ScaledDelta descending
// we only need to return the symbol and return 'Count' symbols
select cf.Symbol).Take(Count);
});

 

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.


Is it possible to manage 2 universes? one for long and one for short? or maybe there's a more efficient way to maintain those 2 seperated lists (having different selection configurations)?

0

Hi Eyal,

You can add multiples universes.
I wrote a algorithm where we manage a universe for long and another for short in the spirit of your previous questions ("I want to handle all the securities that ARE in the universe"). It is buying the top 3 dollar volume stocks with price below $10 and shorting the top 3 dollar volume stocks with price above $10.

We now need to better stock picking/ranking method to make it profitable!

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 for sharing! I'll try to look at it soon.

Meanwhile - sidestepping the main issue of this thread - what amazes me is some preliminary results from trying to play with ema crossovers. When I previously looked at ema crossovers by eye-sampling a good number of stocks, it looked very, very promising to me as a basis for a strategy. And now when backtesting it here, it took my virtual account to almost 0. So that's also something I have to look into though i wondered if that's something any of the other readers of this thread has experienced or learned.

0

Bug?
foreach (var shortMember in universeLongMembers.Values)
Shouldn't it be:
foreach (var shortMember in universeShortMembers.Values)

0

Hi Eyal,

First, I rewrote the algorithm above. It had that bug you caught and it wasn't very smart to rebalance everyday.

I've worked with some discritionary traders that would easily fool themselves with eye-sampling results. It falls into known cognitive biases

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.


How can the _longUniverse be fine filtered, in this example?

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