• Free Book
  • Store
    • Books
    • Free First Chapters
    • Free Newsletters
  • Recent Articles

TradingMarkets.com

Quantified Stock Market Trading Strategies & Systems

  • Home
  • New Trading Research
  • Education
    • Articles
      • Connors Research
      • ETFs
      • Options
      • Stocks
      • Volatility
    • Trading Lessons
    • Connors Research
    • Glossary
    • Interview Archive
    • Videos
  • Python
  • Quantamentals
    • Quantamentals: The Next Great Forefront of Trading and Investing
    • Quantamentals Resources
  • Courses
  • Store
    • New Book! The Alpha Formula
    • “Buy The Fear, Sell The Greed” – Best Seller!
    • Swing Trading College 2019
    • Trading Books and Guidebooks
    • Street Smarts
    • Online Trading Courses
    • Private Mentoring with Larry Connors
    • Customized Trading Research
    • Amibroker Strategy Add On Modules
You are here: Home / Connors Research Traders Journal / Connors Research Traders Journal (Volume 38): Here’s How We Built a Superior Strategy in Python in Under 30 Minutes (And You Can Too)

Connors Research Traders Journal (Volume 38): Here’s How We Built a Superior Strategy in Python in Under 30 Minutes (And You Can Too)

March 4, 2019 by Larry Connors

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

 

 

 

Analysis

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:

  1. We ideally don’t want to be invested in stocks in long-term bear markets.
  2. 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.
  3. 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!
  4. 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.

  1. When SPY is below its simple moving average we’ll be in bonds. Let the bear market play itself out.
  2. 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.

That’s it!

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.

If you would like to listen now to the recording of the same webinar we did on Thursday, click here to listen now.

 

Sincerely,
Larry

Larry Connors
Connors Research, LLC.

P.S.
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!

Filed Under: Connors Research Traders Journal, Recent Tagged With: python

About Larry Connors

Larry Connors has over 30 years in the financial markets industry. His opinions have been featured at the Wall Street Journal, Bloomberg, Dow Jones, & many others. For over 15 years, Larry Connors and now Connors Research has provided the highest-quality, data-driven research on trading for individual investors, hedge funds, proprietary trading firms, and bank trading desks around the world.

Larry has been published extensively, with titles like How Markets Really Work, Short Term Trading Strategies That Work, High Probability ETF Trading, and The Connors Research Trading Strategy Series including our latest Guidebook High Probability Trading with Multiple Up & Down Days.

Buy The Fear, Sell The Greed

Buy The Fear, Sell The Greed

Swing Trading College

New Book From Larry Connors and Chris Cain, CMT – "The Alpha Formula; High Powered Strategies to Beat The Market With Less Risk"

We’re excited to announce the release of a new investment book written by Larry Connors and Chris Cain, CMT. The book, “The Alpha Formula; High Powered Strategies to Beat The Market With Less Risk “ combines… Hedge fund legend Ray Dalio’s brilliant insight into combining uncorrelated strategies… With new, minimally correlated, quantified, systematic strategies to trade… [Read More]

Buy The Alpha Formula Now

Connors Research Traders Journal (Volume 57): 7 Real-World Reasons Why Short Strategies Should Be Included In Your Portfolio

In our new book, The Alpha Formula – High Powered Strategies to Beat the Market with Less Risk, we show the benefits of including short-strategies in your portfolio. As a reminder, building portfolios should be based on First Principles – otherwise known as truths. These truths are: Markets Go Up Market Go Down Markets Go… [Read More]

Company Info

The Connors Group, Inc.
185 Hudson St., Suite 2500
Jersey City, NJ 07311
www.cg3.com

About Us

About
Careers
Contact Us
Link To Us

Company Resources

Help
Privacy Policy
Return Policy
Terms & Conditions

Properties

TradingMarkets
Connors Research

Connect with TradingMarkets

Contact

info@cg3.com
973-494-7311 ext. 628

Free Book

Short Term Trading Strategies That Work

© Copyright 2020 The Connors Group, Inc.

Copyright © 2023 · News Pro Theme on Genesis Framework · WordPress · Log in