Building a Stock Universe from a Securities Master Database
Summary
This tutorial explains how to define a reusable universe of securities for historical data collection and strategy research. It starts by querying a securities master database into a table, where each instrument has a unique security identifier. If the database contains many listings, the tutorial shows how to restrict the query to securities already present in a sample historical database.
The example then filters out exchange-traded funds because the intended universe is stocks, and uploads the remaining identifiers as a named universe. The demonstrated sample contains nine symbols when using the tutorial’s sample database, with one ETF excluded. This is an operational example rather than a trading signal or performance study: it teaches how to select and save a research universe, but does not compare universe definitions or test how screening choices affect results. The approach depends on the completeness and accuracy of the securities master and the selected historical listings.
Key ideas
- A universe is a reusable, user-defined group of securities for data collection and strategy runs.
- The securities master query returns listings with unique identifiers that can be used to build a universe.
- Queries can be limited to securities available in a selected historical database.
- The example excludes ETFs before saving a stock-only universe.
- The tutorial demonstrates setup steps but provides no evidence about trading performance.
Tags
Full text
# Define a universe
<a href="https://www.quantrocket.com"><img alt="QuantRocket logo" src="https://www.quantrocket.com/assets/img/notebook-header-logo.png"></a><br>
<a href="https://www.quantrocket.com/disclaimer/">Disclaimer</a>
***
[Moonshot Intro](Introduction.ipynb) › Part 2: Universe Selection
***
# Define a universe
QuantRocket relies heavily on the concept of universes, which are user-defined groupings of securities. Universes provide a convenient way to refer to and manipulate groups of securities when collecting historical data, running a trading strategy, etc. You can create universes based on exchanges, security types, sectors, liquidity, or any criteria you like. A universe could consist of one or two securities or thousands of securities.
## Query securities
To create our first universe, we will query securities from the securities master database, pare them down to a subset of securities, then upload the pared down securities to create our universe. Learn more about universes in the [usage guide](https:www.quantrocket.com/docs/#master).
Security listings for our sample stocks were automatically collected during historical data collection. We query the stock listings from the securities master database using the `get_securities` function, which loads them into a pandas DataFrame:
```python
from quantrocket.master import get_securities
securities = get_securities(vendors="usstock")
print(f"loaded {len(securities)} securities")
securities.head()
```
Note the `Sid` index in the DataFrame: Sid is short for "security ID" and is the unique identifier for a particular security or contract. Sids are used throughout QuantRocket to refer to securities.
If the `usstock-free-1d` database is the first historical database you've collected, your securities DataFrame will contain 9 rows of sample tickers: AAPL, HD, JNJ, MSFT, SPY, MON, KKD, XOM, and AA. However, if you previously collected the learning bundle, then querying the securities master database may have returned many thousands of rows. If that is the case, you can execute the following cell to load only the 9 sample symbols, which will keep you aligned with this tutorial. Instead of querying everything in the securities master database, the following code first uses `list_sids(...)` to obtain the list of sids that is present in the `usstock-free-1d` history database that we collected in the previous tutorial, then uses `get_securities(...)` to retrieve only those securities from the securities master database.
```python
from quantrocket.history import list_sids
free_sids = list_sids("usstock-free-1d")
securities = get_securities(sids=free_sids)
print(f"loaded {len(securities)} securities")
securities.head()
```
## Filter securities
Our strategy only targets stocks, so before creating our universe, we will filter the securities DataFrame to exclude ETFs (in our case, SPY is the only ETF in our sample data):
```python
securities = securities[securities.Etf==False]
print(f"filtered to {len(securities)} securities")
securities.head()
```
## Create universe
To create a universe consisting of these securities, we simply upload the list of sids to the `create_universe` function. We'll name the universe "usstock-free":
```python
from quantrocket.master import create_universe
create_universe("usstock-free", sids=securities.index.tolist())
```
The function output confirms the name and size of our new universe.
***
## *Next Up*
Part 3: [Momentum Factor Research](Part3-Momentum-Factor-Research.ipynb)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.