Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help organizing stocks.

My algorithm is only buying the top stock in my context.stock list. I am not sure if I need to make some type of matrix so it knows which stock to buy. I used a similar code to my update_universe code, which worked. It might be an error in the loops.

6 responses

Quantdog,

Try this way

Quantdog,

It may be better with different MA length.

Quantdog, Vladimir: In each of the three algorithms above, the return statement at the end of handle_data is contained within the body of the for loop. Thus, after handling the first stock in context.stocks, the return will end the execution of handle_data, and the loop with it. Remove the return.

Thanks Andre, Does this code look right?

It doesn't perform well. But is it buying the stocks correctly now.

So now it does what you want it to do? Then your work on this strategy is done. It brought you about 10% of the returns you would have made buying and holding SPY.

Now you can start thinking of a different strategy: change the condition for inclusion in the buy list, trading frequency, and/or the assets, eg. individual stocks from each sector. Or find a different strategy altogether.

One way to improve the performance of this strategy would be to invest more money. Look at the leverage plot: you are only fully invested once, and the average leverage over time is about 50%. So, on the average, about half of your money is sitting there and gathering dust. Clearly, if you don't sow, you won't reap.

How can you allocate all the money all the time? It's convenient that you first build a buy list, then buy the assets. So, at the time you're buying, you know how many. How would you change the code to allocate an equal amount to each asset you're buying so as to be fully invested? I'll leave it as an exercise for the reader.