2025年11月14日 星期五

Python 學習筆記 : 技術分析套件 pandas-ta 用法 (一)

我昨天在樹莓派 Pi 400 主機上安裝技術指標套件 Ta-Lib 失敗, 原因是此套件太舊了, 在 Linux 主機上安裝需先編譯 C 原始碼, 然後再用 pip 安裝套上 Python 外殼, 以前在舊版 Raspbian OS Buster 上曾經成功安裝, 但在 Bookworm 就是不行, 只好放棄. 

在詢問 ChatGPT 替代方案時, 它建議了兩款套件 : ta 與 pandas-ta, 前者我已測試過, padas-ta 則是初次得知, 此套件為 Kevin Johnson 所創建, 提供 +130 個技術指標, 使用純 Python 開發, 因此用 pip 即可安裝, 比 Ta-Lib 方便很多, 參考 :  


本系列全部文章索引參考 :



1. 安裝最新版 pandas-ta :

pandas-ta 在 PyPi 網站上的最新版 0.4.71b 要求 Python >= 3.12, 我的 MSI 桌機符合此條件, 可直接用 pip install 安裝 : 

PS C:\Users\USER> pip install pandas-ta   
Collecting pandas-ta
  Downloading pandas_ta-0.4.71b0-py3-none-any.whl.metadata (2.3 kB)
Collecting numba==0.61.2 (from pandas-ta)
  Downloading numba-0.61.2-cp312-cp312-win_amd64.whl.metadata (2.9 kB)
Collecting numpy>=2.2.6 (from pandas-ta)
  Downloading numpy-2.3.4-cp312-cp312-win_amd64.whl.metadata (60 kB)
Collecting pandas>=2.3.2 (from pandas-ta)
  Downloading pandas-2.3.3-cp312-cp312-win_amd64.whl.metadata (19 kB)
Collecting tqdm>=4.67.1 (from pandas-ta)
  Downloading tqdm-4.67.1-py3-none-any.whl.metadata (57 kB)
Collecting llvmlite<0.45,>=0.44.0dev0 (from numba==0.61.2->pandas-ta)
  Downloading llvmlite-0.44.0-cp312-cp312-win_amd64.whl.metadata (5.0 kB)
Collecting numpy>=2.2.6 (from pandas-ta)
  Downloading numpy-2.2.6-cp312-cp312-win_amd64.whl.metadata (60 kB)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\user\appdata\local\programs\python\python312\lib\site-packages (from pandas>=2.3.2->pandas-ta) (2.9.0.post0)
Collecting pytz>=2020.1 (from pandas>=2.3.2->pandas-ta)
  Downloading pytz-2025.2-py2.py3-none-any.whl.metadata (22 kB)
Collecting tzdata>=2022.7 (from pandas>=2.3.2->pandas-ta)
  Downloading tzdata-2025.2-py2.py3-none-any.whl.metadata (1.4 kB)
