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

In [1]:
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()
Out[1]:
SymbolExchangeCountryCurrencySecTypeEtfTimezoneNamePriceMagnifierMultiplierDelistedDateDelistedLastTradeDateRolloverDate
Sid
FIBBG000B9XRY4AAPLXNASUSUSDSTKFalseAmerica/New_YorkAPPLE INC11FalseNaTNaTNaT
FIBBG000BFWKC0MONXNYSUSUSDSTKFalseAmerica/New_YorkMONSANTO CO11True2018-06-06NaTNaT
FIBBG000BKZB36HDXNYSUSUSDSTKFalseAmerica/New_YorkHOME DEPOT INC11FalseNaTNaTNaT
FIBBG000BMHYD1JNJXNYSUSUSDSTKFalseAmerica/New_YorkJOHNSON & JOHNSON11FalseNaTNaTNaT
FIBBG000BPH459MSFTXNASUSUSDSTKFalseAmerica/New_YorkMICROSOFT CORP11FalseNaTNaTNaT

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.

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

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

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

Filter securities

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:

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

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 [4]:
filtered_securities = widget.get_changed_df()
filtered_securities.head()
Out[4]:
SymbolExchangeNameDelisted
Sid
FIBBG000B9XRY4AAPLXNASAPPLE INCFalse
FIBBG000BKZB36HDXNYSHOME DEPOT INCFalse
FIBBG000BMHYD1JNJXNYSJOHNSON & JOHNSONFalse
FIBBG000BPH459MSFTXNASMICROSOFT CORPFalse
FIBBG000GZQ728XOMXNYSEXXON MOBIL CORPFalse

Then we can create a universe from the filtered securities.

In [5]:
create_universe("usstock-free-active", sids=filtered_securities.index.tolist())
Out[5]:
{'code': 'usstock-free-active',
 'provided': 6,
 'inserted': 6,
 'total_after_insert': 6}