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.
To create our first universe, we will query securities from the securities master database, optionally 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.
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:
from quantrocket.master import get_securities
# by specifying sec_types='STK', we exclude ETFs, which are present in the sample data
securities = get_securities(vendors="usstock", sec_types="STK")
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.
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":
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.
As a demonstration, we could also pare down the securities to create a universe from a subset of securities. One way to do this is using qgrid
, a tool that provides Excel-like filtering and sorting of DataFrames inside Jupyter notebooks. We limit the number of columns to make the grid more readable:
import qgrid
widget = qgrid.show_grid(securities[["Symbol","Exchange","Name","Delisted"]])
widget
(this is an image of a grid, execute the above cell to see the actual grid)
Use the grid to filter the DataFrame by one or more columns. For example, you could exclude delisted stocks. After filtering, use get_changed_df()
to access the filtered DataFrame:
filtered_securities = widget.get_changed_df()
filtered_securities.head()
Then we can create a universe from the filtered securities.
create_universe("usstock-free-active", sids=filtered_securities.index.tolist())