Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Fundamental Moving Average Long/Short Attempt

Several of the fundamental moving average algorithm tests showed positive alpha, but the returns were mostly driven by beta. So I decided to create a long/short algorithm to reduce the beta, but it's not working correctly. The algorithm is buying and selling the same stocks right now, and the screen is only returning a few stocks. Not sure how to fix it so posting what I have right now.

8 responses

Need someone to combine .rank with .top in pipeline and meanwhile maybe a thing or two here to adopt.

Thank you, that looks a lot more complicated than I thought it would be. Also looks like the code I was using was optimized significantly, I did not know how to do that part myself. I was using the gross margin metric to test whether the algorithm worked, so using a custom metric like work-in-process turnover might perform even better.

I was able to modify the algorithm to use work-in-process turnover instead of gross margin, so custom metrics are possible although this test had poor results.

Incidentally, minus signs in front of these two lines reverse long short and flip the curve upside-down.

            'mrank' : -momentum.rank(),  
            'm'     : -momentum,  

Try https://www.quantopian.com/posts/track-orders with start in the options set to '2017-05-03' and see what's up with the big down[turn] above.

I looked at the stock orders in the backtester and it looks like Horizon Pharma (HZNP) had a bad quarter and its stock fell from around $15 to $10. Might need more than 20 longs/shorts to balance out something like that. Also, the best-performing moving average algorithm I have so far is here: https://www.quantopian.com/posts/fundamental-wma-crossover-with-2-metrics With gross margin and assets turnover as the fundamentals it looks like it would have about 0.18 alpha and zero beta if the long and short versions were combined, although these two metrics might not return enough stocks. Picking similar metrics, such as net margin and gross margin, would return more stocks but might not be as good at producing alpha.

I was also testing a moving average strategy that used momentum for three moving averages at the same time. The algorithm crashed with too many inputs when I attempted to add a fourth one. There was a strategy on Alpha Architect called Dual Momentum that used seven moving averages and a price average that was from an academic paper, just tried to visit the site again but it wouldn't load because it contained errors. They didn't provide the code but suggested that someone at Quantopian might be able to do it.

Here's the crossover version. That's probably not the best way to code it but it seems like the algo works.

Longer moving average periods might make this work a little better. The moving average period can be extended to 5 years, but then the algorithm uses too much memory on a 2010-2017 backtest. Think that's because of the way I'm pulling up statistics for past quarters - wish it was possible to do something like gross_margin.quarters_ago[20] but the other fundamental metric thread explained why that's difficult to code. Also fixed a calculation error that was in the first crossover algorithm.