Skip to content
All library documents

Rotating a User-Defined Stock Universe on an Hourly Schedule

Article Strategy library · Author: QuantConnect

Summary

This example demonstrates custom universe selection by cycling through a predefined list of stocks on an hourly schedule. The selection function uses the hour of the timestamp to choose one symbol from the list, while the universe and security data are configured at hourly resolution.

When securities leave the universe, invested positions are liquidated; when securities are added, the algorithm assigns holdings across the newly added securities. The example uses a fixed list of eight tickers and describes the broader pattern as rotating through a list of stocks. It illustrates how universe membership can be controlled directly by user logic rather than by a built-in fundamental or market-cap screen.

The document provides implementation behavior, not evidence of investment performance. The selection rule is clock-based and does not evaluate company data, prices, liquidity, or trading costs, so it should be understood as a framework example rather than a stock-selection strategy.

Key ideas

  • A custom universe can select securities according to user-defined logic and timing.
  • The example chooses a stock from a fixed list using the current hour.
  • Invested securities are liquidated when they leave the universe.
  • Newly added securities receive holdings based on the count of additions.
  • The example demonstrates universe mechanics and gives no evidence of trading returns.

Tags

Full text
# UserDefinedUniverseAlgorithm


# UserDefinedUniverseAlgorithm









This algorithm shows how you can handle universe selection in anyway you like, at any time you like. This algorithm has a list of 10 stocks that it rotates through every hour.

## Source (Apache-2.0)

```python
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from AlgorithmImports import *

### <summary>
### This algorithm shows how you can handle universe selection in anyway you like,
### at any time you like. This algorithm has a list of 10 stocks that it rotates
### through every hour.
### </summary>
### <meta name="tag" content="using data" />
### <meta name="tag" content="universes" />
### <meta name="tag" content="custom universes" />
class UserDefinedUniverseAlgorithm(QCAlgorithm):

	def initialize(self) -> None:
		self.set_cash(100000)
		self.set_start_date(2015, 1, 1)
		self.set_end_date(2015, 12, 1)
		self._symbols = ["SPY", "GOOG", "IBM", "AAPL", "MSFT", "CSCO", "ADBE", "WMT"]

		self.universe_settings.resolution = Resolution.HOUR
		self.add_universe('my_universe_name', Resolution.HOUR, self.selection)

	def selection(self, time: datetime) -> list[str]:
		index = time.hour % len(self._symbols)
		return [self._symbols[index]]

	def on_securities_changed(self, changes: SecurityChanges) -> None:
		for removed in changes.removed_securities:
			if removed.invested:
				self.liquidate(removed.symbol)

		for added in changes.added_securities:
			self.set_holdings(added.symbol, 1/len(changes.added_securities))

```

Shown in full with attribution under the source's licence. Licence: Apache-2.0

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.