In order to computer the moving average, we will use the rolling function.
#120 days moving averagemoving_averages=aapl[["Open","High","Low","Close","Volume"]].rolling(window=120).mean()moving_averages.tail()
Open
High
Low
Close
Volume
Date
1980-12-18
28.457667
28.551917
28.385000
28.385000
139495.000000
1980-12-17
28.410750
28.502917
28.338083
28.338083
141772.500000
1980-12-16
28.362833
28.453917
28.289167
28.289167
141256.666667
1980-12-15
28.335750
28.426833
28.262083
28.262083
144321.666667
1980-12-12
28.310750
28.402833
28.238167
28.238167
159625.000000
%matplotlibinlineimportmatplotlib.pyplotaspltplt.plot(aapl.index,aapl.Open,label='Open price')plt.plot(moving_averages.index,moving_averages.Open,label="120 MA Open price")plt.legend()plt.show()