2023年2月22日 星期三

Mapleboard MP510-50 測試 (十五) : 編譯與安裝 TA-Lib 套件

安裝 Python 技術分析套件 Ta-Lib 的程序比較特殊, 不是直接用 pip install 就能安裝成功, 在 Windows 系統上必須依據 Python 版本下載編譯過的 whl 檔用 pip install 安裝, 參考 :


在 Linux 系統則必須先下載舊版 (v0.4.0) 原始碼用 make 進行編譯后, 再用 pip install 安裝最新版的 Ta-Lib (v4.5.0), 作法參考在樹莓派上的安裝方法 : 


Ta-Lib v0.4.0 的下載網址有如下兩個 :


在 Mapleboard 的 Ubuntu 下編譯原始碼與在樹莓派上有一點不同, 在執行 configure 時必須指定 build 參數, 否則會出現無法識別作業系統之錯誤, 完整的編譯安裝指令如下 :

$ wget https://sourceforge.net/projects/ta-lib/files/ta-lib/0.4.0/ta-lib-0.4.0-src.tar.gz
$ tar -xvf ta-lib-0.4.0-src.tar.gz 
$ cd ta-lib   
$ ./configure --prefix=/usr --build=aarch64-unknown-linux-gnu   
$ sudo make    
$ sudo make install   
$ sudo pip3 install ta-lib     (這會升級到最新版)

本系列全部測試文章參考 :


安裝過程記錄如下 : 


1. 下載 Ta-lib v0.4.0 版原始碼 : 

one@LX2438:~$ wget https://sourceforge.net/projects/ta-lib/files/ta-lib/0.4.0/ta-lib-0.4.0-src.tar.gz
--2023-02-21 23:22:20--  https://sourceforge.net/projects/ta-lib/files/ta-lib/0.4.0/ta-lib-0.4.0-src.tar.gz
正在查找主機 sourceforge.net (sourceforge.net)... 2606:4700::6812:b80, 2606:4700::6812:a80, 104.18.10.128, ...
正在連接 sourceforge.net (sourceforge.net)|2606:4700::6812:b80|:443... 連上了。
已送出 HTTP 要求,正在等候回應... 301 Moved Permanently
位置:https://sourceforge.net/projects/ta-lib/files/ta-lib/0.4.0/ta-lib-0.4.0-src.tar.gz/ [跟隨至新的 URL]
--2023-02-21 23:22:21--  https://sourceforge.net/projects/ta-lib/files/ta-lib/0.4.0/ta-lib-0.4.0-src.tar.gz/
繼續使用對 [sourceforge.net]:443 的已存在連線。
已送出 HTTP 要求,正在等候回應... 301 Moved Permanently
位置:https://sourceforge.net/projects/ta-lib/files/ta-lib/0.4.0/ta-lib-0.4.0-src.tar.gz/download [跟隨至新的 URL]
--2023-02-21 23:22:21--  https://sourceforge.net/projects/ta-lib/files/ta-lib/0.4.0/ta-lib-0.4.0-src.tar.gz/download
繼續使用對 [sourceforge.net]:443 的已存在連線。
已送出 HTTP 要求,正在等候回應... 302 Found
位置:https://downloads.sourceforge.net/project/ta-lib/ta-lib/0.4.0/ta-lib-0.4.0-src.tar.gz?ts=gAAAAABj9OGtiJFHrIYZPdNfFZXpYzkjNna20gjFx4zIIo9CSWrsLmooS0B0pg84kRoPK1eOFMo8jm-lcdMG8UVOBaO8QyHB-g%3D%3D&use_mirror=nchc&r= [跟隨至新的 URL]
--2023-02-21 23:22:21--  https://downloads.sourceforge.net/project/ta-lib/ta-lib/0.4.0/ta-lib-0.4.0-src.tar.gz?ts=gAAAAABj9OGtiJFHrIYZPdNfFZXpYzkjNna20gjFx4zIIo9CSWrsLmooS0B0pg84kRoPK1eOFMo8jm-lcdMG8UVOBaO8QyHB-g%3D%3D&use_mirror=nchc&r=
正在查找主機 downloads.sourceforge.net (downloads.sourceforge.net)... 204.68.111.105
正在連接 downloads.sourceforge.net (downloads.sourceforge.net)|204.68.111.105|:443... 連上了。
已送出 HTTP 要求,正在等候回應... 302 Found
位置:https://nchc.dl.sourceforge.net/project/ta-lib/ta-lib/0.4.0/ta-lib-0.4.0-src.tar.gz [跟隨至新的 URL]
--2023-02-21 23:22:22--  https://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)... 2001:e10:ffff:1f02::17, 140.110.96.69
正在連接 nchc.dl.sourceforge.net (nchc.dl.sourceforge.net)|2001:e10:ffff:1f02::17|:443... 連上了。
已送出 HTTP 要求,正在等候回應... 200 OK
長度: 1330299 (1.3M) [application/x-gzip]
儲存到:‘ta-lib-0.4.0-src.tar.gz’

