開始使用免費開始

計算並繪製 SMA

每日價格資料本質上雜訊多且不整潔。你想分析 Apple 股票的每日價格資料,並計畫加入一個簡單移動平均(SMA)指標來平滑資料。你特別決定使用 50 日 SMA。

股票資料已預先載入在 aapl_data,且已將 matplotlib.pyplotplt 匯入。圖表的其他自訂項(例如標題與圖例)也已為你準備好。

本練習屬於課程

Financial Trading in Python

檢視課程

練習說明

  • 使用 Close 價計算 50 日 SMA,並將結果存到名為 sma_50 的新欄位。
  • 使用 sma_50Close 欄位的資料繪製折線圖。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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()
編輯並執行程式碼