Hi, I'm writing an future algorithm using algorithm framework.
class FutureBuyandHold(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2013, 1, 1)
self.SetEndDate(2023, 12, 31)
self.SetCash (100000)
self.universe_settings.asynchronous = True
self.add_universe_selection(FrontMonthFutureUniverseSelectionModel())
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.Indices.SP500E_MINI, SecurityType.FUTURE, Market.CME),
Symbol.create(Futures.Indices.MICRO_SP_500_E_MINI, SecurityType.FUTURE, Market.CME),
]
def filter(self, filter: FutureFilterUniverse) -> FutureFilterUniverse:
return filter.front_month()
On top of the front_month() filter, I want to filter the futures which are on the last trading day. How can I do it?
Mia Alissi
To filter futures contracts that are on their last trading day, you can use the
Expiration
method of theFutureFilterUniverse
class. This method allows you to select contracts that expire within a range of dates relative to the current day.To select contracts on their last trading day, you can set both the minimum and maximum expiry to 0 days. Here's how you can modify your
filter
method:This will select the front month contracts that are on their last trading day.
Please note that the
Expiration
method only runs at the first time step of each day, so it will not continuously filter out contracts throughout the day.For more information, you can refer to the QuantConnect documentation on Futures Universes.
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.
Uhahahaha
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!