Selecting a Daily Equity Universe by Dollar Volume
Summary
This educational example shows how to define a dynamic equity universe using a built-in selection helper. It selects the three stocks with the highest dollar volume and applies daily data resolution to securities added through the universe. The algorithm responds to membership changes: it submits market orders for newly added securities and liquidates holdings that leave the selection. The example also records whether the securities-changed event was called and raises an assertion at the end if the event never occurred.
The code illustrates universe membership handling rather than a complete investment strategy. It uses a short sample date range and a fixed order quantity, and it does not provide performance analysis, transaction cost assumptions, or a portfolio allocation method. Ranking by dollar volume favors actively traded stocks, but the example does not address survivorship bias, rebalancing effects, or whether the resulting holdings suit a particular objective. Its main value is as a compact demonstration of universe selection and lifecycle events in an algorithmic trading framework.
Key ideas
- A built-in universe helper can select a fixed number of stocks ranked by dollar volume.
- Universe settings determine the data resolution for selected securities.
- The securities-changed event provides lists of additions and removals to process.
- The example buys newly added names and liquidates invested names that leave the universe.
- This is a framework demonstration, not evidence of a profitable portfolio strategy.
Tags
Full text
# UniverseSelectionDefinitionsAlgorithm
# UniverseSelectionDefinitionsAlgorithm
This algorithm shows some of the various helper methods available when defining universes
## 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 some of the various helper methods available when defining universes
### </summary>
### <meta name="tag" content="using data" />
### <meta name="tag" content="universes" />
### <meta name="tag" content="coarse universes" />
class UniverseSelectionDefinitionsAlgorithm(QCAlgorithm):
def initialize(self):
# subscriptions added via universe selection will have this resolution
self.universe_settings.resolution = Resolution.DAILY
self.set_start_date(2014,3,24) # Set Start Date
self.set_end_date(2014,3,28) # Set End Date
self.set_cash(100000) # Set Strategy Cash
# add universe for the top 3 stocks by dollar volume
self.add_universe(self.universe.top(3))
self.changes = None
self.on_securities_changed_was_called = False
def on_data(self, data):
if self.changes is None: return
# liquidate securities that fell out of our universe
for security in self.changes.removed_securities:
if security.invested:
self.liquidate(security.symbol)
# invest in securities just added to our universe
for security in self.changes.added_securities:
if not security.invested:
self.market_order(security.symbol, 10)
self.changes = None
# this event fires whenever we have changes to our universe
def on_securities_changed(self, changes):
self.changes = changes
self.on_securities_changed_was_called = True
def on_end_of_algorithm(self):
if not self.on_securities_changed_was_called:
raise AssertionError("OnSecuritiesChanged() method was never called!")
```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.