最近打算活化資產, 讓兩台 Pi 3 做量化投資的統計運算 (已空轉好幾年), 在這之前要安裝所需要的 Python 第三方套件, 如下表所示 :
量化投資第三方套件 | 說明 |
requests | 爬蟲工具, 用來擷取網頁 |
BeautifulSoup | 爬蟲工具, 用來解析所擷取之網頁 |
Matplotlib | 繪圖工具, 用來繪製統計圖形 |
Pandas | 資料分析工具, 用來做資料前處理與時間序列分析 |
twstock | 擷取台股價量資料 (歷史 & 即時) |
Ta-Lib | 技術分析套件, 包含價量之平均, 動能, 量能, 波動, 與型態等指標 |
其中 requests, BeautifulSoup, Matplotlib, 以及 Pandas 都已經安裝過了, 這幾個套件在 Windows 都可以用 pip install 順利安裝, 在樹莓派上大部分套件用 pip3 install 安裝也沒問題, 只有 Ta-Lib 較麻煩, 必須先用 make 從 0.4.0 版原始碼編譯安裝後, 再用 pip3 install 安裝到最新的 0.4.24 版, 參考 :
以下紀錄安裝程序 (Windows 主要是測試用, 而 Pi 3 則是運轉用) :
1. 在 Windows 安裝 twstock 與 Ta-Lib :
在 Windows 安裝 Ta-Lib 與 twstock 都很快 :
D:\test\python>pip install twstock -U
Requirement already satisfied: twstock in c:\python37\lib\site-packages (1.3.1)
Requirement already satisfied: requests in c:\python37\lib\site-packages (from twstock) (2.21.0)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\python37\lib\site-packages (from requests->twstock) (3.0.4)
Requirement already satisfied: urllib3<1.25,>=1.21.1 in c:\python37\lib\site-packages (from requests->twstock) (1.24.1)
Requirement already satisfied: certifi>=2017.4.17 in c:\python37\lib\site-packages (from requests->twstock) (2018.11.29)
Requirement already satisfied: idna<2.9,>=2.5 in c:\python37\lib\site-packages (from requests->twstock) (2.8)
但 Ta-Lib 不要直接用 pip install Ta-Lib 安裝, 因為過程中會出現編譯錯誤, 必須到加州大學爾灣分校網站根據本機 Python 版本與位元長度 (32/64)下載正確的 whl 檔, 然後再用 pip install 離線方式安裝 :
例如本機 Python 是 64 位元 3.7 版, 故要下載上圖中框起來的檔案來安裝 :
D:\test\python>pip install TA_Lib-0.4.24-cp37-cp37m-win_amd64.whl
Processing d:\test\python\ta_lib-0.4.24-cp37-cp37m-win_amd64.whl
Requirement already satisfied: numpy in c:\python37\lib\site-packages (from TA-Lib==0.4.24) (1.19.4)
Installing collected packages: TA-Lib
Successfully installed TA-Lib-0.4.24
2. 在樹莓派 Pi 3 安裝 twstock 與 Ta-Lib :
在樹莓派安裝 twstock 也很快 :
pi@raspberrypi:~ $ pip3 install twstock
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting twstock
Downloading https://files.pythonhosted.org/packages/38/a0/61e6b65093a7564cb8e34aea54de28113bfe24409fb05e173360fbf5a35d/twstock-1.3.1-py3-none-any.whl (1.9MB)
Requirement already satisfied: requests in /usr/lib/python3/dist-packages (from twstock) (2.21.0)
Installing collected packages: twstock
Successfully installed twstock-1.3.1
但安裝 Ta-Lib 卻沒這麼容易, 如果直接用 pip3 install Ta-Lib 安裝會出現很多 C 原始碼的編譯錯誤 :
pi@raspberrypi:~ $ sudo pip3 install TA-Lib
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting TA-Lib
Downloading https://files.pythonhosted.org/packages/78/f5/59f1d7d0c43d3d96e93249ae9d5415b42591362bfcf2f4e50574ad063312/TA-Lib-0.4.24.tar.gz (269kB)
.... (略) .....
talib/_ta_lib.c:682:10: fatal error: ta-lib/ta_defs.h:
#include "ta-lib/ta_defs.h"
^~~~~~~~~~~~~~~~~~
compilation terminated.
error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1
----------------------------------------
Failed building wheel for TA-Lib
Running setup.py clean for TA-Lib
Failed to build TA-Lib
Installing collected packages: TA-Lib
Running setup.py install for TA-Lib ... - \ | / - error
Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-mopxrg5g/TA-Lib/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-84sdcdd5/install-record.txt --single-version-externally-managed --compile:
/tmp/pip-install-mopxrg5g/TA-Lib/setup.py:77: UserWarning: Cannot find ta-lib library, installation may fail.
warnings.warn('Cannot find ta-lib library, installation may fail.')
.... (略) .....
talib/_ta_lib.c:682:10: fatal error: ta-lib/ta_defs.h:
#include "ta-lib/ta_defs.h"
^~~~~~~~~~~~~~~~~~
compilation terminated.
error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1
----------------------------------------
Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-mopxrg5g/TA-Lib/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-84sdcdd5/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-mopxrg5g/TA-Lib/
在 PyPi 網站的 Ta-Lib 套件說明文件中有 Linux 系統上的安裝方式指引, 參考 :
(1). 下載 Ta-Lib 0.4.0 版原始碼壓縮檔 :
pi@raspberrypi:~ $ wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
--2022-09-01 10:44:33-- http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
正在查找主機 prdownloads.sourceforge.net (prdownloads.sourceforge.net)... 204.68.111.105
正在連接 prdownloads.sourceforge.net (prdownloads.sourceforge.net)|204.68.111.105|:80... 連上了。
已送出 HTTP 要求,正在等候回應... 301 Moved Permanently
位置: http://downloads.sourceforge.net/project/ta-lib/ta-lib/0.4.0/ta-lib-0.4.0-src.tar.gz [跟隨至新的 URL]
--2022-09-01 10:44:33-- http://downloads.sourceforge.net/project/ta-lib/ta-lib/0.4.0/ta-lib-0.4.0-src.tar.gz
正在查找主機 downloads.sourceforge.net (downloads.sourceforge.net)... 204.68.111.105
繼續使用和 prdownloads.sourceforge.net:80 的連線。
已送出 HTTP 要求,正在等候回應... 302 Found
位置: http://nchc.dl.sourceforge.net/project/ta-lib/ta-lib/0.4.0/ta-lib-0.4.0-src.tar.gz [跟隨至新的 URL]
--2022-09-01 10:44:33-- http://nchc.dl.sourceforge.net/project/ta-lib/ta-lib/0.4.0/ta-lib-0.4.0-src.tar.gz
正在查找主機 nchc.dl.sourceforge.net (nchc.dl.sourceforge.net)... 140.110.96.69, 2001:e10:ffff:1f02::17
正在連接 nchc.dl.sourceforge.net (nchc.dl.sourceforge.net)|140.110.96.69|:80... 連上了。
已送出 HTTP 要求,正在等候回應... 200 OK
長度: 1330299 (1.3M) [application/x-gzip]
儲存到:`ta-lib-0.4.0-src.tar.gz'
ta-lib-0.4.0-src.ta 100%[===================>] 1.27M 1.60MB/s 於 0.8s
2022-09-01 10:44:34 (1.60 MB/s) - 已儲存 `ta-lib-0.4.0-src.tar.gz' [1330299/1330299]
(2). 用 tar 解壓縮 :
pi@raspberrypi:~ $ tar xvf ta-lib-0.4.0-src.tar.gz
ta-lib/
ta-lib/config.sub
ta-lib/aclocal.m4
ta-lib/CHANGELOG.TXT
ta-lib/include/
.... (略) .....
ta-lib/src/tools/ta_regtest/ta_test_priv.h
ta-lib/src/tools/ta_regtest/ReadMe.txt
ta-lib/src/tools/ta_regtest/ta_gDataLow.c
ta-lib/src/tools/Makefile.in
ta-lib/src/tools/Makefile.am
ta-lib/src/tools/gen_code/
ta-lib/src/tools/gen_code/java/
ta-lib/src/tools/gen_code/java/PrettyCode.java
ta-lib/src/tools/gen_code/java/Main.java
ta-lib/src/tools/gen_code/gen_code.c
ta-lib/src/tools/gen_code/Makefile.in
ta-lib/src/tools/gen_code/Makefile.am
ta-lib/src/tools/gen_code/mcpp.exe
(3). 設定程式存放路徑 :
由上可知這些原始碼是解壓縮在 ta-lib 子目錄下, 先用 cd 指令切換到此子目錄, 然後用 configure 指令設定程式的存放路徑 :
pi@raspberrypi:~ $ cd ta-lib
pi@raspberrypi:~/ta-lib $ ./configure --prefix=/usr
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output file name... a.out
.... (略) .....
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating src/ta_abstract/Makefile
config.status: creating src/ta_common/Makefile
config.status: creating src/ta_func/Makefile
config.status: creating src/tools/Makefile
config.status: creating src/tools/gen_code/Makefile
config.status: creating src/tools/ta_regtest/Makefile
config.status: creating ta-lib-config
config.status: creating ta-lib.spec
config.status: creating ta-lib.dpkg
config.status: creating include/ta_config.h
config.status: executing depfiles commands
參考 :
(4). 用 make 指令編譯原始碼 :
pi@raspberrypi:~/ta-lib $ sudo make
Making all in src
make[1]: Entering directory '/home/pi/ta-lib/src'
Making all in ta_abstract
make[2]: Entering directory '/home/pi/ta-lib/src/ta_abstract'
make[2]: Nothing to be done for 'all'.
make[2]: Leaving directory '/home/pi/ta-lib/src/ta_abstract'
Making all in ta_common
make[2]: Entering directory '/home/pi/ta-lib/src/ta_common'
make[2]: Nothing to be done for 'all'.
make[2]: Leaving directory '/home/pi/ta-lib/src/ta_common'
Making all in ta_func
make[2]: Entering directory '/home/pi/ta-lib/src/ta_func'
/bin/bash ../../libtool --tag=CC --mode=link gcc -g -O2 -version-info 0:0:0 -o libta_func.la
.... (略) .....
creating ta_regtest
make[2]: Leaving directory '/home/pi/ta-lib/src/tools/ta_regtest'
make[2]: Entering directory '/home/pi/ta-lib/src/tools'
make[2]: Nothing to be done for 'all-am'.
make[2]: Leaving directory '/home/pi/ta-lib/src/tools'
make[1]: Leaving directory '/home/pi/ta-lib/src/tools'
make[1]: Entering directory '/home/pi/ta-lib'
make[1]: Nothing to be done for 'all-am'.
make[1]: Leaving directory '/home/pi/ta-lib'
(5). 用 make install 安裝程式 :
pi@raspberrypi:~/ta-lib $ sudo make install
Making install in src
make[1]: Entering directory '/home/pi/ta-lib/src'
Making install in ta_abstract
make[2]: Entering directory '/home/pi/ta-lib/src/ta_abstract'
make[3]: Entering directory '/home/pi/ta-lib/src/ta_abstract'
make[3]: Nothing to be done for 'install-exec-am'.
.... (略) .....
make[3]: Leaving directory '/home/pi/ta-lib/src/tools'
make[2]: Leaving directory '/home/pi/ta-lib/src/tools'
make[1]: Leaving directory '/home/pi/ta-lib/src/tools'
make[1]: Entering directory '/home/pi/ta-lib'
make[2]: Entering directory '/home/pi/ta-lib'
test -z "/usr/bin" || /usr/bin/mkdir -p "/usr/bin"
/usr/bin/install -c 'ta-lib-config' '/usr/bin/ta-lib-config'
make[2]: Nothing to be done for 'install-data-am'.
make[2]: Leaving directory '/home/pi/ta-lib'
make[1]: Leaving directory '/home/pi/ta-lib'
這樣就完成 Ta-Lib 0.4.0 版的安裝了.
(6). 用 pip3 install Ta-Lib 升至最新版 :
這時再用 sudo pip3 install Ta-Lin 指令安裝就可以順利安裝最新版的 0.4.24 版, 不會出現錯誤 :
pi@raspberrypi:~/ta-lib $ sudo pip3 install TA-Lib
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting TA-Lib
Using cached https://files.pythonhosted.org/packages/78/f5/59f1d7d0c43d3d96e93249ae9d5415b42591362bfcf2f4e50574ad063312/TA-Lib-0.4.24.tar.gz
Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (from TA-Lib) (1.16.2)
Building wheels for collected packages: TA-Lib
Running setup.py bdist_wheel for TA-Lib ... done
Stored in directory: /root/.cache/pip/wheels/14/b3/21/c411da8eb6ed49519862798a2b5cc01add3c1bf7c026ebb005
Successfully built TA-Lib
Installing collected packages: TA-Lib
Successfully installed TA-Lib-0.4.24
以上安裝過程之完整記錄參考 :
用 pip3 show Ta-Lib 指令確認目前確實是 0.4.24 版 :
pi@raspberrypi:~/ta-lib $ cd ..
pi@raspberrypi:~ $pi@raspberrypi:~ $ pip3 show Ta-Lib
Name: TA-Lib
Version: 0.4.24
Summary: Python wrapper for TA-Lib
Home-page: http://github.com/mrjbq7/ta-lib
Author: John Benediktsson
Author-email: mrjbq7@gmail.com
License: BSD
Location: /usr/local/lib/python3.7/dist-packages
Requires: numpy
Required-by:
Bingo! 大功告成啦!
以上先編譯 0.4.0 版原始碼安裝舊版 Ta-Lib, 然後再用 pip3 install Ta-Lib 安裝最新版程序能夠成功的原因, 我猜應該是編譯安裝舊版時已經處理掉原始碼中部份 C 程式的編譯問題 (新版 0.4.24 也會用到相同的程式), 所以用 pip3 install 從 PyPi 安裝新版時就不再有原始碼編譯失敗問題了.
沒有留言 :
張貼留言