11. Visualizing Moving Averages#

The moving average is a widely used statistical technique in time series analysis that helps smooth out fluctuations in data to identify underlying trends. By averaging a specific number of past data points, the moving average creates a smooth curve that provides insight into the long-term direction of a dataset. This technique is particularly useful for time series data, where irregularities and noise can obscure the true signal.

There are several types of moving averages, including the simple moving average (SMA), which calculates the mean of a fixed number of periods, and the weighted moving average (WMA), which assigns different weights to past observations, giving more importance to recent data. Exponential moving averages (EMAs) are another variation that applies a diminishing weight to older observations, making them responsive to recent changes.

To visualize moving averages effectively, they can be plotted alongside the original time series data in a line chart. The original data is often represented by a solid line, while the moving average is displayed as a separate line, allowing viewers to easily compare the smoothed trend against the raw data. Different colors or line styles can be used to distinguish between the two lines, enhancing clarity. This visualization technique can help identify patterns and trends over time, such as upward or downward movements, seasonal effects, and potential turning points in the data. In addition to line charts, moving averages can also be displayed using area charts, where the area under the moving average line is shaded to emphasize the trend.

Moving averages are particularly useful in fields like finance, economics, and meteorology, where analyzing trends over time is essential for decision-making. For example, in stock market analysis, moving averages can help traders identify entry and exit points by signaling when an asset is trending upward or downward. However, it is essential to choose an appropriate window size for the moving average. A shorter window may capture short-term fluctuations but could lead to overfitting and noise, while a longer window might obscure important short-term trends. Ultimately, the choice of moving average and its visualization should align with the analysis goals and the characteristics of the data being studied.

Getting ready#

In addition to plotly, numpy and pandas, make sure the yfinance and scipy Python library avaiable in your Python environment You can install it using the command:

pip install scipy, yfinance
  1. Import the Python modules numpy, pandas, and yfinance

import numpy as np
import pandas as pd
import yfinance as yf
  1. Download the financial time series that we are going to use for this recipe

data = yf.download('MSFT',start="2020-01-01")
YF.download() has changed argument auto_adjust default to True
[*********************100%***********************]  1 of 1 completed

How to do it#

  1. Calculate the moving averages using the functions rolling and mean

df = data
x1 = 20
x2 = 120
x3 = 250

y =  "-day moving average"

string1 = str(x1) + str(y)
string2 = str(x2) + str(y)
string3 = str(x3) + str(y)

df['MA1'] = df['Open'].rolling(window=x1).mean()
df['MA2'] = df['Open'].rolling(window=x2).mean()
df['MA3'] = df['Open'].rolling(window=x3).mean()

and inspect the new DataFrame

df.head()
Price Close High Low Open Volume MA1 MA2 MA3
Ticker MSFT MSFT MSFT MSFT MSFT
Date
2020-01-02 153.630722 153.735936 151.440376 151.870792 22622100 NaN NaN NaN
2020-01-03 151.717743 152.989871 151.182113 151.430809 21116200 NaN NaN NaN
2020-01-06 152.109879 152.176840 149.699531 150.244735 20813700 NaN NaN NaN
2020-01-07 150.723022 152.722074 150.474341 152.387313 21634100 NaN NaN NaN
2020-01-08 153.123764 153.802875 151.076886 152.014237 27746500 NaN NaN NaN
  1. Import the plotly.graph_objects module as go

import plotly.graph_objects as go
  1. Create a Figure object; add the plot the original time series using the function go.Scatter; and customise the title and dimensions of the plot using the method update_layout

fig = go.Figure(data=[go.Scatter(x=df.index, y=df['Close'], 
                                 name = 'Stock Price', 
                                 line = dict(color = 'blue'), )
                      ])

fig.update_layout(title="MSF Stock and Moving Averages", height = 600, width = 1000,)
fig.show()
  1. Add the different moving average curves by adding into the data list. Notice that we specify different colors for each line

fig = go.Figure(data=[go.Scatter(x=df.index, y=df['Close'], 
                                 name = 'Stock Price', 
                                 line = dict(color = 'blue'), ),
                      go.Scatter(x=df.index, y=df.MA1, line=dict(color='green'), name =  string1),
                      go.Scatter(x=df.index, y=df.MA2, line=dict(color='orange'), name = string2),
                      go.Scatter(x=df.index, y=df.MA3, line=dict(color='red'), name = string3)
                      ])

fig.update_layout(title="MSF Stock and Moving Averages", height = 600, width = 1000,)
fig.show()