Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to iterate over positions and sell some of them?

The problem is that when iterating over the portfolio, I get position objects back, instead of equity objects, and I can't sell position objects!

#iterate over all currently held positions  
for sec in context.portfolio.positions:  
       #check each security to see if we want to keep it  
        if sec not in context.secs_to_keep:  
             #if we don't want to keep it, sell it  
            order_target_percent(sec,0)  
1 response

Position is a dictionary of all the open positions, keyed by security ID. You can use below code to get the ID of the security.

for psid, pos in context.portfolio.positions.iteritems():  
order_target(psid, 0)