天天看點

移植 Python 量化交易 TA-Lib 庫到函數計算

TA-Lib全稱“Technical Analysis Library”, 即技術分析庫,是 Python 金融量化的進階庫,涵蓋了 150 多種股票、期貨交易軟體中常用的技術分析名額,如 MACD、RSI、KDJ、動量名額、布林帶等等。

TA-Lib 可分為 10 個子闆塊:

Overlap Studies(重疊名額)

Momentum Indicators(動量名額)

Volume Indicators(交易量名額)

Cycle Indicators(周期名額)

Price Transform(價格變換)

Volatility Indicators(波動率名額)

Pattern Recognition(模式識别)

Statistic Functions(統計函數)

Math Transform(數學變換)

Math Operators(數學運算)

移植 Python 量化交易 TA-Lib 庫到函數計算

本文介紹通過 Funcraft 的模闆将 Python 量化交易庫 TA-lib 移植到函數計算。

依賴工具

本項目是在 MacOS 下開發的,涉及到的工具是平台無關的,對于 Linux 和 Windows 桌面系統應該也同樣适用。在開始本例之前請確定如下工具已經正确的安裝,更新到最新版本,并進行正确的配置。

Docker

Funcraft

對于 MacOS 使用者可以使用 homebrew 進行安裝:

brew cask install dockerbrew tap vangie/formulabrew install fun

安裝好後,記得先執行 fun config 初始化一下配置。

初始化

使用 fun init 指令可以快捷地将本模闆項目初始化到本地。

fun init vangie/ta-lib-example

安裝依賴

$ fun installusing template: template.ymlstart installing function dependencies without dockerbuilding ta-lib-example/ta-lib-exampleFunfile exist, Fun will use container to build forcelyStep 1/5 : FROM registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-python3.6:build-1.7.7 ---> 373f5819463bStep 2/5 : COPY ta-lib-0.4.0-src.tar.gz /tmp ---> Using cache ---> 64f9f85112b4Step 3/5 : RUN cd /tmp; tar -xzf ta-lib-0.4.0-src.tar.gz ---> Using cache ---> 9f2d3f836de9Step 4/5 : RUN cd /tmp/ta-lib/ ; ./configure --prefix=/code/.fun/root/usr ; make ; make install ---> Using cache ---> 7725836973d4Step 5/5 : RUN TA_LIBRARY_PATH=/code/.fun/root/usr/lib TA_INCLUDE_PATH=/code/.fun/root/usr/include fun-install pip install TA-Lib ---> Using cache ---> a338e71895b7sha256:a338e71895b74a0be98278f35da38c48545f04a54e19ec9e689bab976265350bSuccessfully built a338e71895b7Successfully tagged fun-cache-d4ac1d89-5b75-4429-933a-2260e2f7fbec:latestcopying function artifact to /Users/vangie/Workspace/ta-lib-example/{{ projectName }}Install SuccessTips for next step====================== Invoke Event Function: fun local invoke Invoke Http Function: fun local start Build Http Function: fun build Deploy Resources: fun deploy

本地調用

$ fun local invokeusing template: template.ymlMissing invokeName argument, Fun will use the first function ta-lib-example/ta-lib-example as invokeNameskip pulling image aliyunfc/runtime-python3.6:1.7.7...FunctionCompute python3 runtime inited.FC Invoke Start RequestId: dc1495b2-13ec-4ecf-a2dc-a0026d82651aFC Invoke End RequestId: dc1495b2-13ec-4ecf-a2dc-a0026d82651a[ "HT_DCPERIOD", "HT_DCPHASE", "HT_PHASOR", "HT_SINE", "HT_TRENDMODE"]RequestId: dc1495b2-13ec-4ecf-a2dc-a0026d82651a Billed Duration: 350 ms Memory Size: 1998 MB Max Memory Used: 34 MB

部署

$ fun deployusing template: template.ymlusing region: cn-shanghaiusing accountId: 4733using accessKeyId: EUz3using timeout: 600Waiting for service ta-lib-example to be deployed... Waiting for function ta-lib-example to be deployed... Waiting for packaging function ta-lib-example code... The function ta-lib-example has been packaged. A total of 39 files files were compressed and the final size was 3.23 MB function ta-lib-example deploy successservice ta-lib-example deploy success

執行

$ fun invokeusing template: template.ymlMissing invokeName argument, Fun will use the first function ta-lib-example/ta-lib-example as invokeName========= FC invoke Logs begin =========FC Invoke Start RequestId: 83e23eba-02b4-4380-bbca-daec6856bf4aFC Invoke End RequestId: 83e23eba-02b4-4380-bbca-daec6856bf4aDuration: 213.86 ms, Billed Duration: 300 ms, Memory Size: 128 MB, Max Memory Used: 43.50 MB========= FC invoke Logs end =========FC Invoke Result:[ "HT_DCPERIOD", "HT_DCPHASE", "HT_PHASOR", "HT_SINE", "HT_TRENDMODE"