Universe Selection

Manual Universes

Introduction

The ManualUniverseSelectionModel selects a static, fixed set of assets. It is similar to adding securities with the traditional AddSecurity API methods. If your algorithm has a static universe, you can use automatic indicators instead of manual indicators in your algorithm.

Manual universes can be prone to look-ahead bias. For example, if you select a set of securities that have performed well during the backtest period, you are incorporating information from the future into the backtest and the algorithm may underperform in live mode.

Add Manual Universe Selection

To add a ManualUniverseSelectionModel to your algorithm, in the Initialize method, call the AddUniverseSelection method. The ManualUniverseSelectionModel constructor expects a list of Symbol objects that represent the universe constituents. To create the Symbol objects, call the Symbol.Create method.

var tickers = new[] {"SPY", "QQQ", "IWM"};
var symbols = tickers.Select(ticker => QuantConnect.Symbol.Create(ticker, SecurityType.Equity, Market.USA));
AddUniverseSelection(new ManualUniverseSelectionModel(symbols));
tickers = ["SPY", "QQQ", "IWM"]
symbols = [ Symbol.Create(ticker, SecurityType.Equity, Market.USA) for ticker in tickers]
self.AddUniverseSelection(ManualUniverseSelectionModel(symbols))

To move the universe tickers and Symbol objects outside of the algorithm class, create a universe selection model that inherits the ManualUniverseSelectionModel class.

// In Initialize
AddUniverseSelection(new IndexUniverseSelectionModel());

// Outside of the algorithm class
class IndexUniverseSelectionModel : ManualUniverseSelectionModel
{
    public IndexUniverseSelectionModel()
        : base(SelectSymbols()) {}

    public static IEnumerable<Symbol> SelectSymbols()
    {
        var tickers = new[] {"SPY", "QQQ", "IWM"};
        return tickers.Select(ticker => QuantConnect.Symbol.Create(ticker, SecurityType.Equity, Market.USA));
    }
}
# In Initialize
self.AddUniverseSelection(IndexUniverseSelectionModel())

# Outside of the algorithm class
class IndexUniverseSelectionModel(ManualUniverseSelectionModel):
    def __init__(self):
        tickers = ["SPY", "QQQ", "IWM"]
        symbols = [Symbol.Create(ticker, SecurityType.Equity, Market.USA) for ticker in tickers]
        super().__init__(symbols)

To view the implementation of this model, see the LEAN GitHub repositoryLEAN GitHub repository.

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: