Simple Moving Average with Python from scratch.
What is a moving average?
Moving average, also known as rolling average or running average, is a statistical calculation used to analyze data points by creating a series of averages of different subsets of the complete data set. It is a type of finite impulse response filter and can be simple, cumulative, or weighted.
A simple moving average (SMA) is the unweighted mean of the previous k data points. For example, the mean over the last k entries of a data set containing n entries could be closing prices of a stock. If these data points are represented by p1, p2, …, pn, then the mean over the last k data points (days in this example) is denoted as SMAk and calculated as follows:
SMAs are commonly used to determine trend direction. If the SMA is moving up, the trend is up. If the SMA is moving down, the trend is down. In this article, we will implement this trend indicator in Python.
To start, we need to import the libraries we want to use. We will import matplotlib.pyplot and numpy.
import matplotlib.pyplot as plt
import numpy as np
Next, we will write the main function that will perform the calculation of the Simple Moving Average. This function will take as arguments the data we want to perform the calculation and the time-lag we want. The function will replace the first n (lag value) data points with Nan (Not a number) and the rest will be replaced with the average value of the n (lag value) last data points.
def SMA(data,lag=3):
sma = []
for i in range(lag):
sma.append(np.nan)
for i in range(lag,len(data)):
sma.append(np.mean(data[i-lag:i]))
return np.array(sma)
We will then create two datasets to test our function on. One random dataset, created by taking values from a Normal distribution, and the other one from a Sinusoid function.
random_data = np.random.normal(0,1,100)
x1 = np.linspace(0,1,100)
x2 = np.linspace(0,2*np.pi,100)
y = np.sin(x2)
We will visualize them.
plt.plot(x1,random_data)
plt.plot(x2,y)
Next, we will calculate the Simple Moving Averages and visualize the results to better understand the method.
sma_rand = SMA(random_data)
sma_y = SMA(y)
plt.plot(x1,random_data)
plt.plot(x1,sma_rand)
plt.plot(x2,y)
plt.plot(x2,sma_y)
plt.show()
The full code for this article can be found on GitHub.