I’m trying to follow the official Futures Universes documentation, and I subclassed FutureUniverseSelectionModel to override the filter method. However, even though my select_future_chain_symbols is used, the filter method seems to be completely ignored. I receive all contracts in on_data, not just the front month.
Why isn’t the filter method being executed?
from AlgorithmImports import *
from datetime import timedelta, datetime
class FrameworkFutureUniverseSelectionAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self.set_start_date(2022, 1, 1)
self.set_end_date(2022, 12, 31)
self.set_cash(100000)
self.universe_settings.asynchronous = True
self.universe_settings.extended_market_hours = True
self.universe_settings.resolution = Resolution.DAILY
self.universe_settings.data_mapping_mode = DataMappingMode.OPEN_INTEREST
self.universe_settings.data_normalization_mode = DataNormalizationMode.BACKWARDS_RATIO
self.universe_settings.contract_depth_offset = 0
self.add_universe_selection(FrontMonthFutureUniverseSelectionModel())
def on_data(self, data: Slice) -> None:
self.debug(f"{self.time}: === On Data ===")
self.debug(f"Count: {len(self.active_securities)}")
count = 0
for kvp in self.active_securities:
self.debug(f"Symbol: {kvp.key}")
class FrontMonthFutureUniverseSelectionModel(FutureUniverseSelectionModel):
def __init__(self) -> None:
super().__init__(timedelta(1), self.select_future_chain_symbols)
def select_future_chain_symbols(self, utc_time: datetime) -> list[Symbol]:
return [
Symbol.create(Futures.Metals.GOLD, SecurityType.FUTURE, Market.COMEX)
]
def filter(self, filter: FutureFilterUniverse) -> FutureFilterUniverse:
return filter.front_month()
Mia Alissi
The relevant bug is that your universe selection model subclasses 'FutureUniverseSelectionModel' (singular), while the QuantConnect framework requires you to subclass 'FuturesUniverseSelectionModel' (plural). When you inherit from the incorrect base class, your overridden filter method is never invoked, so all contracts are returned regardless of your filter. To resolve this, change your parent class to 'FuturesUniverseSelectionModel'. This correction will ensure your filter method is called and only the filtered contracts (e.g., front month) are provided by the universe.
Sources: Futures Universes - QuantConnect.com, Universes - QuantConnect.com, Future Universe Selection Model, Combined Carry and Trend, Add Future Universe Selection Model Regression Algorithm
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Hagiya
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!