Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Retrieving Data History on Current Positions

Goal: Retrieve price data on securities I am currently holding.

My struggle: Now that we need to explicitly call out the securities to retrieve price data I am having a hard time extracting the info I need from context.portfolio.positions to feed into the data.history() function. Seems like context.portfolio.positions deals in SID while data.history() is looking for the symbol string. Is there a clean way to get a list of security symbols of the positions I am currently holding?

Any help is appreciated!
Thanks

7 responses

See if this does it for you. Trick is to use:

context.portfolio.positions.keys()  

Presumably if the number of shares in ticker XYZ drops to 0, XYZ is dropped from the dictionary. I would confirm this.

Thank you very much for the quick reply! When I use context.portfolio.positions.keys() I get the same error and crazily enough when I copy in your entire line of code with data.history into my algo I still get the exact same error! (TypeError: Expected assets argument to be of type or iterable of type Asset, str). Even though when I use it in your example algo everything works fine.

When I print context.portfolio.positions.keys() it still looks like i get the SID's just fine but for some reason doesnt work when i use it in the Order function or History function.

Thank you for the input. Seems like this problem is specific to my code then and I'm messing up my portfolio object somewhere along the way.

Robert,

Well, feel free to strip out anything you don't want to share, and post your code. Sounds like a mystery.

Grant

Ok just found the problem!

When entering my positions I was using this loop:

for Stock in context.Stock_List.index:  
    if context.portfolio.positions[Stock.sid].amount == 0:

This loop worked on Quantopian V1 however, for Quantopian V2 the Stock.sid somehow seems to be messing up the portfolio.positions object for any time I call it afterwards. Its now working correctly by switching my loop to this:

 for Stock in context.Stock_List.index:  
    if context.portfolio.positions[Stock].amount == 0:

Grant, thank you for your help. You definitely shortened my debugging time here big time.

Robert,

Glad you found the error. That may be a bug in the Quantopian software. I don't see why this code should modify your the positions:

for Stock in context.Stock_List.index:  
    if context.portfolio.positions[Stock.sid].amount == 0  

I'll send an e-mail to Quantopian support asking them to have a look.

Grant

Hi Robert,

To answer your original post, data.history() expects either an Equity object (the type returned by sid()/symbol()), or a list of Equity objects. Similarly, context.portfolio.positions is a dictionary keyed by Equity objects. As well, the context.portfolio.positions object behaves like a defaultdict. If a key doesn't exist in the dict when it's referenced, a default (zero) entry will be made. Combining these two facts, using Stock.sid, which returns the integer value of the security ID will never get you a non-zero amount.

On top of this, there's a bug with context.portfolio.positions. It's supposed to get rid of all zero-amount entries in the dict at the end of each day. This is something that we were recently made aware of and our engineers are looking into it.

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.

It's supposed to get rid of all zero-amount entries in the dict at the end of each day.

It doesn't say that in the help. In fact, it implies that only current open positions should be found ("The position object represents a current open position..."). It would seem to be a lot less confusing if you immediately dropped entries when they drop to zero. I suppose one can always loop over the keys and collect only the ones that are non-zero, but you could do that under the hood.

Also, I don't understand the discussion about being able to modify the keys. Your code shouldn't allow this, right?