Skip to main content

talib extension for formula computation

Project description

ta_formula

PyPI PyPI

依赖库

安装使用

pip install ta_formula

参考案例 examples.ipynb

特性

  • 自定义指标,在TA-Lib(0.4.29)库(Github)的基础上扩展自己的指标,完全在Cython中实现。
  • 自定义区间指标。
  • 自定义策略(同样纯Cython实现),根据参数和输入数据即时编译成动态链接库,实现微秒级信号发现
    • 普通台式机CPU i5-10400 @2.9GHz 大概一个指标计算在0.5~5微秒(timeit结果,实际运行考虑到缓存缺失,会慢很多,可以尝试多进程方案)
    • 支持传入不同市场多个标的物
    • 支持传参
    • 支持自定义返回字段
    • 一次计算中,相同指标计算结果缓存,不重复计算
  • 数据流入、信号流出框架,自定义数据流,数据流支持asyncio,多线程,相同策略相同数据去重,避免重复计算。

策略文件示例

cimport ta_formula.indicators as ta
cimport numpy as np

# define datas intervals
datas = [['1m']]

# define constant params
kdj_minvalue = 10
kdj_maxvalue = 90

# define datas params
CLOSE = datas[0][0]['close']
HIGH = datas[0][0]['high']
LOW = datas[0][0]['low']

# define indicators
ma5 = ta.SMA(CLOSE, 5)
ma250 = ta.SMA(CLOSE, 250)
skd = ta.stream_SLOW_KD(HIGH, LOW, CLOSE, 69, 3)

# define signals
ret = {
    'open_long_condition1': ta.kup(ma250,-1) and ta.crossdown(ma5, ma250, -1),
    'open_short_condition1': ta.kdown(ma250,-1) and ta.crossup(ma5, ma250, -1),
    'open_long_condition2': skd[0] <= kdj_minvalue and CLOSE[-1] > ma250[-1],
    'open_short_condition2': skd[0] >= kdj_maxvalue and CLOSE[-1] < ma250[-1],
    'close_long': skd[0] >= kdj_maxvalue,
    'close_short': skd[0] <= kdj_minvalue,
    'last_close_price': CLOSE[-1],
    'last_ma250': ma250[-1],
}

test_strategy.pyx

返回示例:

{
  "symbols": [["shanghai001", "ag2412"]],
  "data_rec_time": 1717486288.8170903,
  "calc_time": 269900,
  "open_long_condition1": false,
  "open_short_condition1": false,
  "open_long_condition2": false,
  "open_short_condition2": false,
  "close_long": false,
  "close_short": false,
  "last_close_price": 7952.0,
  "last_ma250": 7922.2
}

其中symbols, data_rec_timecalc_time为附加返回字段,分别表示:

  • symbols返回当前策略计算用的金融标的组合
  • 接收到数据的时间戳,单位秒,float,系统时间可能有误差
  • 策略从接收数据,到计算完成,发送信号经过的时间,单位纳秒,int

通过比较三者的时间差,可以大致知道计算延迟和网络延迟

其他字段为自定义返回字段

TODO

  1. 所有stream_XXX指标函数,需要显式注明返回类型,比如int, double,或者tuple类型,比如(double, double)。如果不标明,返回的不是c类型,而是python类型,比如int返回的是PyInt。目前只有部分函数修改了。Cython不支持python对象的tuple,比如(np.ndarray, np.ndarray)。
  2. 将所有ndarray传参修改为double[::1],可以避免动态类型检查,避免调用Python的引用计数,提高效率。目前只有部分函数改了。
  3. ZIG、PERIOD_MAX_BIAS 没有stream和recent函数
  4. 多进程数据后台支持

指标

一个指标有三个版本,比如MACD:

  1. MACD: 从头到尾计算所有指标,返回ndarray。
  2. stream_MACD: 只计算最后一天的指标,返回double,或者tuple(double,double)等。
  3. recent_MACD: 计算最近calc_length天的指标,返回ndarray,当calc_length==1时,效果和stream_MACD一样。

已扩展的方法

指标含义及用法见代码indicators.pyx注释

// 一般指标
SMA, BIAS, MACD, STOCH, KD, KDJ, SLOW_KD AMPLITUDE, ZIG,

// 区间指标
PERIOD_MAX_BIAS

// numpy 函数
shift_inplace shift replace ffill rolling_sum

扩展TA-Lib

_ta_lib_xxx文件是从TA-Lib源码复制过来的。安装完TA-Lib之后,不会安装对应的pxd和pxi文件,所以这里的pxd和pxi直接从TA-Lib源码复制过来。

