Exponential Moving Average (EMA)

The Exponential Moving Average (EMA) is a cumulative smoothing calculation which applies greater weighting to more recient data within the period. This weighting exponentially decreases the further away the data is from the current data point in the timeseries.

About

The Exponential Moving Average (EMA) is a cumulative calculation including all previous periods and applies a weighted average where the weighting exponentially decreases with each previous period. This results in a higher weighting for more recent data and lower weighting to past data. This is incontracts to the Simple Moving Average (SMA) which applies an even weighting to all previous periods furthermore the SMA only includes data for the previous n periods in its calculation.

In practical terms this means the EMA reacts to recent data faster than the SMA as shown in the graph below where we compare SMA and EMA with the same period.

You can see that the EMA line reacts to changes in the adjclose faster than the SAM does but like the SMA the EMA still exhibits similar smoothing characteristics.

Equation

Below is the equation for the EMA (note K is the weighting factor discussed above):

Inputs:

  • Period (p) – this is the number of previous data points you want to include in the average calculation.
  • Index (i) – this is the time series index (e.g. date of current row in time series data)
  • Input (input) – this is the data value (e.g. price data of a stock).
  • ema[i-1] – this is the previously calculated EMA value, if you are calculating the EMA for the first time then use the SMA

Code

Below is the python code to calculate the EMA. To access the maintained version of this code please see python library techind.

Graph

The below graphs shows multiple SMA indicators with varying periods against the underlying timeseries.

Links

  • techind TestPyPi – link

About the author

Leave a Reply

Your email address will not be published. Required fields are marked *