Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
OrderSecurityOutsideUniverse Error with Fundamental Trading

I was playing with an algorithm that Naoki Nagai posted yesterday and I am getting an error that I can't figure out. For some reason I keep running into OrderSecurityOutsideUniverse when the algorithm attempts to sell YRCW, I believe this is due to the fact that it does not pass the fundamental screening criteria laid out. However, from a post last year it looks like all securities in the portfolio should remain in the universe (see below). What could be going on here?

https://www.quantopian.com/posts/how-to-deal-with-an-ordersecurityoutsideuniverse-error

7 responses

Hey Ryan,

I just tried running your algorithm but can't seem to reproduce the error. Are you still having problems with this?

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.

I am. Here is the error message I get.

Something went wrong on our end. Sorry for the inconvenience. Please email us so we can help.
OrderSecurityOutsideUniverse: 0036 You have placed an order with an unexpected security: Security(8370 [YRCW]). Please declare all securities in the initialize function by calling sid(123) for each security that will be used later in your algorithm.
There was a runtime error on line 30.

Hey Seong,

I'm getting the same error in an almost identical situation (picking stocks based on fundamentals and then closing out portfolio each month). It's not consistent in that it doesn't happen with every security the algorithm tries to exit out of.

Any help would be much appreciated!

Ryan, since you were able to attach a backtest to your post, the backtest was able to run to completion successfully. Can you paste the exact code that triggered the OrderSecurityOutsideUniverse error?

To paste code you can you 3 backward ticks ``` before and after the code snippet. Joshua, if you have an example of code that shows this error, I'd also like to help. I'd like to reproduce the error and see what's going on!

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.

Try 'for stock in data' instead of 'for stock in context.portfolio.positions'

Gary, Thanks for that, it does seem to at least allow the backtest to run to completion. Any idea as to why 'for stock in context.portfolio.positions' was generating an error?

Ah I see now.
When a stock is owned it is automatically included in data.
The gotcha is that there can be stocks in context.portfolio.positions with amount 0 and those are not automatically included in data.
So lines two and three here ought to move you forward.

    for stock in context.portfolio.positions:  
        if not context.portfolio.positions[stock].amount:  
            continue   # if 0 shares, skip it  

Q, it would be more efficient if we need not do that check. Unless you are mirroring IB.

On the problem not being reproducible, being human, you unfortunately posted a different version of the backtest than the one intended, as the one above is identical to the original that you referred to, it is here.

Bumping up num_stocks from 100 to 200 will hit that error, just with a different security although close to the same date. If you didn't change that number, then it could be interesting to know what else might bring OrderSecurityOutsideUniverse to the surface.