The Best Trading Strategy Analyzed with Pandas

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?

The bullet proof strategy taken over 20 years span

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.

The winning strategy does not look winning if you start 2010.

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.

What is next?

Want to learn more?

Check out my popular e-book on backtesting strategies. It will teach how to make better analysis, what to look for, and how to evaluate it. This tutorial only touches a few points.

Leave a Reply