2018年5月12日 星期六

Python Fintech 學習筆記 : 安裝技術指標套件 TA-Lib

今天在明儀買了一本強國人的 Fintech 著作 "AI+大數據:用 Python 玩轉金融遊戲的量化交易", 裡面有一章介紹 TA-Lib 模組, 好像在母校高科大林萍珍老師寫的 Fintech 二部曲之 "Python 網頁程式交易 APP 實作" 一書中有介紹, 回來後翻閱果然第 15章就是在講 TA-Lib, 看完後馬上測試看看此模組之功能.

書上說安裝 TA-Lib 前須先安裝 C++ 編譯環境, 但我想試試看直接用 pip3 是否能搞定, 結果真的不行, 會出現 "cannot fine ta-lib library" 錯誤 :

C:\Users\Tony>pip3 install ta-lib 
Collecting ta-lib
  Downloading https://files.pythonhosted.org/packages/90/05/d4c6a778d7a7de0be366bc4a850b4ffaeac2abad927f95fa8ba6f355a082/TA-Lib-0.4.17.tar.gz (717kB)
Requirement already satisfied: numpy in c:\python36\lib\site-packages (from ta-lib) (1.14.1)
Building wheels for collected packages: ta-lib
  Running setup.py bdist_wheel for ta-lib ... error
  Complete output from command c:\python36\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\Tony\\AppData\\Local\\Temp\\pip-install-xx2ycqo8\\ta-lib\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d C:\Users\Tony\AppData\Local\Temp\pip-wheel-y1htdjyy --python-tag cp36:
  C:\Users\Tony\AppData\Local\Temp\pip-install-xx2ycqo8\ta-lib\setup.py:79: UserWarning: Cannot find ta-lib library, installation may fail.
    warnings.warn('Cannot find ta-lib library, installation may fail.') 
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build\lib.win-amd64-3.6
  creating build\lib.win-amd64-3.6\talib
  copying talib\abstract.py -> build\lib.win-amd64-3.6\talib
  copying talib\deprecated.py -> build\lib.win-amd64-3.6\talib
  copying talib\stream.py -> build\lib.win-amd64-3.6\talib
  copying talib\test_abstract.py -> build\lib.win-amd64-3.6\talib
  copying talib\test_data.py -> build\lib.win-amd64-3.6\talib
  copying talib\test_func.py -> build\lib.win-amd64-3.6\talib
  copying talib\test_pandas.py -> build\lib.win-amd64-3.6\talib
  copying talib\test_stream.py -> build\lib.win-amd64-3.6\talib
  copying talib\__init__.py -> build\lib.win-amd64-3.6\talib
  running build_ext
  building 'talib._ta_lib' extension
  error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

  ----------------------------------------
  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 c:\python36\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\Tony\\AppData\\Local\\Temp\\pip-install-xx2ycqo8\\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 C:\Users\Tony\AppData\Local\Temp\pip-record-f76bv6c8\install-record.txt --single-version-externally-managed --compile:
    C:\Users\Tony\AppData\Local\Temp\pip-install-xx2ycqo8\ta-lib\setup.py:79: UserWarning: Cannot find ta-lib library, installation may fail.
      warnings.warn('Cannot find ta-lib library, installation may fail.')
    running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-3.6
    creating build\lib.win-amd64-3.6\talib
    copying talib\abstract.py -> build\lib.win-amd64-3.6\talib
    copying talib\deprecated.py -> build\lib.win-amd64-3.6\talib
    copying talib\stream.py -> build\lib.win-amd64-3.6\talib
    copying talib\test_abstract.py -> build\lib.win-amd64-3.6\talib
    copying talib\test_data.py -> build\lib.win-amd64-3.6\talib
    copying talib\test_func.py -> build\lib.win-amd64-3.6\talib
    copying talib\test_pandas.py -> build\lib.win-amd64-3.6\talib
    copying talib\test_stream.py -> build\lib.win-amd64-3.6\talib
    copying talib\__init__.py -> build\lib.win-amd64-3.6\talib
    running build_ext
    building 'talib._ta_lib' extension
    error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

    ----------------------------------------
