Simple Moving Average Strategy Using Python

Introduction

Since I began coding, writing my own trading algorithm has been a top priority on my to-do list. I know that it is incredibly hard to make them profitable, as it takes a team of individuals working on and updating the algorithm on a daily basis. As a first-attempt personal project, I do not expect this algorithm to start making me any money. A simple working algorithm is the goal for now.

Simple MA Strategy

Goal:

  • Output: subset list made from input list made up of stock tickers in which the 30-day moving average has crossed the 200-day moving average in the last 10 days

Visualize Moving Average Strategy

I first downloaded TSLA price data from Yahoo Finance ranging from 2010 to 2020. I wanted to visualize the 30–200 moving average strategy. I first plotted the price:

Returning Subset List of Tickers

As you may have noticed, I am using a 30-day moving average instead of the classical 50-day moving average. This was simply done to ensure that I was not left empty handed with my output. Furthermore, no real-world implementation will be made with the code in this blog. Everything done here was more for curiosity’s sake than anything else.

import pandas_datareader as pdr
import datetime
from datetime import date
def ticker_satisfy(list_of_tickers):

# Getting today's date
today = date.today()
# Splitting today's date into day, month, year
end_day = today.day
end_month = today.month
end_year = today.year
# Getting the date 365 days before today
year_ago = date.today() - datetime.timedelta(days=365)
# Splitting year_ago date into day, month, year
start_day = year_ago.day
start_month = year_ago.month
start_year = year_ago.year

buy_list = []

# iterating over list to return satisfying tickers
for ticker in list_of_tickers:

try:
# Grabs daily price data of specified ticker with specified start and end dates
data = pdr.get_data_yahoo(ticker,
start=datetime.datetime(start_year, start_month, start_day),
end=datetime.datetime(end_year, end_month, end_day))
# Creating columns to assess crossover
data['SMA30'] = data['Adj Close'].rolling(window=30).mean()
data['SMA200'] = data['Adj Close'].rolling(window=200).mean()
data['above'] = np.where(data['SMA30'] > data['SMA200'], True, False)

# Dropping null values from dataframe
data = data.dropna()

# Resetting index for simplified slicing
data.reset_index(inplace=True)

# Creating variables for comparison
last_10 = data.iloc[len(data)-10:, :].above.any()
eleventh = data.iloc[len(data)-11].above
except:
continue

# Assessing crossover in last 10 days
if eleventh == False and last_10 == True:
buy_list.append(ticker)
else:
pass

return buy_list
  1. The dates are set up where you can run the function on any given day, and it will return you a list of stocks that satisfy the strategy criteria relative to the current day.
  2. No effort was yet made as to the optimization of the function. With a list of about 3,500 ticker symbols, I killed the kernel after the function was going on an hour of run time. Therefore, as you will see below, I limited the function to an input of only 100 ticker symbols.
['DDD','MMM','WBAI','EGHT','AHC','AOS','ATEN','AIR','AAN','ABB','ABT','ABBV','ANF','AGD','AWP','ACP','JEQ','ASGI','AOD','ABM','AKR','ACEL','ACN','ACCO','ATV','AYI','GOLF','ADX','PEO','ADCT','AGRO','ADNT','ADT','ATGE','AAP','WMS','ASIX','AVK','IMPX.U','ACM','AEFC','AEB','AEG','AENZ','AER','AJRD','AMG','MGR','MGRB','AFL','MITT','MITT^A','MITT^B','MITT^C','AGCO','A','AEM','ADC','AL','AL^A','APD','AJAX.U','ALP^Q','ALG','AGI','ALK','AIN','ALB','ACI','AA','ALC','ALEX','ALX','ARE','AQN','AQNA','AQNB','BABA','Y','ATI','ALLE','ALE','ADS','AFB','AWF','AB','AIO','CBH','NCV','NCV^A','NCZ','NCZ^A','ACV','NFJ','NIE','ALSN','ALL','ALL^B','ALL^G','ALL^H']
['AIR',
'ABBV',
'AWP',
'ACCO',
'AGRO',
'AFL',
'ADC',
'ALK',
'AIN',
'ALEX',
'Y',
'ATI',
'ALLE',
'ADS']

Conclusion

Given that the function returned a suitable list, I think we can chalk this exercise up to a success. As we continue to explore python within the field of finance, the goals will get more complex, and the outcomes, hopefully, will incrementally be more useful in real-world applications.