移植 Python 量化交易 TA-Lib 库到函数计算
admin
2023-03-27 01:01:36
0次

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 docker
brew tap vangie/formula
brew install fun

Windows 和 Linux 用户安装请参考:

https://github.com/aliyun/fun/blob/master/docs/usage/installation.md

安装好后,记得先执行 fun config 初始化一下配置。

初始化

使用 fun init 命令可以快捷地将本模板项目初始化到本地。

fun init vangie/ta-lib-example

安装依赖

$ fun install
using template: template.yml
start installing function dependencies without docker

building ta-lib-example/ta-lib-example
Funfile exist, Fun will use container to build forcely
Step 1/5 : FROM registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-python3.6:build-1.7.7
 ---> 373f5819463b
Step 2/5 : COPY ta-lib-0.4.0-src.tar.gz /tmp
 ---> Using cache
 ---> 64f9f85112b4
Step 3/5 : RUN cd /tmp; tar -xzf ta-lib-0.4.0-src.tar.gz
 ---> Using cache
 ---> 9f2d3f836de9
Step 4/5 : RUN cd /tmp/ta-lib/ ;     ./configure --prefix=/code/.fun/root/usr ;     make ; make install
 ---> Using cache
 ---> 7725836973d4
Step 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
 ---> a338e71895b7
sha256:a338e71895b74a0be98278f35da38c48545f04a54e19ec9e689bab976265350b
Successfully built a338e71895b7
Successfully tagged fun-cache-d4ac1d89-5b75-4429-933a-2260e2f7fbec:latest
copying function artifact to /Users/vangie/Workspace/ta-lib-example/{{ projectName }}

Install Success

Tips 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 invoke
using template: template.yml

Missing invokeName argument, Fun will use the first function ta-lib-example/ta-lib-example as invokeName

skip pulling image aliyunfc/runtime-python3.6:1.7.7...
FunctionCompute python3 runtime inited.
FC Invoke Start RequestId: dc1495b2-13ec-4ecf-a2dc-a0026d82651a
FC 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 deploy
using template: template.yml
using region: cn-shanghai
using accountId: ***********4733
using accessKeyId: ***********EUz3
using timeout: 600

Waiting 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 success
service ta-lib-example deploy success

执行

$ fun invoke
using template: template.yml

Missing 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-daec6856bf4a
FC Invoke End RequestId: 83e23eba-02b4-4380-bbca-daec6856bf4a

Duration: 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"
]

参考阅读

  1. 函数计算
  2. 【手把手教你】股市技术分析利器之TA-Lib(一)

“阿里巴巴云原生关注微服务、Serverless、容器、Service Mesh 等技术领域、聚焦云原生流行技术趋势、云原生大规模的落地实践,做最懂云原生开发者的技术圈。”

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...