計算並繪製 SMA
每日價格資料本質上雜訊多且不整潔。你想分析 Apple 股票的每日價格資料,並計畫加入一個簡單移動平均(SMA)指標來平滑資料。你特別決定使用 50 日 SMA。
股票資料已預先載入在 aapl_data,且已將 matplotlib.pyplot 以 plt 匯入。圖表的其他自訂項(例如標題與圖例)也已為你準備好。
本練習屬於課程
Financial Trading in Python
練習說明
- 使用
Close價計算 50 日 SMA,並將結果存到名為sma_50的新欄位。 - 使用
sma_50與Close欄位的資料繪製折線圖。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Calculate SMA
aapl_data['sma_50'] = aapl_data['____'].____.mean()
# Plot the SMA
____(aapl_data['____'], color='green', label='SMA_50')
# Plot the close price
____(aapl_data['____'], color='red', label='Close')
# Customize and show the plot
plt.title('Simple moving averages')
plt.legend()
plt.show()