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!

Loading notebook preview...
Notebook previews are currently unavailable.
12 responses

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

Clone Algorithm
Backtest from to with initial capital
Total Returns
Max Drawdown
Benchmark Returns
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5573bb4c78c54010b6bc8ec1
There was a runtime error.

'...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.

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.


Loading notebook preview...
Notebook previews are currently unavailable.

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?


Thanks for the post

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