Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Number of Shares Held...

Hi,
I know this must seem like a simple/foolish question, but what is the easiest way to tell if there are any stocks currently being held, regardless of what stocks they are? I am trying to make an algorithm that will only buy if there are no other stocks currently held.

Thanks!

2 responses

I'd check 'context.portfolio.positions'. It's a dictionary and will return False if it's empty otherwise it returns True. Something like this:

 if context.portfolio.positions:  
        # There are some holdings  
        pass

    else:  
       # There are no current holdings  
       pass

Thanks, that exactly what I was looking for!