In the previous two issues of the Connors Research Traders Journal, Chris Cain and I made a compelling case why every trader should be doing their trading strategy development and research in Python on Quantopian.
There are many advantages for you to program in Python including it’s faster, simpler, open source, and broadly used by tens of thousands of professional traders around the world including many who trade for the best quantitative trading firms globally. Being open source, you have the advantage of using many of the same tools as the industry leaders do.
If your objective is to move from the closed source retail world like Amibroker and TradeStation and move into the open source, professional trading world, programming in Python is the single best way to get there.
Python In Action
To show you the elegance and simplicity of Python combined with Quantopian (or any other Python-based testing platform), Chris and I created the following trading strategy this past Friday afternoon after the market closed.
We started at approximately 4:30 pm ET and by 5 pm we built a strategy in SPY that outperformed buy and hold over the past 12 years, plus had far lower volatility, while only being long US stocks less than 16% of the time!
Firms spend weeks, months, and years looking to do things like this. We did it in 30 minutes because we had the knowledge, combined with Python language skills, and Quantopian’s database. In my opinion, if you have the same resources, there’s a high probability you can do the same.
Our Objective For This Test: Build A Strategy That Allows One To Achieve Market Returns (Or Greater) While Significantly Lowering The Risk of Buy and Hold
We asked a simple question at 4;30 PM on Friday: could we build a strategy to equal buy and hold without taking on all the risk of buy and hold.
To do this we first needed to see just how much risk has there been in buy and hold since 2007.
In under 5 minutes, starting from scratch, Chris wrote the code and generated the following data from Quantopian for us to analyze.
Time period is 01/01/2007 to present
It’s ugly out there when markets drop!
Quantopian shows this in multiple forms from the 5 max drawdowns, to the high volatility, to the time underwater it takes to recover from losses.
While the S&P 500 has been in a bull market for most of this time, there was the drawdown of nearly 55% during the global financial crisis of 2008/2009. Notice rolling six-month volatility during this time reached a peak of over 50% annualized! Also, notice that it took the market years following the crash to make new highs. This is easily observed in the list of worst drawdowns, while the “recovery date” post 2008 didn’t happen until August 2012.
We Then Asked…
Could we have avoided those drawdowns without curve fitting a strategy, while still participating in the long term market rise?
From years of research, we know four truths:
- We ideally don’t want to be invested in stocks in long-term bear markets.
- We do want to be invested in bull markets when they are short-term oversold. I’ve published multiple books over two decades that statistically proves this fact out.
- We don’t want to be invested in stocks when there are no short-term edges. I’ve published this for nearly two decades and I continue to update this fact – when SPY is overbought there is minimal to no short-term edges in place! In fact, many of the years after the market has significantly risen over the short-term, the short-term returns have been negative over the next days and weeks in spite of a major bull market!
- Bonds (AGG) are a solid place to be when the market is in a long-term bear market, or when the market is short-term overbought.
With this in mind, we added two simple rules to the buy and hold strategy.
- When SPY is below its simple moving average we’ll be in bonds. Let the bear market play itself out.
- If SPY is above its 200-day moving average, buy SPY when it’s 4-period RSI is below 20. Exit SPY when it’s 4-period RSI is above 70 and go long bonds. Re-enter when the 4-period RSI closes under 20.
We will call this the “Superior S&P 500 Mean Reversion Strategy”
Total time to write the two rules in Python on Quantopian? 10 minutes.
Let’s look at the simulated test results versus buy and hold (the run time took under 1 minute)
Analysis – An across the board improvement. Higher returns, lower volatility, much higher Sharpe Ratio, 75% reduction in the drawdown and the Daily value at risk was cut in half.
S&P 500 Buy and Hold (Monthly Returns)
Superior S&P 500 Mean Reversion Strategy – Monthly Returns
Analysis – All years were profitable including 2008 (+3.6% vs. -37%) and 2018 (+.02% vs -4.4%)
SPY Buy and Hold – Worst Drawdowns
Superior S&P 500 Mean Reversion Strategy – Worst Drawdowns
Analysis – Far lower Drawdowns. The largest DD went from -54.6% to -13.9%
SPY Buy and Hold: Rolling 6 Month Volatility
Superior S&P 500 Mean Reversion Strategy: Rolling 6 Month Volatility
Analysis– Far lower volatility. In fact, volatility was cut in half.
SPY Buy and Hold – Underwater Plot
Superior S&P 500 Mean Reversion Strategy – Underwater Plot
Analysis – Much faster recovery time to new equity highs in the portfolio. No one likes being underwater! This strategy significantly lessened the time to making new highs.
The Results Speak For Themselves
Our very simple strategy, constructed and written in Python, and tested on Quantopian produced much better risk-adjusted returns. Total time to do this – under 30 minutes!
Would You Like To Do This Type of Research and Strategy Development For All Your Trading Including Intra-day Trading, Short-term Trading, Momentum Trading, Portfolio Development, and More?
Attend our Free Webinar on Tuesday, March 5 at 1 pm and learn how you can program in Python like the top professional traders in the world. Click here to sign up for our Programming in Python for Traders webinar.
Connors Research, LLC.
My new book Buy the Fear, Sell the Greed – 7 Behavioral Quant Strategies for Traders, is now available. Click here to order your copy today.
Receive The Connors Research Traders Journal Free To Your Inbox
CLICK HERE to sign up to receive new issues of the Connors Research Traders Journal newsletter directly via email up to 3 times a week!