Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
What exactly does update_universe() do?

I guess I don't know what this function actually does, because to me there's no reason why the attached 'algorithm' should ever reach the 'except' block. I'm setting 'context.watch' and then passing it to 'update_universe()'. As I understand it, 'update_universe()' is supposed to populate 'data' with the securities from the iterable passed into it. So, how come when I later loop over 'context.watch', I get random KeyErrors trying to access the information from 'data'?

Is this a bug or am I misinformed as to what 'update_universe()' does? If it's not a bug, what's the point of 'update_universe()' if not every security chosen to be in my universe makes it into 'data'?

If it is a bug, how should we work around it, and when can we expect it to be fixed?

Furthermore, shouldn't the API documentation mention this explicitly if this is the expected behavior? And if it already does, where is it mentioned in the docs?

3 responses

Now that we have Pipeline and are able to access the full universe of securities there is an increased tendency to pick low liquidity stocks. What you are running into, the KeyErrors, are points where we don't have data for that bar for that security. So update_universe is doing its job fine, taking the Pipeline results and adding them to your algorithm's universe. Here is the Common Error Messages section of the help docs, in fact it has the answer to the root of your question. And code to remedy it.

Attached is your algo with a Pipeline filter to screen out low liquidity securities, and some ordering code.

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.

Hello James,

I am confused by this error, too. My understanding is that pipeline should provide a daily point-in-time global universe of equities, to which screens could be applied. Presumably, this means that any equity with an end_date equal to the current trading day would be accepted into the global universe (if a given security had just been de-listed, its end_date would be equal to the prior trading day). And any equity with a start_date equal to the current trading day would be accepted into the global universe, too.

Is it correct that for both backtesting and live trading that when an algorithm is started, an initial trade on the market is required before a Quantopian-originated trade could be made? Or does this problem only occur for equities with a start_date equal to the current trading day?

I'm also confused by your work-around. I thought the recommended approach is described on the help page:

To check if trade data exists for a security during a given trading period, just check if it exists in the data structure that is passed to handle_data. For example, if sid in data: ... Your algorithm will check to see if the security has data in the bar. Otherwise, it will skip the security and continue with your algorithm calculations without receiving an error.

Is it correct that update_universe wipes out the prior universe, creating a new one every day (except that certain equities are retained, e.g. ones with positions or with open orders)? This would say that forward-filling, as described on https://www.quantopian.com/posts/thinly-traded-stocks-why-no-gaps would not carry over from day to day, which would exacerbate the problem of key errors?

By the way, this would appear to be a kind of flaw/shortcoming in the Quantopian platform, since if an equity is open for trading on a market, one should be able to buy/sell it using Quantopian. My sense is that this is a limitation of the platform, but perhaps I'm misinterpreting.

Grant

James,

Thank you! The doc page that you sent me was exactly what I needed, and your explanation cut through the heart of my confusion. I guess I just need to get better at navigating the manual.

Best,
Nick