Universe Selection
Futures Universes
Future Universe Selection
The FutureUniverseSelectionModel
selects all the contracts for a set of Futures you specify. To use this model, provide a refreshInterval
and a selector function. The refreshInterval
defines how frequently LEAN calls the selector function. The selector function receives a
DateTime
datetime
object that represents the current Coordinated Universal Time (UTC) and returns a list of Symbol
objects. The Symbol
objects you return from the selector function are the Futures of the universe.
AddUniverseSelection(new FutureUniverseSelectionModel(refreshInterval, futureChainSymbolSelector));
from Selection.FutureUniverseSelectionModel import FutureUniverseSelectionModel self.AddUniverseSelection(FutureUniverseSelectionModel(refreshInterval, futureChainSymbolSelector))
The following table describes the arguments the model accepts:
Argument | Data Type | Description | Default Value |
---|---|---|---|
refreshInterval | TimeSpan timedelta | Time interval between universe refreshes | |
futureChainSymbolSelector | Func<DateTime, IEnumerable<Symbol>> Callable[[datetime], List[Symbol]] | A function that selects the Future symbols. To view the supported assets in the US Futures dataset, see Supported Assets. | |
universeSettings | UniverseSettings | Universe settings define attributes of created subscriptions, such as their resolution and the minimum time in universe before they can be removed | null None |
If you don't provide a universeSettings
argument, the algorithm.UniverseSettings
is used by default.
public override void Initialize() { AddUniverseSelection( new FutureUniverseSelectionModel(TimeSpan.FromDays(1), SelectFutureChainSymbols) ); } private static IEnumerable<Symbol> SelectFutureChainSymbols(DateTime utcTime) { return new[] { QuantConnect.Symbol.Create(Futures.Indices.SP500EMini, SecurityType.Future, Market.CME), QuantConnect.Symbol.Create(Futures.Metals.Gold, SecurityType.Future, Market.COMEX) }; }
from Selection.FutureUniverseSelectionModel import FutureUniverseSelectionModel def Initialize(self) -> None: universe = FutureUniverseSelectionModel(timedelta(days=1), self.select_future_chain_symbols) self.SetUniverseSelection(universe) def select_future_chain_symbols(self, utc_time: datetime) -> List[Symbol]: return [ Symbol.Create(Futures.Indices.SP500EMini, SecurityType.Future, Market.CME), Symbol.Create(Futures.Metals.Gold, SecurityType.Future, Market.COMEX) ]
This model uses the default Future contract filter, which doesn't select any Futures contracts. To use a different filter, subclass the FutureUniverseSelectionModel
and define a Filter
method. The Filter
method accepts and returns a FutureFilterUniverse
object to select the Futures contracts. The following table describes the filter methods of the FutureFilterUniverse
class:
Method | Description |
---|---|
StandardsOnly() | Selects standard contracts |
IncludeWeeklys() | Selects non-standard weekly contracts |
WeeklysOnly() | Selects weekly contracts |
FrontMonth() | Selects the front month contract |
BackMonths() | Selects the non-front month contracts |
BackMonth() | Selects the back month contracts |
Expiration(TimeSpan minExpiry, TimeSpan maxExpiry) Expiration(minExpiry: timedelta, maxExpiry: timedelta) | Selects contracts that expire within a range of dates relative to the current day |
Expiration(int minExpiryDays, int maxExpiryDays) Expiration(minExpiryDays: int, maxExpiryDays: int) | Selects contracts that expire within a range of dates relative to the current day |
Contracts(IEnumerable<Symbol> contracts) Contracts(contracts: List[Symbol]) | Selects a list of contracts |
Contracts(Func<IEnumerable<Symbol>, IEnumerable< Symbol>> contractSelector) Contracts(contractSelector: Callable[[List[Symbol]], List[Symbol]]) | Selects contracts that a selector function selects |
OnlyApplyFilterAtMarketOpen() | Instructs the engine to only filter contracts on the first time step of each market day |
Depending on how you define the contract filter, LEAN may call it once a day or at every time step.
To move the selection functions outside of the algorithm class, create a universe selection model that inherits the FutureUniverseSelectionModel
class.
// In Initialize AddUniverseSelection(new FrontMonthFutureUniverseSelectionModel()); // Outside of the algorithm class class FrontMonthFutureUniverseSelectionModel : FutureUniverseSelectionModel { public FrontMonthFutureUniverseSelectionModel() : base(TimeSpan.FromDays(1), SelectFutureChainSymbols) {} private static IEnumerable<Symbol> SelectFutureChainSymbols(DateTime utcTime) { return new List<Symbol> { QuantConnect.Symbol.Create(Futures.Indices.SP500EMini, SecurityType.Future, Market.CME), QuantConnect.Symbol.Create(Futures.Metals.Gold, SecurityType.Future, Market.COMEX) }; } protected override FutureFilterUniverse Filter(FutureFilterUniverse filter) { return filter.FrontMonth().OnlyApplyFilterAtMarketOpen(); } }
from Selection.FutureUniverseSelectionModel import FutureUniverseSelectionModel # In Initialize self.AddUniverseSelection(FrontMonthFutureUniverseSelectionModel()) # Outside of the algorithm class class FrontMonthFutureUniverseSelectionModel(FutureUniverseSelectionModel): def __init__(self,) -> None: super().__init__(timedelta(1), self.select_future_chain_symbols) def select_future_chain_symbols(self, utcTime: datetime) -> List[Symbol]: return [ Symbol.Create(Futures.Indices.SP500EMini, SecurityType.Future, Market.CME), Symbol.Create(Futures.Metals.Gold, SecurityType.Future, Market.COMEX) ] def Filter(self, filter: FutureFilterUniverse) -> FutureFilterUniverse: return filter.FrontMonth().OnlyApplyFilterAtMarketOpen()
Some of the preceding filter methods only set an internal enumeration in the FutureFilterUniverse
that it uses later on in the filter process. This subset of filter methods don't immediately reduce the number of contract Symbol
objects in the FutureFilterUniverse
.
To view the implementation of this model, see the LEAN GitHub repositoryLEAN GitHub repository.
Open Interest Future Universe Selection
The OpenInterestFutureUniverseSelectionModel
is an extension of the FutureUniverseSelectionModel
that selects the contract with the greatest open interest on a daily basis.
AddUniverseSelection(new OpenInterestFutureUniverseSelectionModel(algorithm, futureChainSymbolSelector));
The following table describes the arguments the model accepts:
Argument | Data Type | Description | Default Value |
---|---|---|---|
algorithm | IAlgorithm | Algorithm | |
futureChainSymbolSelector | Func<DateTime, IEnumerable<Symbol>> | A function that selects the Future symbols. To view the supported assets in the US Futures dataset, see Supported Assets. | |
chainContractsLookupLimit | int? | Limit on how many contracts to query for open interest | 6 |
resultsLimit | int? | Limit on how many contracts will be part of the universe | 1 |
public override void Initialize() { var symbols = new[] { QuantConnect.Symbol.Create(Futures.Indices.SP500EMini, SecurityType.Future, Market.CME), QuantConnect.Symbol.Create(Futures.Metals.Gold, SecurityType.Future, Market.COMEX) }; AddUniverseSelection(new OpenInterestFutureUniverseSelectionModel(this, dateTime => symbols)); }
To move the selection functions outside of the algorithm class, create a universe selection model that inherits the OpenInterestFutureUniverseSelectionModel
class.
// In Initialize AddUniverseSelection(new GoldOpenInterestFutureUniverseSelectionModel(this)); // Outside of the algorithm class class GoldOpenInterestFutureUniverseSelectionModel : OpenInterestFutureUniverseSelectionModel { public GoldOpenInterestFutureUniverseSelectionModel(QCAlgorithm algorithm, int? chainContractsLookupLimit = 6, int? resultsLimit = 1) : base(algorithm, SelectFutureChainSymbols, chainContractsLookupLimit, resultsLimit) {} private static IEnumerable<Symbol> SelectFutureChainSymbols(DateTime utcTime) { return new List<Symbol> { QuantConnect.Symbol.Create(Futures.Metals.Gold, SecurityType.Future, Market.COMEX) }; } }
To view the implementation of this model, see the LEAN GitHub repository.
OpenInterestFutureUniverseSelectionModel
isn't currently available for Python. To track the feature progress, subscribe to GitHub Issue #6352.