复制过来的方法,是def定义的,全部改成了cdef,只允许c内部调用。如果要用python测试,可以封装成strategy

如果要改写TA-Lib的方法,_func.pxi源文件、或_stream.pxi源文件已经复制到对应_ta_lib_xxx.pxi,直接在里面改写就行。但是要添加好注释。

添加recent_xxx方法:

从TA-Lib_stream.pxi源文件直接复制到indicators.pyx改写,并改名为recent_xxx

stream_xxx 和 recent_xxx 函数计算精度的问题

使用任何talib库中的stream函数时,都要测试他和非stream函数的返回是否一致

talib计算stream指标时,只计算最后一天的值,但是会往前查看历史数据,一般长度为timeperiod+不稳定期限。

比如EMA(3), 3天前的数据也会影响最后结果,不稳定期限越长,最后结果越精确。

talib所有指标默认不稳定期限为0,要满足自己的精度要求,需要自己设置不稳定期限。

不同的指标,不同的timeperiod,需要设置不同的不确定长度,才能达到相同的精度。

设置不确定长度的方法为:__ta_set_unstable_period(非线程安全)

talib计算精度受历史数据长度影响的指标有:

ADX, ADXR, ATR, CMO, DX, EMA, HT_DCPERIOD, HT_DCPHASE, HT_PHASOR,
HT_SINE, HT_TRENDLINE, HT_TRENDMODE, KAMA, MAMA, MFI, MINUS_DI,
MINUS_DM, NATR, PLUS_DI, PLUS_DM, RSI, STOCHRSI, T3

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ta_formula-1.1.tar.gz (29.9 kB view details)

Uploaded Source

Built Distributions

ta_formula-1.1-cp39-cp39-musllinux_1_2_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.9 musllinux: musl 1.2+ x86-64

ta_formula-1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

ta_formula-1.1-cp39-cp39-macosx_14_0_arm64.whl (460.1 kB view details)

Uploaded CPython 3.9 macOS 14.0+ ARM64

ta_formula-1.1-cp39-cp39-macosx_13_0_x86_64.whl (526.5 kB view details)

Uploaded CPython 3.9 macOS 13.0+ x86-64

File details

Details for the file ta_formula-1.1.tar.gz.

File metadata

  • Download URL: ta_formula-1.1.tar.gz
  • Upload date:
  • Size: 29.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.7

File hashes

Hashes for ta_formula-1.1.tar.gz
Algorithm Hash digest
SHA256 0d4c0d798fd95161545bf0bd89a8394f6f10b8372da12ce1e3ec58f0e7349fe5
MD5 04b16e4f72dff3ea69f05ed5b18b13e1
BLAKE2b-256 f0a55d40247fb4b29d28fd56329c48caf993160ce9755a09a88c995b3161a0f5

See more details on using hashes here.

File details

Details for the file ta_formula-1.1-cp39-cp39-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ta_formula-1.1-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 3da90d6772ba2e9236f8ac1bff253109ecee6c11ab8aaec539173b1ef3b1cdf4
MD5 db03d8674289bb1bd5317578b3110003
BLAKE2b-256 34bf964ebdb6206154fe51d88b5866bbc54f2d8e6556c658945f87c2dcf0caa8

See more details on using hashes here.

File details

Details for the file ta_formula-1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ta_formula-1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 78bfed585db7f33f5f27e60b96df649aae23e61487641cae1553f461682e31df
MD5 234fe6a3a7793a9301f0e8feade9a0f0
BLAKE2b-256 83402cc8d351aaf16acdda9a78354c71c6de24e9362bac34169a9e5c4eda319e

See more details on using hashes here.

File details

Details for the file ta_formula-1.1-cp39-cp39-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for ta_formula-1.1-cp39-cp39-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 c531ffb40a63bc122aadf39d8edcac5eaa74050088949e2b1ec2eed7eb6882c8
MD5 465a052c0e07619d2dce06ccc9b5034e
BLAKE2b-256 c185d5d10af08093096d124f4ef5c2dbc9d4bb6a13c0300793d697e10c8c3360

See more details on using hashes here.

File details

Details for the file ta_formula-1.1-cp39-cp39-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for ta_formula-1.1-cp39-cp39-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 9d9b02d5ddfc9763f192ddd1ea68dedb0d9ea8f77e708880096ee01c0b694921
MD5 13309d81356487b1c49a008e0018287c
BLAKE2b-256 d51011726582fdff6005faa2dcfa218509aa693dcbaa3321c3e174ff7cecca22

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page