Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
L/S Mean Reversion for SPDR

What this tries to achieve is to hold long positions when the 5 day avg (short term moving avg) is below the 120 day avg (long-term moving avg) and tries to sell once the short term mean is well above that long-term threshold. My hypothesis is that equity markets would generally be moving in a upward trajectory in the long run overall (esp in eras with low-interest rates), and so the long-term moving avg would be naturally under the short-term mean and moving in an upward trajectory. Therefore, this algorithm aims to capture the inefficiencies caused by drop in prices below the natural long-term upward moving avg. Finally, to capture the swinging motions in the market in the short-term, it will aim to make short positions if the price in the short-term mean is well above the natural upward long-term moving average (or several degrees of standard deviations from the long-term mean vis-a-vis the 30 day moving avg).

This is still in work and is being consistently modified/updated. What this is not able to achieve (yet) is balancing with sudden shocks in the market (due to random events, or black swan moments) that can disrupt the moving averages. But, is a decent starting framework to work within nonetheless. What this does try to take into account is limiting leverage to realistic levels, an attempt at normal slippage, and maintaining accurate trade commissions.