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, 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
securities = get_securities(vendors="usstock")
print(f"loaded {len(securities)} securities")
securities.head()
loaded 9 securities
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 |
FIBBG000BDTBL9 | SPY | ARCX | US | USD | STK | True | America/New_York | SPDR S&P 500 ETF TRUST | 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 |
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.
If the usstock-free-1d
database is the first historical database you've collected, your securities DataFrame will contain 9 rows of sample tickers: AAPL, HD, JNJ, MSFT, SPY, MON, KKD, XOM, and AA. However, if you previously collected the learning bundle, then querying the securities master database may have returned many thousands of rows. If that is the case, you can execute the following cell to load only the 9 sample symbols, which will keep you aligned with this tutorial. Instead of querying everything in the securities master database, the following code first uses list_sids(...)
to obtain the list of sids that is present in the usstock-free-1d
history database that we collected in the previous tutorial, then uses get_securities(...)
to retrieve only those securities from the securities master database.
from quantrocket.history import list_sids
free_sids = list_sids("usstock-free-1d")
securities = get_securities(sids=free_sids)
print(f"loaded {len(securities)} securities")
securities.head()
loaded 9 securities
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 |
FIBBG000BDTBL9 | SPY | ARCX | US | USD | STK | True | America/New_York | SPDR S&P 500 ETF TRUST | 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 |
Our strategy only targets stocks, so before creating our universe, we will filter the securities DataFrame to exclude ETFs (in our case, SPY is the only ETF in our sample data):
securities = securities[securities.Etf==False]
print(f"filtered to {len(securities)} securities")
securities.head()
filtered to 8 securities
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 |
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.