Research Environment

Universes

Introduction

Universe selection is the process of selecting a basket of assets to research. Dynamic universe selection increases diversification and decreases selection bias in your analysis.

Get Universe Data

Universes are data types. To get historical data for a universe, pass the universe data type to the UniverseHistory method. The object that returns contains a universe data collection for each day. With this object, you can iterate through each day and then iterate through the universe data objects of each day to analyze the universe constituents.

For example, follow these steps to get the US Equity Fundamental data for a specific universe:

  1. Load the assembly files and data types in their own cell.
  2. #load "../Initialize.csx"
  3. Load the necessary assembly files.
  4. #load "../QuantConnect.csx"
  5. Import the QuantConnect package and the universe data type.
  6. using QuantConnect;
    using QuantConnect.Research;
    using QuantConnect.Data.Fundamental;
  7. Create a QuantBook.
  8. var qb = new QuantBook();
    qb = QuantBook()
  9. Define a universe.
  10. The following example defines a dynamic universe that contains the 10 Equities with the lowest PE ratios in the market. To see all the Fundamental attributes you can use to define a filter function for a Fundamental universe, see Data Point Attributes. To create the universe, call the AddUniverseadd_universe method with the filter function.

    var universe = qb.AddUniverse(fundamentals =>
    {
        return fundamentals
            .Where(f => !double.IsNaN(f.ValuationRatios.PERatio))
            .OrderBy(f => f.ValuationRatios.PERatio)
            .Take(10)
            .Select(f => f.Symbol);
    });
    def filter_function(fundamentals):
        sorted_by_pe_ratio = sorted(
            [f for f in fundamentals if not np.isnan(f.valuation_ratios.pe_ratio)], 
            key=lambda fundamental: fundamental.valuation_ratios.pe_ratio
        )
        return [fundamental.symbol for fundamental in sorted_by_pe_ratio[:10]]
    
    universe = qb.add_universe(filter_function)
  11. Call the UniverseHistoryuniverse_history method with the universe, a start date, and an end date.
  12. var universeHistory = qb.UniverseHistory(universe, new DateTime(2023, 11, 6), new DateTime(2023, 11, 13));
    universe_history = qb.universe_history(universe, datetime(2023, 11, 6), datetime(2023, 11, 13))

    The end date arguments is optional. If you omit it, the method returns Fundamental data between the start date and the current day.

    The universe_history method returns a Series where the multi-index is the universe Symbol and the time when universe selection would occur in a backtest. Each row in the data column contains a list of Fundamental objects. The following image shows the first 5 rows of an example Series:

    Historical fundamental universe dataframe

    The UniverseHistory method returns an IEnumerable<IEnumerable<Fundamental>> object.

  13. Iterate through the Series to access the universe data.
  14. Iterate through the result to access the universe data.
  15. universe_history = universe_history.droplevel('symbol', axis=0)
    for date, fundamentals in universe_history.items():
        for fundamental in fundamentals:
            symbol = fundamental.symbol
            price = fundamental.price
            if fundamental.has_fundamental_data:
                pe_ratio = fundamental.valuation_ratios.pe_ratio
    foreach (var fundamentals in universeHistory)
    {
        foreach (Fundamental fundamental in fundamentals)
        {
            var date = fundamental.Time;
            var symbol = fundamental.Symbol;
            var price = fundamental.Price;
            if (fundamental.HasFundamentalData)
            {
                var peRatio = fundamental.ValuationRatios.PERatio;
            }
        }
    }

Available Universes

To get universe data for other types of universes, you usually just need to replace Fundamental in the preceding code snippets with the universe data type. The following table shows the datasets that support universe selection and their respective data type. For more information, about universe selection with these datasets and the data points you can use in the filter function, see the dataset's documentation.

Dataset NameUniverse Type(s)Documentation
US Fundamental DataFundamentalLearn more
US ETF ConstituentsETFConstituentUniverseLearn more
Crypto Price DataCryptoUniverseLearn more
US Congress TradingQuiverQuantCongresssUniverseLearn more
Wikipedia Page ViewsQuiverWikipediaUniverseLearn more
WallStreetBetsQuiverWallStreetBetsUniverseLearn more
Corporate Buybacks
  • SmartInsiderIntentionUniverse
  • SmartInsiderTransactionUniverse
Learn more
Brain Sentiment IndicatorBrainSentimentIndicatorUniverseLearn more
Brain ML Stock RankingBrainStockRankingUniverseLearn more
Brain Language Metrics on Company Filings
  • BrainCompanyFilingLanguageMetricsUniverseAll
  • BrainCompanyFilingLanguageMetricsUniverse10K
Learn more
CNBC TradingQuiverCNBCsUniverseLearn more
US Government ContractsQuiverGovernmentContractUniverseLearn more
Corporate LobbyingQuiverLobbyingUniverseLearn more
Insider TradingQuiverInsiderTradingUniverseLearn more

To get universe data for Futures and Options, use the FutureHistoryfuture_history and OptionHistoryoption_history methods, respectively.

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: