#region imports from AlgorithmImports import * #endregion class LiquidAssetsUniverseSelectionModel(FineFundamentalUniverseSelectionModel): def __init__(self, algorithm: QCAlgorithm, universe_settings: UniverseSettings = None, universe_size: int = 100) -> None: self.algorithm = algorithm self.universe_size = universe_size self.hours = None self.month = -1 super().__init__(self._select_coarse, self._select_fine, universe_settings) # Define the coarse universe selector function def _select_coarse(self, coarse: List[CoarseFundamental]) -> List[Symbol]: if not self.hours or self.algorithm.live_mode: self.hours = self.algorithm.market_hours_database.get_entry(Market.USA, "SPY", SecurityType.EQUITY).exchange_hours self.next_open = self.hours.get_next_market_open(self.algorithm.time, False) # Refresh monthly if self.month == self.next_open.month: return Universe.UNCHANGED # Select the 100 most liquid stocks that have fundamental data selected = [c for c in coarse if c.has_fundamental_data] sorted_by_dollar_volume = sorted(selected, key=lambda c: c.dollar_volume, reverse=True) return [c.symbol for c in sorted_by_dollar_volume[:self.universe_size]] # Define the fine universe selector function def _select_fine(self, fine: List[FineFundamental]) -> List[Symbol]: if self.month == self.next_open.month: return Universe.UNCHANGED self.month = self.next_open.month # Select all of the Symbol objects from the coarse selector function return [c.symbol for c in fine]