開始使用免費開始

計算 RSI

RSI 的計算遵循一個相對直觀的公式。RS(Relative Strength,相對強弱)是選定的 n 期內,上漲幅度的平均值,除以下跌幅度的平均值。

\( RSI = 100 - 100/(1+RS)\)

其中: RS = 上漲幅度平均值 / 下跌幅度平均值

以上運算在 Python 中用一行程式碼就能完成。在這個練習中,你會用 Google 股票的歷史日價做第一次 RSI 計算。

日價資料已載入為 stock_data。另外,talib 也已替你匯入。

本練習屬於課程

Financial Trading in Python

檢視課程

練習說明

  • 使用 talib 中合適的方法,對價格資料的 Close 欄位計算 RSI,並將結果存到新欄位 RSI_14
  • 以 21 期作為時間區間計算 RSI,並將結果存到新欄位 RSI_21
  • 列印 stock_data 的最後 5 列。

動手互動練習

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

# Calculate RSI with the default time period
stock_data['RSI_14'] = ____(stock_data['____'])

# Calculate RSI with a time period of 21
stock_data['RSI_21'] = ____

# Print the last five rows
print(stock_data.____())
編輯並執行程式碼