Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
I'm trying to keep the cash positive and not be short any individual stocks, but to keep cash positive it ends up shorting one or multiple stocks.

How do I get it to only sell off stocks that I already own shares in and not end up shorting a stock to keep the cash above 0?

1 response

I am not sure what you are trying to do here.

As written the algo will sell only if cash < 0 line 22
And you are running second for loop at line 23 which
makes the algo sell stock 2 times.
Also amount is asigned at line 16 outside this second loop

Deleting line 23 makes the algo not sell things you don't have.

However since you are buying 1000 shares of different prices you are creating
bigger sell order for the shares having lower price and hence MSFT always gets
sold more than you have (but only after it is bought).
Hence added check qty before selling