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.

Download master file

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:

In [1]:
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:

In [2]:
import pandas as pd
securities = pd.read_csv("securities.csv")
securities.head()
Out[2]:
SidSymbolExchangeCountryCurrencySecTypeEtfTimezoneNamePriceMagnifierMultiplierDelistedDateDelistedLastTradeDateRolloverDate
0FIBBG000B9XRY4AAPLXNASUSUSDSTK0America/New_YorkAPPLE INC110NaNNaNNaN
1FIBBG000BFWKC0MONXNYSUSUSDSTK0America/New_YorkMONSANTO CO1112018-06-06NaNNaN
2FIBBG000BKZB36HDXNYSUSUSDSTK0America/New_YorkHOME DEPOT INC110NaNNaNNaN
3FIBBG000BMHYD1JNJXNYSUSUSDSTK0America/New_YorkJOHNSON & JOHNSON110NaNNaNNaN
4FIBBG000BPH459MSFTXNASUSUSDSTK0America/New_YorkMICROSOFT CORP110NaNNaNNaN

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.

Create universe

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":

In [3]:
from quantrocket.master import create_universe
create_universe("usstock-free", infilepath_or_buffer="securities.csv")
Out[3]:
{'code': 'usstock-free', 'provided': 8, 'inserted': 8, 'total_after_insert': 8}

The function output confirms the name and size of our new universe.

Filter master file

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:

In [ ]:
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)

QGrid widget

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:

In [5]:
filtered_securities = widget.get_changed_df()
filtered_securities.head()
Out[5]:
SidSymbolExchangeNameDelisted
0FIBBG000B9XRY4AAPLXNASAPPLE INC0
2FIBBG000BKZB36HDXNYSHOME DEPOT INC0
3FIBBG000BMHYD1JNJXNYSJOHNSON & JOHNSON0
4FIBBG000BPH459MSFTXNASMICROSOFT CORP0
6FIBBG000GZQ728XOMXNYSEXXON MOBIL CORP0

To create a universe from the filtered securities, we can simply write the DataFrame to a CSV and upload the CSV.

In [6]:
filtered_securities.to_csv("filtered_securities.csv")
In [7]:
create_universe("usstock-free-active", infilepath_or_buffer="filtered_securities.csv")
Out[7]:
{'code': 'usstock-free-active',
 'provided': 6,
 'inserted': 6,
 'total_after_insert': 6}