Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Api question: what is universe

I am very confused with universe in Help doc. In Help docs I find there are 3 methods can change universe:

set_universe(sids)

update_universe(sids)

fetch_csv(url, pre_func=None, post_func=None, universe_func, date_column='date',  
           date_format='%m/%d/%y', timezone='UTC', symbol=None, mask=True, **kwargs)  

(1) set_universe, 200 limit, in initialize() method.

(2) update_universe, it seems no limit, only if we update in different time. In before_trading_start() method.

(3) fetch_csv, 200 limit, in initialize() method.

So my guess is:

(1) if we only use set_universe or fetch_csv, then universe have a cap of 200.

(2) if we use update_universe, then universe size will grow, it records all the securities we have updated. Then in handle_data(context, data), we can see all the securities we have updated, even they are not in our position or order now.

Is my understanding right?

6 responses

Hi Jian,

"Universe" is the entire collection of securities that your algorithm can trade. There are 4 ways to create your stock universe:

  1. Manually using the sid(), symbol(), or symbols() methods. The SID is a unique identifier on Quantopian for each stock. Symbol() lets you look up a single ticker to trade. Symbols() lets you pass a list of tickers to trade.

  2. set_universe() is a random basket of stocks, based on their traded dollar volume. This reduces the bias introduced by manually hand-picking stocks. This universe of stocks gets updated automatically each quarter, as the dollar volume changes.

  3. Fetcher allows you to import data in a CSV to your algorithm. You can import stocks to create your universe, or you can import other data, as a signal to trade upon.

  4. Fundamental data can be used in before_trading_start(). You can query an unlimited number of stocks and filter down based on some criteria (market cap, P/E ratio, etc). You then limit the query to return a maximum of 200 tradeable stocks. You can then use the function update_universe() to update your universe of stocks each day to your criteria is always met.

In all 4 of these cases, you can trade up to 200 securities in your algorithm. So you can either manually enter 200 tickers, set a 2% universe (roughly 160 stocks), have up to 200 stocks per day in your CSV, or use fundamentals to filter down 200 stocks based on value metrics.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thank you for this detailed answer Alisa!

I can't put my fingers on the link where I showed it, but in practice, using update_universe(), more than 200 stocks can be traded, but only 200 new securities can be added per trading day. --Grant

https://www.quantopian.com/posts/how-to-create-a-symbol-from-string#54f5f2248be13134f2000027 demonstrates that (using get_fundamentals). I saw an algo trading over 1400 stocks once. Note too Jian, if you print len(data), you'll probably find that the count is not always what you might expect, because, for a bar where a stock does not trade, it won't be present. Meanwhile a stock can be absent from data yet present in context.portfolio.positions (plus number of shares, amount, can be zero there). One other thing to keep in mind wrt what the data object contains, is that if you use fetcher, it adds string keys to data.

Great discovery, Thank you Grant and garyha!

Just keep in mind that Q doesn't really support trading of large portfolios. Watch out for the 50-second time out of a call to handle_data(), which will crash the algo. Also, there is a finite amount of memory available (there's no way to tell how much has been allocated to the algo and how much has been used). You might consider the Q research platform, if you need more flexibility (although you'll still have to deal with an unknown amount of memory allocated, that could be exceeded).