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?
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?
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