Moving Averages with Python

In this article, I will take you through how we can implement Moving Averages with Python. Moving averages are commonly used by technical analysts and traders. If you’ve never heard of a moving average, you’ve probably at least seen one in practice.

A moving average can help an analyst filter out the noise and create a smooth curve from an otherwise noisy curve. It is important to note that the moving averages are lagged because they are based on historical data and not on the current price.

Also, Read – Edge AI in Machine Learning.

An example of using moving averages is to follow crossovers. For example, a bullish cross occurs when the short term SMA crosses above the long term SMA. A bearish cross occurs when the short-term SMA crosses below the long-term SMA.

Implementing Moving Averages with Python

I’ll start by plotting the desired stock over one month. In this case, I will check AMD. I am a huge fan of the IEX API and love using the Python API for IEX. Let’s start with the task of Moving Averages with Python:

import pandas as pd
import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt
import pyEX as p
ticker = 'AMD'
timeframe = '1y'
df = p.chartDF(ticker, timeframe)
df = df[['close']]
df.reset_index(level=0, inplace=True)
plt.plot(df.ds, df.y) language: JavaScript (javascript)
Image for post

Then we run a few lines to make the simple moving average work. The code should be intuitive. Let’s see how we can do this:

rolling_mean = df.y.rolling(window=20).mean()
rolling_mean2 = df.y.rolling(window=50).mean()
plt.plot(df.ds, df.y, label='AMD')
plt.plot(df.ds, rolling_mean, label='AMD 20 Day SMA', color='orange')
plt.plot(df.ds, rolling_mean2, label='AMD 50 Day SMA', color='magenta')
plt.legend(loc='upper left') language: JavaScript (javascript)
Image for post

Next, I will implement the Exponential Moving Average (EMA). For that, we add a bit of granularity and go on a shorter time frame. In this case, I do a one-year delay:

exp1 = df.y.ewm(span=20, adjust=False).mean()
exp2 = df.y.ewm(span=50, adjust=False).mean()
plt.plot(df.ds, df.y, label='AMD')
plt.plot(df.ds, exp1, label='AMD 20 Day EMA')
plt.plot(df.ds, exp2, label='AMD 50 Day EMA')
plt.legend(loc='upper left') language: PHP (php)
moving averages implementation

Let’s use the same graphic as above and take a look at our concept of prior crossbreeding. We can see that by using this signal we could have predicted the price trend of AMD. When the short term crosses over the long term, we get a buy signal. When the short term goes below the long term, we get a sell signal.

Moving Averages

Also, Read – Main Parts of a Robot: Robots with Python.

You can see above how the stock turned bullish for a long time after the bullish cross and bearish for a long time after the bearish cross. I hope you liked this article on implementing Moving Averages with Python. Feel free to ask you valuable questions in the comments section below. You can also follow me on Medium to learn every topic of Machine Learning and Python.

Follow Us:

Articles: 75

Leave a Reply