Command "c:\python36\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\Tony\\AppData\\Local\\Temp\\pip-install-xx2ycqo8\\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 C:\Users\Tony\AppData\Local\Temp\pip-record-f76bv6c8\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\Tony\AppData\Local\Temp\pip-install-xx2ycqo8\ta-lib\

安裝過程顯示有 Error, 沒有安裝成功, 進 IDLE 測試 import talib 顯示 No module named 'talib' :

C:\Users\Tony>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import talib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'talib' 

從 pip 吐出的訊息中可知確實需先安裝 C++ 14.0 編譯環境, 因為 TA-Lib 是用 C++ 撰寫的, 經二次包裝成 Python 模組. 根據下面這篇文章, VC++ 14.0 編譯環境需安裝 Visual Studio 2015 :

Microsoft Visual C++ 14.0 is required (Unable to find vcvarsall.bat)

但是 Visual Studio 2015 是個龐然大物的 C++ 開發工具, 為了一個小小的 Python 模組卻要安裝這麼大且用不到的 IDE 實在是用牛刀殺雞. 事實上該篇文章底下有人回覆說直接去下載 TA-Lib 的 whl 檔, 再用 pip3 來安裝即可, whl 檔是一種 zip 壓縮包, 裡面包含了已編譯過的 pyd 檔, 因此不需要 C++ 編譯環境了 :

Failed to install ta-lib #127

在加州大學爾灣分校網站可下載 TA-Lib 的 whl 檔 :

https://www.lfd.uci.edu/~gohlke/pythonlibs/#ta-lib

不過下載前要先看看電腦裡面安裝的 Python 是哪一版本, 我的是 Win10 (64-bit) Python 3.6 版, 所以是下載 TA_Lib-0.4.17-cp36-cp36m-win_amd64.whl 檔 :




然後用 pip3 install 指令安裝 whl 檔, 三兩下就完成了 :

D:\Python\test>pip3 install TA_Lib-0.4.17-cp36-cp36m-win_amd64.whl 
Processing d:\python\test\ta_lib-0.4.17-cp36-cp36m-win_amd64.whl
Installing collected packages: TA-Lib
Successfully installed TA-Lib-0.4.17

使用 TA-Lib 需先用 import talib 匯入模組, 另外數學計算通常也需要用到 Numpy, 所以 numpy 模組也要匯入 :

import talib
import numpy as np 

TA-Lib 說明文件參考 :

https://mrjbq7.github.io/ta-lib/doc_index.html
https://www.ta-lib.org/function.html

首先用 TA-Lib 模組的 SMA() 方法來計算五日移動平均值, 對象是以 100 個隨機數組成之串列來模擬收盤價格, 例如 :

>>> import talib                                    #匯入 TA-Lib 模組
>>> import numpy as np                      #匯入 Numpy 模組
>>> close=np.random.random(100)    #產生 100 個隨機數
>>> print(talib.SMA(close, 5))             #計算五日移動平均值
[       nan        nan        nan        nan 0.41437592 0.3034201
 0.34724216 0.34857386 0.4713277  0.39766768 0.55105383 0.47972592
 0.57626587 0.60916467 0.53667767 0.45744977 0.63673404 0.61160771
 0.50445564 0.58839037 0.60293051 0.41724317 0.37082079 0.40462401
 0.39324222 0.35427605 0.41921397 0.37615645 0.38082908 0.36077551
 0.33780034 0.31641491 0.40325122 0.39969208 0.34394382 0.31827174
 0.27691481 0.29127448 0.33601023 0.44966687 0.4718034  0.52014788
 0.40558883 0.42722963 0.4806484  0.46705996 0.59169057 0.78396661
 0.70125913 0.68401168 0.74957012 0.65054416 0.51079224 0.54560696
 0.53737591 0.63300363 0.68375619 0.67292258 0.72767829 0.702373
 0.58208199 0.60504462 0.61336209 0.57265528 0.63112225 0.63813339
 0.57806177 0.60135303 0.51023397 0.49479377 0.4628476  0.39711095
 0.35182176 0.32921153 0.33890121 0.4555945  0.41823565 0.54527423
 0.62800393 0.61685837 0.65603855 0.85066106 0.85732753 0.89697342
 0.7758187  0.59437911 0.54854651 0.49072806 0.33670597 0.360909
 0.3587656  0.33070327 0.3948016  0.3910122  0.36450729 0.45347369
 0.42672516 0.25851077 0.42287915 0.4265908 ]


