Calculating moving averages for stock prices, visualizing

Question

Suppose you are given the following dataset containing closing prices of a stock across a number of days:

day price
0 1 119
1 2 146
2 3 147
3 4 119
4 5 102
5 6 124
6 7 148
7 8 140
8 9 133
9 10 153
10 11 155
11 12 162
12 13 150
13 14 171
14 15 167

Using this dataset, add a column with the 3 and 5-day moving averages for the closing prices. Then, visualize the output with a line chart.

To help get you started, here is code to load the same dataset into a Pandas dataframe:

#code to generate example dataframe
import pandas as pd
raw_data = {'day': [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], 
        'price': [119, 146, 147, 119, 102, 124, 148, 140, 133, 153, 155, 162, 150, 171, 167]
        }
df = pd.DataFrame(raw_data, columns = ['day', 'price'])

Solution

Access restricted

Subscribe to premium account to see the solution.

Get premium now