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()
Symbol | Exchange | Country | Currency | SecType | Etf | Timezone | Name | PriceMagnifier | Multiplier | Delisted | DateDelisted | LastTradeDate | RolloverDate | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Sid | ||||||||||||||
FIBBG000B9XRY4 | AAPL | XNAS | US | USD | STK | False | America/New_York | APPLE INC | 1 | 1 | False | NaT | NaT | NaT |
FIBBG000BFWKC0 | MON | XNYS | US | USD | STK | False | America/New_York | MONSANTO CO | 1 | 1 | True | 2018-06-06 | NaT | NaT |
FIBBG000BKZB36 | HD | XNYS | US | USD | STK | False | America/New_York | HOME DEPOT INC | 1 | 1 | False | NaT | NaT | NaT |
FIBBG000BMHYD1 | JNJ | XNYS | US | USD | STK | False | America/New_York | JOHNSON & JOHNSON | 1 | 1 | False | NaT | NaT | NaT |
FIBBG000BPH459 | MSFT | XNAS | US | USD | STK | False | America/New_York | MICROSOFT CORP | 1 | 1 | False | NaT | NaT | NaT |
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())
{'code': 'usstock-free', 'provided': 8, 'inserted': 8, 'total_after_insert': 8}
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. For example, we might limit the universe to currently listed securities:
filtered_securities = securities[securities.Delisted==False]
# also limit the number of columns for readability
filtered_securities = filtered_securities[["Symbol", "Exchange", "Name", "Delisted"]]
filtered_securities.head()
Symbol | Exchange | Name | Delisted | |
---|---|---|---|---|
Sid | ||||
FIBBG000B9XRY4 | AAPL | XNAS | APPLE INC | False |
FIBBG000BKZB36 | HD | XNYS | HOME DEPOT INC | False |
FIBBG000BMHYD1 | JNJ | XNYS | JOHNSON & JOHNSON | False |
FIBBG000BPH459 | MSFT | XNAS | MICROSOFT CORP | False |
FIBBG000GZQ728 | XOM | XNYS | EXXON MOBIL CORP | False |
Then we can create a universe from the filtered securities.
create_universe("usstock-free-active", sids=filtered_securities.index.tolist())
{'code': 'usstock-free-active', 'provided': 6, 'inserted': 6, 'total_after_insert': 6}