開始使用免費開始

計算 ADX

平均方向性移動指數(ADX)由 J. Welles Wilder 提出,用來衡量趨勢強度。它結合另外兩個指標:正向指數(+DI)與負向指數(-DI),其計算過程相當冗長。不過在 Python 中,你只要一行程式碼就能算出來。這個練習中,你會用特斯拉股票的日收盤資料實作第一個 ADX 指標。

歷史的每日股價資料已載入在 stock_data。此外,talib 也已為你匯入。

本練習屬於課程

Financial Trading in Python

檢視課程

練習說明

  • 使用 talib 中對應的函式,並以 stock_dataHighLowClose 欄位計算 ADX。將結果存成新欄位 ADX_14
  • 再次計算 ADX,這次把預設期間改為 21,並將結果存成新欄位 ADX_21
  • 列印 stock_data 的最後 5 列。

動手互動練習

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

# Calculate the ADX with the default time period
stock_data['ADX_14'] = ____(stock_data['____'],
                            stock_data['____'], 
                            stock_data['____'])

# Calculate the ADX with the time period set to 21
stock_data['ADX_21'] = ____

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