Universe Selection

Manual Universes

Introduction

The ManualUniverseSelectionModel selects a static, fixed set of assets. It is similar to adding securities with the traditional AddSecurityadd_security 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 Initializeinitialize method, call the AddUniverseSelection method. The ManualUniverseSelectionModel constructor expects a list of Symbol objects that represent the universe constituents.

// Use ManualUniverseSelectionModel method to select a static set of equities, similar to traditional add_security methods, for a fixed trading universe.
var tickers = new[] {"SPY", "QQQ", "IWM"};
var symbols = tickers.Select(ticker => QuantConnect.Symbol.Create(ticker, SecurityType.Equity, Market.USA));
AddUniverseSelection(new ManualUniverseSelectionModel(symbols));
# Use ManualUniverseSelectionModel method to select a static set of equities, similar to traditional add_security methods, for a fixed trading universe.
tickers = ["SPY", "QQQ", "IWM"]
symbols = [ Symbol.create(ticker, SecurityType.EQUITY, Market.USA) for ticker in tickers]
self.add_universe_selection(ManualUniverseSelectionModel(symbols))

The following table describes the arguments the model accepts:

ArgumentData TypeDescriptionDefault Value
symbolsIEnumerable<Symbol>List[Symbol]Universe constituents
universeSettingsuniverse_settingsUniverseSettingsThe universe settings. If you don't provide an argument, the model uses the algorithm.UniverseSettingsalgorithm.universe_settings by default.None

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

// Create a custom universe selection model to move universe tickers and Symbol objects outside of the algorithm class for improved modularity, reusability, and maintainability.
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));
    }
}
# Create a custom universe selection model to move universe tickers and Symbol objects outside of the algorithm class for improved modularity, reusability, and maintainability.
self.add_universe_selection(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: