Either I have a bug or I'm the greatest trader ever
Either I have a bug or I'm the greatest trader ever
That's a lot.
Try adding these two lines before order_percent():
if context.portfolio.cash < 200:
return
order_percent(stock, context.order_size)
People who include apple in their code will always show great returns as past performance however, removing this security results in a -702% downturn. It isn't a sustainable algorithm in my opinion and the risk is high. I would suggest moving to an index portfolio.
Yeah I'm not trying to develop an Apple trading strategy, just trying to get something to run right now. A closer look based on the suggestion of Gary and looking at my daily positions and gains, is that I'm buying on margin consistently. His proposal to check my cash before ordering should fix it. Is there a margin rate that is applied if cash is negative?
In your close_buy_order_code, you have
order(stock, -context.portfolio.positions[stock].amount * -1)
Isn't this doubling your position instead of closing? Why did you have * -1 there?
See Martingale betting strategy.