Universe Selection
Fundamental Universes
Introduction
A FundamentalUniverseSelectionModel
selects a universe of US Equities based on Fundamental
data. Depending on the Fundamental
properties you use, these universes rely on the US Equity Coarse Universe dataset, the US Fundamental dataset, or both.
These types of universes operate on daily schedule. In backtests, they select assets at midnight. In live trading, the selection timing depends on the data feed you use.
If you use a fundamental Universe Selection model, the only way to unsubscribe from a security is to return a list from the selection function that doesn't include the security Symbol
. The RemoveSecurity
method doesn't work with these types of Universe Selection models.
Fundamental Selection
The FundamentalUniverseSelectionModel
lets you select stocks based on corporate Fundamental
data.
You can specific the selection method, which takes a list of Fundamental
objects as argument and returns a list of Symbol
objects.
public override void Initialize() { AddUniverseSelection(new FundamentalUniverseSelectionModel(FundamentalFilterFunction)); } public override List<Symbol> FundamentalFilterFunction(List<Fundamental> fundamental) { return (from f in fundamental where f.HasFundamentalData && f.Price > 10 orderby f.ValuationRatios.PERatio select f.Symbol).Take(10); }
def Initialize(self) -> None: self.AddUniverseSelection(FundamentalUniverseSelectionModel(self.FundamentalFilterFunction)) def FundamentalFilterFunction(self, fundamental: List[Fundamental]): sorted_by_pe_ratio = sorted([f for f in fundamental if f.HasFundamentalData and f.Price > 10], key=lambda f: f.ValuationRatios.PERatio) return [f.Symbol for f in sorted_by_pe_ratio[:10]]
The following table describes the arguments the model accepts:
Argument | Data Type | Description | Default Value |
---|---|---|---|
selector |
Func<IEnumerable<Fundamental>, IEnumerable<Symbol>>
Callable[[List[Fundamental]], List[Symbol]] | Filter function to select assets based on fundamental data. | |
universeSettings | UniverseSettings | The universe settings. If you don't provide an argument, the model uses the algorithm.UniverseSettings by default. | None null |
The Fundamental
objects have the following properties:
To move the selection function outside of the algorithm class, create a universe selection model that inherits the FundamentalUniverseSelectionModel
class and override its Select
method.
// In Initialize AddUniverseSelection(new LiquidAndLowPEUniverseSelectionModel()); // Outside of the algorithm class public class LiquidAndLowPEUniverseSelectionModel : FundamentalUniverseSelectionModel { public override IEnumerableSelect(QCAlgorithm algorithm, IEnumerable fundamental) { return fundamental // select symbols with fundamental data and a price above $1 .Where(x => x.HasFundamentalData && x.Price > 1) // sort descending by daily dollar volume .OrderByDescending(x => x.DollarVolume) .Take(100) // sort descending by P/E ratio .OrderByDescending(x => x.ValuationRatios.PERatio) .Take(10) .Select(x => x.Symbol); } }
# In Initialize self.AddUniverseSelection(LiquidAndLowPEUniverseSelectionModel()) # Outside of the algorithm class from Selection.FundamentalUniverseSelectionModel import FundamentalUniverseSelectionModel class LiquidAndLowPEUniverseSelectionModel(FundamentalUniverseSelectionModel): def Select(self, algorithm: QCAlgorithm, fundamental: List[Fundamental]) -> List[Symbol]: most_liquid = sorted([x for x in fundamental if x.Price > 1], key=lambda x: x.DollarVolume)[-100:] lowest_pe_ratio = sorted(most_liquid, key=lambda x: x.ValuationRatios.PERatio)[:10] return [x.Symbol for x in lowest_pe_ratio]
To return the current universe constituents from the selection function, return Universe.Unchanged
.
To view the implementation of this model, see the LEAN GitHub repositoryLEAN GitHub repository.
EMA Cross Selection
The EmaCrossUniverseSelectionModel
applies two exponential moving average (EMA) indicators to the price history of assets and then selects the assets that have their fast EMA furthest above their slow EMA on a percentage basis.
public override void Initialize() { AddUniverseSelection(new EmaCrossUniverseSelectionModel()); }
def Initialize(self) -> None: self.AddUniverseSelection(EmaCrossUniverseSelectionModel())
The following table describes the arguments the model accepts:
Argument | Data Type | Description | Default Value |
---|---|---|---|
fastPeriod | int | Fast EMA period | 100 |
slowPeriod | int | Slow EMA period | 300 |
universeCount | int | Maximum number of members of this universe selection | 500 |
universeSettings | UniverseSettings | The universe settings. If you don't provide an argument, the model uses the algorithm.UniverseSettings by default. | None null |
To view the implementation of this model, see the LEAN GitHub repositoryLEAN GitHub repository.