Requirement already satisfied: colorama in c:\users\user\appdata\local\programs\python\python312\lib\site-packages (from tqdm>=4.67.1->pandas-ta) (0.4.6)
Requirement already satisfied: six>=1.5 in c:\users\user\appdata\local\programs\python\python312\lib\site-packages (from python-dateutil>=2.8.2->pandas>=2.3.2->pandas-ta) (1.16.0)
Downloading pandas_ta-0.4.71b0-py3-none-any.whl (240 kB)
Downloading numba-0.61.2-cp312-cp312-win_amd64.whl (2.8 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.8/2.8 MB 6.3 MB/s eta 0:00:00
Downloading numpy-2.2.6-cp312-cp312-win_amd64.whl (12.6 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12.6/12.6 MB 4.5 MB/s eta 0:00:00
Downloading pandas-2.3.3-cp312-cp312-win_amd64.whl (11.0 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 11.0/11.0 MB 3.5 MB/s eta 0:00:00
Downloading tqdm-4.67.1-py3-none-any.whl (78 kB)
Downloading llvmlite-0.44.0-cp312-cp312-win_amd64.whl (30.3 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 30.3/30.3 MB 4.0 MB/s eta 0:00:00
Downloading pytz-2025.2-py2.py3-none-any.whl (509 kB)
Downloading tzdata-2025.2-py2.py3-none-any.whl (347 kB)
Installing collected packages: pytz, tzdata, tqdm, numpy, llvmlite, pandas, numba, pandas-ta
  Attempting uninstall: tqdm
    Found existing installation: tqdm 4.66.5
    Uninstalling tqdm-4.66.5:
      Successfully uninstalled tqdm-4.66.5
  Attempting uninstall: numpy
    Found existing installation: numpy 1.26.4
    Uninstalling numpy-1.26.4:
      Successfully uninstalled numpy-1.26.4
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
mediapipe 0.10.21 requires numpy<2, but you have numpy 2.2.6 which is incompatible.
Successfully installed llvmlite-0.44.0 numba-0.61.2 numpy-2.2.6 pandas-2.3.3 pandas-ta-0.4.71b0 pytz-2025.2 tqdm-4.67.1 tzdata-2025.2

這裡的 Error 是因為 MediaPipe 使用的 Numpy 版本與 pandas-ta 衝突, 可匯入 pandas_ta 測試看看有無影響 : 

PS C:\Users\USER> python   
Python 3.12.2 (tags/v3.12.2:6abddd9, Feb  6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas_ta as ta   
>>> print(ta.version)   
0.4.71b0

注意, 雖然安裝的套件名稱為 pandas-ta, 但匯入使用時的模組名稱為 pandas_ta. 


2. 安裝舊版 pandas-ta (v0.3.14b) :

我的 LG Gram 筆電的系統 Python 環境為 v3.10 直接用 pip 安裝 pandas-ta 會出現錯誤 : 

D:\python\test>pip install pandas-ta   
ERROR: Ignored the following versions that require a different python version: 0.4.67b0 Requires-Python >=3.12; 0.4.71b0 Requires-Python >=3.12
ERROR: Could not find a version that satisfies the requirement pandas-ta (from versions: none)
ERROR: No matching distribution found for pandas-ta

因為 pandas-ta 在 Pypi 上的最新版本 0.4.71b 要求 Python >= 3.12, 找不到適合 Python 3.10 的  pandas-ta 版本所以報錯. 支援 Python 3.10 的 pandas-ta 最後穩定版是 0.3.14b, 原作者的 GitHub 與個人網站上均已刪除, 還好我在 Source Forge 找到 0.3.14b 版原始碼的壓縮檔 :





下載 zip 檔後解壓縮可看到包含原始碼的套件檔案結構 :




開啟 CMD 或 PS 視窗, 切換到解壓縮後的資料夾下, 執行 python -m build 指令建立 Python 套件的發佈檔案 (build artifacts), 將套件打包成 .whl 或 .tar.gz 等可讓 pip 安裝的檔案 : 

D:\python\test>cd pandas-ta-0.3.14b   
D:\python\test\pandas-ta-0.3.14b>python -m build   
* Creating isolated environment: venv+pip...
* Installing packages in isolated environment:
  - setuptools >= 40.8.0
* Getting build dependencies for sdist...
C:\Users\tony1\AppData\Local\Temp\build-env-3dr3arg6\lib\site-packages\setuptools\dist.py:332: InformationOnly: Normalizing '0.3.14b' to '0.3.14b0'
  self.metadata.version = self._normalize_version(self.metadata.version)
C:\Users\tony1\AppData\Local\Temp\build-env-3dr3arg6\lib\site-packages\setuptools\dist.py:759: SetuptoolsDeprecationWarning: License classifiers are deprecated.
!!

        ********************************************************************************
        Please consider removing the following classifiers in favor of a SPDX license expression:

        License :: OSI Approved :: MIT License

        See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
        ********************************************************************************

!!
  self._finalize_license_expression()
* Building sdist...
C:\Users\tony1\AppData\Local\Temp\build-env-3dr3arg6\lib\site-packages\setuptools\dist.py:332: InformationOnly: Normalizing '0.3.14b' to '0.3.14b0'
  self.metadata.version = self._normalize_version(self.metadata.version)
C:\Users\tony1\AppData\Local\Temp\build-env-3dr3arg6\lib\site-packages\setuptools\dist.py:759: SetuptoolsDeprecationWarning: License classifiers are deprecated.
!!

        ********************************************************************************
        Please consider removing the following classifiers in favor of a SPDX license expression:

        License :: OSI Approved :: MIT License

        See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
        ********************************************************************************

!!
  self._finalize_license_expression()
* Building wheel from sdist
* Creating isolated environment: venv+pip...
* Installing packages in isolated environment:
  - setuptools >= 40.8.0
* Getting build dependencies for wheel...
C:\Users\tony1\AppData\Local\Temp\build-env-zzd8ujoa\lib\site-packages\setuptools\dist.py:332: InformationOnly: Normalizing '0.3.14b' to '0.3.14b0'
  self.metadata.version = self._normalize_version(self.metadata.version)
C:\Users\tony1\AppData\Local\Temp\build-env-zzd8ujoa\lib\site-packages\setuptools\dist.py:759: SetuptoolsDeprecationWarning: License classifiers are deprecated.
!!

        ********************************************************************************
        Please consider removing the following classifiers in favor of a SPDX license expression:

        License :: OSI Approved :: MIT License

        See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
        ********************************************************************************

!!
  self._finalize_license_expression()
* Building wheel...
C:\Users\tony1\AppData\Local\Temp\build-env-zzd8ujoa\lib\site-packages\setuptools\dist.py:332: InformationOnly: Normalizing '0.3.14b' to '0.3.14b0'
  self.metadata.version = self._normalize_version(self.metadata.version)
C:\Users\tony1\AppData\Local\Temp\build-env-zzd8ujoa\lib\site-packages\setuptools\dist.py:759: SetuptoolsDeprecationWarning: License classifiers are deprecated.
!!

        ********************************************************************************
        Please consider removing the following classifiers in favor of a SPDX license expression:

        License :: OSI Approved :: MIT License

        See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
        ********************************************************************************

!!
  self._finalize_license_expression()
Successfully built pandas_ta-0.3.14b0.tar.gz and pandas_ta-0.3.14b0-py3-none-any.whl

這樣就得到了 Build 後的 gz 與 whl 檔, 放在 dist 資料夾下 : 




用 pip 安裝 dist 底下的 whl 檔 :

D:\python\test\pandas-ta-0.3.14b>pip install dist\pandas_ta-0.3.14b0-py3-none-any.whl   
Processing d:\python\test\pandas-ta-0.3.14b\dist\pandas_ta-0.3.14b0-py3-none-any.whl
Requirement already satisfied: pandas in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from pandas-ta==0.3.14b0) (2.3.1)
Requirement already satisfied: numpy>=1.22.4 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from pandas->pandas-ta==0.3.14b0) (1.26.4)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from pandas->pandas-ta==0.3.14b0) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from pandas->pandas-ta==0.3.14b0) (2025.2)
Requirement already satisfied: tzdata>=2022.7 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from pandas->pandas-ta==0.3.14b0) (2025.2)
Requirement already satisfied: six>=1.5 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from python-dateutil>=2.8.2->pandas->pandas-ta==0.3.14b0) (1.17.0)
Installing collected packages: pandas-ta
Successfully installed pandas-ta-0.3.14b0