參考 :

Python wrapper for TA-Lib (http://ta-lib.org/)
https://www.lfd.uci.edu/~gohlke/pythonlibs/#ta-lib


2018-05-13 補充 :

今天試著在樹莓派 Pi 3 上安裝 TA-Lib 卻無法成功, 吐出一堆 Warning 後就停住了 :

pi@raspberrypi:~/ta-lib $ pip3 install TA-Lib
Downloading/unpacking TA-Lib
  Downloading TA-Lib-0.4.17.tar.gz (717kB): 717kB downloaded
  Running setup.py (path:/tmp/pip-build-b_3jbzt4/TA-Lib/setup.py) egg_info for package TA-Lib

Requirement already satisfied (use --upgrade to upgrade): numpy in /usr/local/lib/python3.4/dist-packages (from TA-Lib)
Installing collected packages: TA-Lib
  Running setup.py install for TA-Lib
    building 'talib._ta_lib' extension
    arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC -I/usr/local/lib/python3.4/dist-packages/numpy/core/include -I/usr/include -I/usr/local/include -I/opt/include -I/opt/local/include -I/home/pi/.linuxbrew/Cellar/ta-lib/0.4.0/include -I/usr/include/python3.4m -c talib/_ta_lib.c -o build/temp.linux-armv7l-3.4/talib/_ta_lib.o
    In file included from /usr/local/lib/python3.4/dist-packages/numpy/core/include/numpy/ndarraytypes.h:1816:0,
                     from /usr/local/lib/python3.4/dist-packages/numpy/core/include/numpy/ndarrayobject.h:18,
                     from /usr/local/lib/python3.4/dist-packages/numpy/core/include/numpy/arrayobject.h:4,
                     from talib/_ta_lib.c:533:
    /usr/local/lib/python3.4/dist-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
     #warning "Using deprecated NumPy API, disable it by " \
      ^
    talib/_ta_lib.c: In function ‘__pyx_pf_5talib_7_ta_lib_14ACOS’:
    talib/_ta_lib.c:8328:15: warning: assignment from incompatible pointer type
         __pyx_t_2 = PyArray_GETCONTIGUOUS(__pyx_v_real); if (unlikely(!__pyx_t_2)) __PYX_ERR(2, 48, __pyx_L1_error)
                   ^
    talib/_ta_lib.c: In function ‘__pyx_pf_5talib_7_ta_lib_16AD’:
    talib/_ta_lib.c:8784:15: warning: assignment from incompatible pointer type
         __pyx_t_2 = PyArray_GETCONTIGUOUS(__pyx_v_high); if (unlikely(!__pyx_t_2)) __PYX_ERR(2, 100, __pyx_L1_error)
                   ^
    talib/_ta_lib.c:8889:15: warning: assignment from incompatible pointer type
         __pyx_t_2 = PyArray_GETCONTIGUOUS(__pyx_v_low); if (unlikely(!__pyx_t_2)) __PYX_ERR(2, 107, __pyx_L1_error)
                   ^
...... (略)

arm-linux-gnueabihf-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 build/temp.linux-armv7l-3.4/talib/_ta_lib.o -L/usr/local/lib -L/usr/local/lib64 -L/opt/lib -L/opt/local/lib -L/home/pi/.linuxbrew/Cellar/ta-lib/0.4.0/lib -Wl,--enable-new-dtags,-R/home/pi/.linuxbrew/Cellar/ta-lib/0.4.0/lib -lta_lib -o build/lib.linux-armv7l-3.4/talib/_ta_lib.cpython-34m.so

running install_lib

creating /usr/local/lib/python3.4/dist-packages/talib

error: could not create '/usr/local/lib/python3.4/dist-packages/talib': Permission denied

----------------------------------------
Cleaning up...
Command /usr/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip-build-yib_to9o/TA-Lib/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-odlvshqy-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /tmp/pip-build-yib_to9o/TA-Lib
Storing debug log for failure in /home/pi/.pip/pip.log


我參考了下面幾篇指引, 說是要下載 TA-Lib 原始檔去編譯才行 :

Installing ta-lib c library on Ubuntu #47
HOW TO INSTALL TA-LIB ON UBUNTU SERVER
Ta-lib + bigdata = stock analysis tool
安装ta-lib问题记录

主要是下這五個指令去編譯原始碼 :

wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.17-src.tar.gz 
$ tar -xvf ta-lib-0.4.17-src.tar.gz 
$ cd ta-lib   
$ ./configure --prefix=/usr 
$ sudo make   
$ sudo make install   

編譯完後再下 pip3 install TA-Lib 還是跟上面一樣出現很多 Warning 後停住, 只好 Ctrl-C 停掉了. 其實 Warning 不用擔心, 該擔心的是 Error, 後來我仔細看上面的 pip3 輸出訊息, 發現似乎是這個 Permission denied 有問題 :

Error: could not create '/usr/local/lib/python3.4/dist-packages/talib': Permission denied

所以就在 pip3 前面加上 sudo 重新安裝一次, 這回就沒有 error, 只剩下一堆 Warning 了, 安裝成功 !

pi@raspberrypi:~/ta-lib $ sudo pip3 install TA-Lib 
Downloading/unpacking TA-Lib
  Downloading TA-Lib-0.4.17.tar.gz (717kB): 717kB downloaded
  Running setup.py (path:/tmp/pip-build-_xn7lcem/TA-Lib/setup.py) egg_info for package TA-Lib

Requirement already satisfied (use --upgrade to upgrade): numpy in /usr/local/lib/python3.4/dist-packages (from TA-Lib)
Installing collected packages: TA-Lib
  Running setup.py install for TA-Lib
    building 'talib._ta_lib' extension
    arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC -I/usr/local/lib/python3.4/dist-packages/numpy/core/include -I/usr/include -I/usr/local/include -I/opt/include -I/opt/local/include -I/usr/include/python3.4m -c talib/_ta_lib.c -o build/temp.linux-armv7l-3.4/talib/_ta_lib.o
    In file included from /usr/local/lib/python3.4/dist-packages/numpy/core/include/numpy/ndarraytypes.h:1816:0,
                     from /usr/local/lib/python3.4/dist-packages/numpy/core/include/numpy/ndarrayobject.h:18,
                     from /usr/local/lib/python3.4/dist-packages/numpy/core/include/numpy/arrayobject.h:4,
                     from talib/_ta_lib.c:533:
    /usr/local/lib/python3.4/dist-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
     #warning "Using deprecated NumPy API, disable it by " \
      ^
    talib/_ta_lib.c: In function ‘__pyx_pf_5talib_7_ta_lib_14ACOS’:
    talib/_ta_lib.c:8328:15: warning: assignment from incompatible pointer type
         __pyx_t_2 = PyArray_GETCONTIGUOUS(__pyx_v_real); if (unlikely(!__pyx_t_2)) __PYX_ERR(2, 48, __pyx_L1_error)

  ..... (略)

    talib/_ta_lib.c: In function ‘__pyx_pw_5talib_7_ta_lib_669stream_WMA’:
    talib/_ta_lib.c:210531:7: warning: this decimal constant is unsigned only in ISO C90
           __pyx_v_timeperiod = ((int)-2147483648);
           ^
    talib/_ta_lib.c: In function ‘__pyx_pf_5talib_7_ta_lib_668stream_WMA’:
    talib/_ta_lib.c:210649:15: warning: assignment from incompatible pointer type
         __pyx_t_2 = PyArray_GETCONTIGUOUS(__pyx_v_real); if (unlikely(!__pyx_t_2)) __PYX_ERR(3, 8055, __pyx_L1_error)
                   ^
    arm-linux-gnueabihf-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 build/temp.linux-armv7l-3.4/talib/_ta_lib.o -L/usr/local/lib -L/usr/local/lib64 -L/opt/lib -L/opt/local/lib -lta_lib -o build/lib.linux-armv7l-3.4/talib/_ta_lib.cpython-34m.so

Successfully installed TA-Lib 
Cleaning up...

因此, 在樹莓派安裝 Python 第三方模組時若停住不動, 不要以為出問題, 要耐心等它跑完. 進入 Python shell 可正常匯入talib, 可見確實已安裝成功 :

pi@raspberrypi:~/ta-lib $ python3   
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import talib 
>>> import numpy as np 
>>> close=np.random.random(100) 
>>> print(talib.SMA(close, 5)) 
[       nan        nan        nan        nan 0.65915586 0.63201339
 0.52539836 0.6088237  0.53037341 0.50350876 0.40774704 0.53895129
 0.55213804 0.53302074 0.52901657 0.65431574 0.67443004 0.6926023
 0.78975639 0.8610581  0.74491901 0.63566927 0.59443008 0.56242908
 0.54286868 0.59698122 0.62692999 0.57069521 0.50753832 0.49196352
 0.37252548 0.34447198 0.36376759 0.39748272 0.24703842 0.40411608
 0.45773534 0.53769344 0.57408801 0.61001732 0.58808103 0.65871263
 0.69320038 0.68566105 0.70834336 0.65338877 0.4894677  0.34201007
 0.23193722 0.33438444 0.34580115 0.39851912 0.5085709  0.56499867
 0.3957679  0.39825194 0.40111224 0.41781678 0.36268417 0.52854415
 0.57581859 0.60470704 0.50108283 0.5051448  0.44456283 0.32920084
 0.23593222 0.36399325 0.33006214 0.37792135 0.38536767 0.45302815
 0.45906079 0.62181004 0.49975509 0.48649765 0.58951498 0.46188726
 0.34973836 0.41101676 0.57247099 0.58510405 0.63655894 0.59529275
 0.50561807 0.39785465 0.27804495 0.28518371 0.40136344 0.45729309
 0.43371964 0.54630232 0.54471708 0.57052697 0.60589979 0.59538735
 0.46911655 0.47225964 0.34718179 0.29820296]

但是, 安裝成功純粹是沒加 sudo 還是之前編譯過原始碼呢? 這下無法分辨了. 我打算用另外一塊 Pi 3 來確認這個問題.

15 則留言 :

Unknown 提到...

感謝大大!我終於裝好了talib

小狐狸事務所 提到...

恭喜您!

半生 提到...

感謝 有用

frank@地球上沉睡 提到...

我嘗試下載加州大學爾灣分校之3.8版32位元whl檔,但都不成功,請問大大,是該網站禁止下載了,還是有其他的狀況,敬請解惑!

小狐狸事務所 提到...

Dear Frank, 沒問題喔, 我正常下載了 TA_Lib-0.4.18-cp38-cp38-win32.whl, 如果還不行的話, 我寄給你.

frank@地球上沉睡 提到...

大大您好,試了好久,還是無法下載,可否麻煩您寄給我檔案,感激不盡.

小狐狸事務所 提到...

所以 email 是 frank@gmail.com 嗎?

frank@地球上沉睡 提到...

我的email是:franklin600209@gmail.com,謝謝.

小狐狸事務所 提到...

Dear Frank, 檔案已寄出囉!

frank@地球上沉睡 提到...

感謝大大,終於可以使用了.

小狐狸事務所 提到...

無法下載會不會是防火牆在檔 (例如公司資安系統)

frank@地球上沉睡 提到...

應該不是,我是在家裡下載的,公司應該會擋,每次出現的錯誤訊息如下:
{沒有任何應用程式與此檔案相關可執行此動作,請安裝應用程式;或者,若已安裝了應用程式,則請在[預設應用程式設定]頁面建立關聯性}

小狐狸事務所 提到...

可能之前有替 whl 建立關聯程式,但後來那個程式刪除了?

frank@地球上沉睡 提到...

嗯,這個我不太確定~,但若發生這種error,是否代表我無法從爾灣分校的這個網站下載whl檔呢?還是有什麼方法可以解救呢?

小狐狸事務所 提到...

不清楚, 我對 Windows 系統不太了解. 換 FireFox 或其他瀏覽器試試.