Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Weird behaviour - AAPL undefined

I'm working on some helper algorithms - which may or may not prove to be useful - but I'm having a very difficult time in getting the code to execute properly.

    position = context.portfolio.positions  
    for s in position:  
        print s.symbol  
        print s  
        curr = context.portfolio.positions[s].amount  
        known = 0  
        known = context.securities[s.symbol]['known_position']  
        orders = get_open_orders(s)  

Now in that code sample everything is correctly defined elsewhere. The issue is that context.securities only gets defined (elsewhere) when I make an actual trade in a stock. In my sample I just use XIV. The problem is when I run the code it gives me an error saying AAPL is undefined - which it is. However, if I comment out the line that is causing the problem known = context.securities[s.symbol]['known_position'] then AAPL never shows up as a potential symbol.

Blockquote
2015-09-02PRINTXIV
2015-09-02PRINTEquity(40516 [XIV])
2015-09-03PRINTXIV
2015-09-03PRINTEquity(40516 [XIV])
2015-09-04PRINTXIV
2015-09-04PRINTEquity(40516 [XIV])
2015-09-08PRINTXIV
2015-09-08PRINTEquity(40516 [XIV])
2015-09-09PRINTXIV
2015-09-09PRINTEquity(40516 [XIV])
Blockquote

So what am I doing wrong?

3 responses

I have included the algorithm. It doesn't really do anything at this point - I'm just trying to establish some global variables basically inside of context. I know I can work around it by defining all sec in data, but that seems very inefficient. I'm really just more curious about why the error occurs like it does.

To get the error you need to uncomment the line mentioned in the previous post.

Hi Nick,

After reviewing your code, I think I managed to find the problem. The context.portfolio.positions variable actually stores all stocks in your universe including those for which you own 0 shares. In your code, you are looping through all of the stocks in your context.portfolio.positions and then accessing the elements of context.securities with all of these stocks (even the 0-share ones).

I can understand the confusion as our documentation states that context.portfolio.positions only stores open positions which is incorrect. I have noted the discrepancy and we will update the documentation accordingly. Thanks for bringing it to our attention!

Let me know if you have any further questions.

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.

Upon further review and after seeing a similar problem with another user, it seems that the error is not in the documentation, but with the context.portfolio.positions dictionary. The error is related to empty entries being incorrectly added to the dictionary and I have notified our engineers of the problem. Sorry for the earlier confusion and thanks again for bringing this to our attention!