ta-lib-0.4.0-src.tar.gz               100%[=======================================================================>]   1.27M  --.-KB/s    於 0.1s    

2023-02-21 23:22:22 (9.28 MB/s) - 已儲存 ‘ta-lib-0.4.0-src.tar.gz’ [1330299/1330299]


2. 解壓縮 Ta-lib v0.4.0 版原始碼 : 

one@LX2438:~$ 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/include/ta_abstract.h
ta-lib/include/ta_func.h
ta-lib/include/ta_common.h
ta-lib/include/ta_config.h.in
ta-lib/include/Makefile.am
ta-lib/include/ta_libc.h
ta-lib/include/ta_defs.h
ta-lib/missing
ta-lib/ta-lib.spec.in
ta-lib/config.guess
ta-lib/Makefile.in
ta-lib/ta-lib.dpkg.in
ta-lib/Makefile.am
..... (略) .....
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

可見解壓縮后的原始碼都放在 ta-lib 子目錄下. 


3. 用 configure 設定存放路徑 : 

先切換到解壓縮后的原始碼子目錄 ta-lib, 再用 configure 指令設定, 先不帶 build 參數 : 

one@LX2438:~$ cd ta-lib   
one@LX2438:~/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
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables... 
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking build system type... ./config.guess: unable to guess system type

This script, last modified 2006-07-02, has failed to recognize
the operating system you are using. It is advised that you
download the most up to date version of the config scripts from

  http://savannah.gnu.org/cgi-bin/viewcvs/*checkout*/config/config/config.guess
and
  http://savannah.gnu.org/cgi-bin/viewcvs/*checkout*/config/config/config.sub

If the version you run (./config.guess) is already up to date, please
send the following data and any information you think might be
pertinent to <config-patches@gnu.org> in order to provide the needed
information to handle your system.

config.guess timestamp = 2006-07-02

uname -m = aarch64
uname -r = 5.15.74-mp510+
uname -s = Linux
uname -v = #1 SMP PREEMPT Tue Oct 18 09:02:35 CST 2022

/usr/bin/uname -p = aarch64
/bin/uname -X     = 

hostinfo               = 
/bin/universe          = 
/usr/bin/arch -k       = 
/bin/arch              = aarch64
/usr/bin/oslevel       = 
/usr/convex/getsysinfo = 

UNAME_MACHINE = aarch64
UNAME_RELEASE = 5.15.74-mp510+
UNAME_SYSTEM  = Linux
UNAME_VERSION = #1 SMP PREEMPT Tue Oct 18 09:02:35 CST 2022
configure: error: cannot guess build type; you must specify one    

可見與樹莓派環境下編譯不同, 不帶 build 參數無法完成設定, configure 程式碼死在 checking build system type 這階段, 它無法識別出作業系統是哪一種, 一定要透過 build 參數明確指定才行. 

我在下面這篇文章找到 build 參數要帶 'aarch64-unknown-linux-gnu' :


帶上此 build 參數重新設定就能順利完成了 : 

one@LX2438:~/ta-lib$ ./configure --prefix=/usr --build=aarch64-unknown-linux-gnu    
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
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables... 
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking build system type... aarch64-unknown-linux-gnu
checking host system type... aarch64-unknown-linux-gnu
checking for a sed that does not truncate output... /usr/bin/sed
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
..... (略) ......
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

可見明確指定 build system type 后就順利完成設定了. 


4. 用 make 編譯原始碼 :

one@LX2438:~/ta-lib$ sudo make    
[sudo] one 的密碼: 
..... (略) ......
gcc -g -O2 -o .libs/ta_regtest ta_regtest-ta_regtest.o ta_regtest-test_data.o ta_regtest-test_util.o ta_regtest-test_abstract.o ta_regtest-test_adx.o ta_regtest-test_mom.o ta_regtest-test_sar.o ta_regtest-test_rsi.o ta_regtest-test_candlestick.o ta_regtest-test_per_ema.o ta_regtest-test_per_hlc.o ta_regtest-test_stoch.o ta_regtest-test_macd.o ta_regtest-test_minmax.o ta_regtest-test_per_hlcv.o ta_regtest-test_1in_1out.o ta_regtest-test_1in_2out.o ta_regtest-test_per_ohlc.o ta_regtest-test_stddev.o ta_regtest-test_bbands.o ta_regtest-test_ma.o ta_regtest-test_po.o ta_regtest-test_per_hl.o ta_regtest-test_trange.o ta_regtest-test_internals.o  -L/home/one/ta-lib/src /home/one/ta-lib/src/.libs/libta_lib.so -lm -lpthread -ldl
creating ta_regtest
make[2]: 離開目錄「/home/one/ta-lib/src/tools/ta_regtest」
make[2]: 進入目錄「/home/one/ta-lib/src/tools」
make[2]: 對「all-am」無需做任何事。
make[2]: 離開目錄「/home/one/ta-lib/src/tools」
make[1]: 離開目錄「/home/one/ta-lib/src/tools」
make[1]: 進入目錄「/home/one/ta-lib」
make[1]: 對「all-am」無需做任何事。
make[1]: 離開目錄「/home/one/ta-lib」