進入互動環境匯入 pandas_ta 模組檢視版本 : 

D:\python\test\pandas-ta-0.3.14b>python    
Python 3.10.11 (tags/v3.10.11:7d4cc5a, Apr  5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas_ta as ta   
>>> print(ta.version)   
0.3.14b0

舊版功能其實已很完整, 接下來就可以在筆電上進行測試啦. 

我把 pandas-ta 0.3.14b 的源碼套件壓縮檔與打包後的 whl 都放在 GitHub :


如果 Python 環境是 3.10 可直接下載 whl 檔安裝, 否則就下載 zip 自行打包為 whl 檔後安裝. 


3. 三種技術指標套件比較 :

Ta-Lib, ta, 與 pandas-ta 三種技術指標套件之特性與功能比較如下表 :


特性 Ta-Lib ta pandas-ta
安裝難度 高 (需要編譯 C 原始碼) 低 (用 pip 直接安裝) 中 (舊版需手動安裝, 最新版需 Python ≥ 3.12
指標數量 200+ 指標 50+ 指標 130+ 指標
pandas 整合度 中 (需要轉成 numpy) 高 (直接操作 DataFrame) 高 (與 DataFrame 完美整合)
運算速度 非常快 (C 語言實作) 中 (純 Python) 中(純 Python)
更新頻率 低 (成熟但不常更新) 中 (偶爾更新) 高 (維護活躍)
適用情境 高速計算, 大型資料, 量化策略 小型專案、快速加入技術指標 策略開發, 回測, pandas 分析整合


與 ta 比起來, pandas-ta 不僅實作之指標數量多, 且更新頻率也高.

我之前測試 ta 時也做過 Ta-Lib 與 ta 的比較, 參考 :


沒有留言 :