Documentation
Documentation copied to clipboard
Monthly Universe Selections and Rebalancing in Framework Algorithms
Expected Behavior
We document a reliable way to perform universe selection and rebalancing on a monthly basis in framework algorithms.
The docs explain that the universe selection model's constructor should have
self.hours = None
and the selector function should have
if not self.hours or algorithm.LiveMode:
self.hours = algorithm.MarketHoursDatabase.GetEntry(Market.USA, "SPY", SecurityType.Equity).ExchangeHours
self.next_open = self.hours.GetNextMarketOpen(self.algorithm.Time, False)
if self.month == self.next_open.month:
return Universe.Unchanged
as demonstrated in this algorithm.
This content is explained in https://www.quantconnect.com/docs/v2/writing-algorithms/securities/exchange and the universe selection docs.
Actual Behavior
Undocumented. The standard way to perform universe selection and rebalancing is to add
if self.month == self.algorithm.Time.month:
return Universe.Unchanged
self.month = self.algorithm.Time.month
to the universe selection function and
if data.Time.month == self.month:
return []
self.month = data.Time.month
to the Alpha model Update method.
However, if the first trading day of the month is a Monday, then it leads to issues.
For example, in this backtest, the logs show
Universe selection at 2023-04-29 04:00:00 Universe selection at 2023-05-02 04:00:00
2023-05-01 is a Monday, so the Alpha model emits a bunch of insights at 2023-05-01 9:31 AM. Universe selection runs at 2023-05-02 4 AM. It removes some of the securities that we emit insights for, canceling their insights. When the warm-up period ends at 2023-05-18, only some of the insights are still active, so the portfolio rebalances for the first time and has 70% exposure.
Checklist
- [x] I have completely filled out this template
- [x] I have confirmed that this issue exists on the current
master
branch - [x] I have confirmed that this is not a duplicate issue by searching issues