編譯過程會吐出非常多看不懂的資訊, 只要沒出現 error 即可. 


5. 用 make install 安裝編譯好的程式 :

one@LX2438:~/ta-lib$ sudo make install  
[sudo] one 的密碼: 
Making install in src
make[1]: 進入目錄「/home/one/ta-lib/src」
Making install in ta_abstract
make[2]: 進入目錄「/home/one/ta-lib/src/ta_abstract」
make[3]: 進入目錄「/home/one/ta-lib/src/ta_abstract」
make[3]: 對「install-exec-am」無需做任何事。
test -z "/usr/include/ta-lib/" || /usr/bin/mkdir -p "/usr/include/ta-lib/"
 /usr/bin/install -c -m 644 '../../include/ta_defs.h' '/usr/include/ta-lib//ta_defs.h'
 /usr/bin/install -c -m 644 '../../include/ta_libc.h' '/usr/include/ta-lib//ta_libc.h'
 /usr/bin/install -c -m 644 '../../include/ta_abstract.h' '/usr/include/ta-lib//ta_abstract.h'
..... (略) ......
make[3]: 離開目錄「/home/one/ta-lib/src/tools」
make[2]: 離開目錄「/home/one/ta-lib/src/tools」
make[1]: 離開目錄「/home/one/ta-lib/src/tools」
make[1]: 進入目錄「/home/one/ta-lib」
make[2]: 進入目錄「/home/one/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]: 對「install-data-am」無需做任何事。
make[2]: 離開目錄「/home/one/ta-lib」
make[1]: 離開目錄「/home/one/ta-lib」

安裝過程無 error 即可. 


6. 用 pip install Ta-Lib 升級到最新版 :

編譯安裝完舊版 Ta-Lib 就可以跳離並刪除 ta-lib 子目錄了 :

one@LX2438:~/ta-lib$ cd ..   
one@LX2438:~$ sudo pip install Ta-Lib    
Collecting Ta-Lib
  Downloading TA-Lib-0.4.25.tar.gz (271 kB)
 272.0/272.0 KB 1.3 MB/s eta 0:00:00
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Installing backend dependencies ... done
  Preparing metadata (pyproject.toml) ... done
Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (from Ta-Lib) (1.21.5)
Building wheels for collected packages: Ta-Lib
  Building wheel for Ta-Lib (pyproject.toml) ... done
  Created wheel for Ta-Lib: filename=TA_Lib-0.4.25-cp310-cp310-linux_aarch64.whl size=1518950 sha256=c6a2be28b115fd4fce7e21ee625e3056fa658658ae8cdbb201fd0627a2f72ce5
  Stored in directory: /root/.cache/pip/wheels/bd/00/b7/86021ffb1e63d17df0a25e7e5e0ff2a1f94f0a7eb953366eff
Successfully built Ta-Lib
Installing collected packages: Ta-Lib
Successfully installed Ta-Lib-0.4.25
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

用 pip show Ta-Lib 檢視可知已升到最新版 0.4.25 :

one@LX2438:~$ pip show Ta-Lib   
Name: TA-Lib
Version: 0.4.25
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.10/dist-packages
Requires: numpy
Required-by: 

最後用一個簡單的計算五日移動平均值來檢驗一下 Ta-Lib 是否可用 :

Python 3.10.6 (/usr/bin/python3)
>>> import talib   
>>> import numpy as np   
>>> close=np.random.random(30)     
>>> print(talib.SMA(close, 5))       
[       nan        nan        nan        nan 0.69136807 0.72448027
 0.62425612 0.68713407 0.68578759 0.60408337 0.67693753 0.71258124
 0.71956245 0.72228273 0.66785218 0.52727606 0.65132248 0.5209605
 0.47285977 0.4846474  0.52345147 0.43278205 0.48792562 0.44271223
 0.39446687 0.36684241 0.35803496 0.26485917 0.2578122  0.36312063]

收工. 

參考 :


沒有留言:

張貼留言