Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Finding the best moving averages (short, long) for cross-over trading strategy

This example shows the results of a simple moving average cross-over strategy, and it attempts to identify the best moving averages to maximize the sharpe value.

Based on these backtests, here are most optimized parameters for this strategy, when trading AAPL during the specified time-frame:

Short moving average = 25 days
Long moving average = 70 days

This is a really powerful tool, that I can't wait to optimize my other algorithms with!

13 responses

Here is a normal Quantopian algorithm that matches the one in Research.

'...and with five I can make him wiggle his trunk."

Great work! Thanks for sharing,

finally a notebook I understand..tx

@Peter: Thanks! I am glad to hear that, helping others was my goal for sharing. BTW, this is probably the second notebook I understand. :)

@Market Tech: Thanks for the warning about over-fitting. I decided to make a new notebook to help visualize this concept.

https://www.quantopian.com/posts/finding-the-best-moving-averages-now-with-2012-testing-period

if your notes suggest... the best moving average for aapl why doesnt it outperform.. the benchmark... just askng...

Its optimizing the sharpe ratio, not the actual return. Sharpe ratio is the mean (excess) return (so the formula used above is incorrect) divided by the volatility, so therefore it is more sensitive to the volatility than it is to the return. So, e.g., if you reduce the return by 10% but halve volatility, you will almost double the sharp-ratio. Thus you can under-perform the market but have an optimized Sharpe ratio by substantially reducing your volatility.

Can you reshare the notebook? It doesn't let me clone it since you shared it before cloning was added. Thanks.

Hey Steven,

I agree, would have been nice to be able to just click once and clone it.. however, if you really wanted to run it, you might as well copy and paste each cell separately and it will work just fine as well, which is all I did...
Mind you that the 'add_history' line needs to be removed though and the 'history' command is deprecated and should be upgraded to 'data.history' (but I have not managed to get that to work in the same notebook for some reason, I suspect there is a difference in the data type returned...

The working and updated notebook in attachment.

Jens

Thanks for the great work.

But when I copied exactly the same code of Jens's on the notebook and run, it shows the following error:
'the label [2014-01-02 00:00:00+00:00] is not in the [index]'

I have been searching for solutions but haven't got an easy one to fix it. Any help?

Hi,

Thanks for the post

I have the same issue:
'the label [2014-01-02 00:00:00+00:00] is not in the [index]'

Thanks

David

A newbie question, for day trade for a given no of stocks like Trading Universe, I want to look for the
1. Optimial time frame between 1min-1hr bars
2. Optimal long and short EMA / MACD
3. Optimal Stoploss and Profit taking levels

Can someone advise on how to go about this..??