Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Pretty sure I have messed something up

Either I have a bug or I'm the greatest trader ever

8 responses

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?

Results are much more reasonable now. Thanks Gary!

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?

Obviously the best trading strategy is constantly double down instead of getting out

See Martingale betting strategy.

Misplaced leading negative (-) sign now fixed. No more doubling down each time on Apple and no margin in the account. My returns look pretty stellar now.