What will we cover in this tutorial?
Believe it or not? The there are so many promises of the best trading strategies out there.
In this one we will analyze a promising strategy and see whether it holds or not?
What is it?

This looks amazing, how can it perform this good? Well, simply said, it combines short selling and a safety margin to avoid loosing money.
On the surface, it seems to take precautions. But let’s try to examine it closer.
Step 1: Implementing the strategy
The strategy is to have to Moving Averages a 42 days (MA42) and 252 days (MA252), then a safety margin (or safety distance) involved.
The strategy is as follows:
- When MA42 – MA252 > safety distance, then go long.
- When MA252 – MA52 > safety distance, then go short.
To analyze and implement the Strategy, we need Pandas_datareader to read the historic ticker prices.
import pandas_datareader as pdr
from datetime import datetime
data = pdr.get_data_yahoo('^GSPC', datetime(2000, 1, 1), datetime(2020, 1, 1))
data['MA42'] = data['Adj Close'].rolling(42).mean()
data['MA252'] = data['Adj Close'].rolling(252).mean()
data['42-252'] = data['MA42'] - data['MA252']
sd = 50
data['Regime'] = np.where(data['42-252'] > sd, 1, 0)
data['Regime'] = np.where(data['42-252'] < -sd, -1, data['Regime'])
print(data['Regime'].value_counts())
We retrive data for S&P 500 (ticker: ^GSPC) by using pdr.get_data_yahoo(‘^GSPC’, datetime(2000, 1, 1), datetime(2020, 1, 1)) and get 20 years of data.
As you see, the moving average are calculate with data[‘Adj Close’].rolling(42).mean().
We use a safety distance (sd) is set to 50. This makes sure that we do not go long or short immediately, but only when we have some distance.
The Regime is the short and long signal. Short when -1 and long when 1. If 0, do nothing.
We can see an overview of how often we go long and short.
1 2608
0 1422
-1 1001
Step 2: Test the strategy
This is straight forward.
import pandas_datareader as pdr
import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt
data = pdr.get_data_yahoo('^GSPC', datetime(2000, 1, 1), datetime(2020, 1, 1))
data['MA42'] = data['Adj Close'].rolling(42).mean()
data['MA252'] = data['Adj Close'].rolling(252).mean()
data['42-252'] = data['MA42'] - data['MA252']
sd = 50
data['Regime'] = np.where(data['42-252'] > sd, 1, 0)
data['Regime'] = np.where(data['42-252'] < -sd, -1, data['Regime'])
print(data['Regime'].value_counts())
data['Market'] = np.log(data['Adj Close'] / data['Adj Close'].shift(1))
data['Strategy'] = data['Regime'].shift(1) * data['Market']
data[['Market', 'Strategy']].cumsum().apply(np.exp).plot(grid=True, figsize=(8, 5))
plt.show()
To test the strategy you need to compare it. We compare it against the general market (The S&P 500).
We use the log-returns, which can later be accumulated by cumsum and applied with the exponential function to get the result again.
The Strategy uses the Regime, but shifted one day, as we need a day to react on the price, hence, data[‘Regime’].shift(1) * data[‘Market’].
Finally, we plot the Strategy against the Market.

That looks good, right?
Well, let’s look at it closer.
Step 3: Deeper analysis of the winning strategy
First a few notes on short selling in general.
As noted, the strategy utilizes short selling, which often is not advised for starting investors. First of all, it is often more expensive, as you loan.
Often there is a fee to short sell, and a interest on top of that. I have seen fees that are quite high and interest on 4% p.a. This makes it less attractive and needs to be considered.
But there is more, and this is why I always encourage people to make the analysis they see themselves. The above depends on the starting point.
Let’s take a different period: 2010-2020, then this picture arises.

Really, you might ask?
Try it yourself. Often these extremely good strategies, looking too good to be true, are only good under certain circumstances. Like, here, where it depends on the starting point.
Okay, in the past, when you start in 2000, it would have been good. But not from 2010 and forward, then it looks like a loosing strategy.
What about if you start today?
If you ask me, this is speculating.
12% Investment Solution
Would you like to get 12% in return of your investments?
D. A. Carter promises and shows how his simple investment strategy will deliver that in the book The 12% Solution. The book shows how to test this statement by using backtesting.
Did Carter find a strategy that will consistently beat the market?
Actually, it is not that hard to use Python to validate his calculations. But we can do better than that. If you want to work smarter than traditional investors then continue to read here.
Python for Finance: Unlock Financial Freedom and Build Your Dream Life
Discover the key to financial freedom and secure your dream life with Python for Finance!
Say goodbye to financial anxiety and embrace a future filled with confidence and success. If you’re tired of struggling to pay bills and longing for a life of leisure, it’s time to take action.
Imagine breaking free from that dead-end job and opening doors to endless opportunities. With Python for Finance, you can acquire the invaluable skill of financial analysis that will revolutionize your life.
Make informed investment decisions, unlock the secrets of business financial performance, and maximize your money like never before. Gain the knowledge sought after by companies worldwide and become an indispensable asset in today’s competitive market.
Don’t let your dreams slip away. Master Python for Finance and pave your way to a profitable and fulfilling career. Start building the future you deserve today!
Python for Finance a 21 hours course that teaches investing with Python.
Learn pandas, NumPy, Matplotlib for Financial Analysis & learn how to Automate Value Investing.
“Excellent course for anyone trying to learn coding and investing.” – Lorenzo B.
