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 download a CSV of securities from the securities master database, optionally pare down the CSV to a subset of securities, then upload the pared down CSV 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. First we download the listings from the securities master database to a CSV file:
from quantrocket.master import download_master_file
download_master_file("securities.csv", vendors="usstock")
In QuantRocket terminology, the word "collect" refers to retrieving data from a data provider and saving it to your QuantRocket databases. The word "download" refers to retrieving data out of your QuantRocket databases into a file for use by you or your algorithms.
We can load the CSV into pandas:
import pandas as pd
securities = pd.read_csv("securities.csv")
securities.head()
Note the Sid
column in the CSV file: 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 upload the CSV and give the universe a name. (Only the Sid column in the CSV matters for this purpose; other columns are ignored.) We'll name the universe "usstock-free":
from quantrocket.master import create_universe
create_universe("usstock-free", infilepath_or_buffer="securities.csv")
The function output confirms the name and size of our new universe.
As a demonstration, we could also pare down the master file 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[["Sid","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()
To create a universe from the filtered securities, we can simply write the DataFrame to a CSV and upload the CSV.
filtered_securities.to_csv("filtered_securities.csv")
create_universe("usstock-free-active", infilepath_or_buffer="filtered_securities.csv")