Time Series — What is Moving Averages and its types with Python implementation on Google stocks data

Rohan Paris
4 min readApr 14, 2022

--

The moving average (MA) is a simple technical analysis tool that smooths out price trends by filtering out the noise from random short-term price fluctuations. The average is taken over a specific period of time, like 10 days, 20 minutes, 30 weeks, or any time period.

MA with a shift of 1

Types of MA:
1. Simple Moving Avrage (SMA)
2. Cumulative Moving Average (CMA)
3. Exponential Moving Average (EMA)
4. Exponentially Weighted Moving Average (EWMA)

  1. Simple Moving Average (SMA):

A simple moving average (SMA) is an arithmetic moving average calculated by adding recent prices and then dividing that figure by the number of time periods in the calculation average. Let us consider the below example where we have the last seven opening prices of a stock.

SMA for window=3

The null values in the first few rows can be substituted with a minimum value using the min_periods parameter of rolling() function.

2. Cumulative Moving Average (CMA)

Let us consider the below example to understand CMA.

expanding() function can be used to calculate CMA.

3. Exponential Moving Average (EMA)

EMA is calculated by using the below formula

EMA = ((xi- previous EMA)*multiplier)+ previous EMA

multiplier = 2/(window+1)

Please note that for the Stock Open value 14, we have calculated an SMA as the previous EMA is null.

ewm() function can be used to calculate EMA.

4. Exponentially Weighted Moving Average (EWMA)

The EWMA’s simple mathematical formulation to show the value of the moving average at a time ‘t’ is described below:

EWMA(t) = α*x(t) + (1-α)*EWMA(t-1)

where,

α = The weight decided by the user (value is between 0 and 1)

x = Value of the series in the current period

The above equation can be rewritten in terms of older weights, as shown below:

EWMA(t) = α*x(t) + (1-α)*(α*x(t-1) + (1-α)*EWMA(t-2))

The process continues until we reach the base term EWMA(0). The equation can be rearranged to show that the EWMA(t) is the weighted average of all the preceding observations, where the weight of the observation rt–k is given by: α*(1-x)^k

Since α is between 0 and 1, the weight becomes smaller as k becomes larger. In other words, as we go back further in history, the weight becomes smaller.

If α=1, that means only the most recent data has been used to measure EWMA. If α is nearing 0, that means more weightage is given to older data, and if α is near 1, that means newer data has been given more weightage.

Python implementation on Google stocks data

In order to get the stocks data, we will use a library called pandas-datareader. This library can be installed using the below command:

!pip install pandas-datareader

Next, we will import the following packages

import pandas_datareader as pdr
import pandas as pd
import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt
%matplotlib inline

get_data_yahoo function is used to get the stocks data from yahoo finance

## Read Google stocks data from yahoo
df_google = pdr.get_data_yahoo(‘GOOGL’)
df_google.head()

SMA with a window of 10 days is calculated as:

df_google[‘Open: 10 days rolling’]=df_google[‘Open’].rolling(window=10, min_periods=1).mean()

CMA is calculated as:

df_google[‘Open: CMA’]=df_google[‘Open’].expanding().mean()

EMA:

df_google[‘EMA: 10 days’]=df_google[‘Open’].ewm(span=10).mean()

EWMA for α=0.1:

df_google[‘EWMA_0.1’]=df_google[‘Open’].ewm(alpha=0.1, adjust=False).mean()

--

--

Rohan Paris
Rohan Paris

No responses yet