Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Can't get security object from position

I'm trying to close out a position on a security if it's in my positions but not in a separate list of securities I'm maintaining. However, I can only get the sid from the positions, and I can't get the sid from the list of securities. I tried using the sid() function, like everyone else it looks like, only to find out that the parameter must be a literal. Is there a way to get the actual security from the list of positions? That would be the easiest way. I could probably make do with being able to get the sid of a given security, though that feels like the long way around in this case.

2 responses

You should be able to do something like:


for stock in data:  
    print stock.sid  
    print stock.symbol  
    print '-----------------'  

This should get you the numbers and their corresponding symbols.

Yeah, I figured that out. The issue here was I was unaware that security objects had .sid functions. There's documentation for equity objects and mentions of security objects, but no documentation linking equity objects and security objects being one thing. So, I can loop through...

for stock in data:  
    if any(stock.sid == pos.sid for pos in posList):  
        ...