Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
SPY 200MA Backtest w/short

Buy when price > 200MA, liquidate when price < 200MA, short when price < 200MA and price < 10MA

11 responses

Thanks for sharing these Claus. They're interesting. Was there an inspiration for these, or did you just figure them out yourself?

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

what i find really interesting is how gracefully you avoided the 08/09 crash.

I'm just backtesting a couple of trend following strategies from various books, like Clenow and Covel, surprised to see how easily one could've avoided the 08/09 crash. Even without a short component, you'd still be doing great with the 200MA alone.

I switched your MA10 rule round, as equities are mean reverting in the short term. For me, it only makes sense to short them if they're ABOVE the short term (10 day) moving average. This means the short term mean reversion is in the same direction as the bear market trend.

This improves the total return and reduces the max drawdown. It reduces the Sharpe ratio though, which is unexpected! But anyway, I'd rather have a lower DD than a lower Sharpe, all other things being equal!

The risk metrics in the original post were incorrect due to backtester bugs (see the link in the Important Message tab). Your changes actually improve Sharpe from 0.43 (the value obtained by running the original code in today's backtester) to 0.66. Nice work!

Thanks Michael.

I just noticed the algo had a hardcoded short size of $10,000. I've changed this to half the total cash. Obviously it could be 100% of the cash, like the long side, but this is a personal preference of mine. Bear markets are often subject to very sharp reversals.

This change brings the Sharpe down slightly, but it's probably just noise.

Here's the same algo, tidied up a bit. It uses the schedule_function to run 15 minutes before market close, so it has a good estimate of the day's close, and uses order_target_percent, as recommended by Q.

Hi Dan,
That's very cool, I had totally forgotten that I had posted this, it's been a couple of years. I like what you did with the schedule function, I'll have to check out the new API features. Thx!

Quantopian/Python has a steep learning curve, so revisiting code is a good way for me to learn. I made the newbie error using schedule_function then running the algo in daily mode. Here's the fix, with the exposure halved on the short side.

Hey, it looks like you need to isolate the peak for the short buy. Notice your short on 2009's crash only netted you 1% of the potential 30% gain.

Maybe you could write in to close the short when it is above x% off of the y day moving average low to potentially close on long and short peaks?