ta_formula
依赖库
- ta-lib底层lib库==0.4.0
安装使用
pip install ta_formula
参考案例 examples.ipynb
- matype.md 所有移动平均算法参考
- cdl_pattern_recognition.md 所有TA-Lib中的K线形态识别算法参考
特性
- 自定义指标,在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],
}
返回示例:
{
"symbols": [["shanghai001", "ag2412"]],
"data_update_time": 0,
"data_rec_time": 17174862888170903,
"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_update_time, data_rec_time、calc_time为附加返回字段,分别表示:
- symbols返回当前策略计算用的金融标的组合
- data_update_time数据更新的时间,由数据服务器发送过来,单位以数据后台为准
- 接收到数据的时间戳,单位微妙,int,系统时间可能有误差
- 策略从接收数据,到计算完成,发送信号经过的时间,单位纳秒,int
通过比较三者的时间差,可以大致知道计算延迟和网络延迟
其他字段为自定义返回字段
TODO
- 所有
stream_XXX指标函数,需要显式注明返回类型,比如int, double,或者tuple类型,比如(double, double)。如果不标明,返回的不是c类型,而是python类型,比如int返回的是PyInt。目前只有部分函数修改了。Cython不支持python对象的tuple,比如(np.ndarray, np.ndarray)。 - 将所有ndarray传参修改为double[::1],可以避免动态类型检查,避免调用Python的引用计数,提高效率。目前只有部分函数改了。
- ZIG、PERIOD_MAX_BIAS 没有stream和recent函数
- 多进程数据后台支持
指标
一个指标有三个版本,比如MACD:
MACD: 从头到尾计算所有指标,返回ndarray。stream_MACD: 只计算最后一天的指标,返回double,或者tuple(double,double)等。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
Metadata
Release files for ta-formula 1.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ta_formula-1.1.4.tar.gz | 27.2 kB | Without provenance | Details for ta_formula-1.1.4.tar.gz |
Built distributions (wheels)
| File | Actions Reset | |||
|---|---|---|---|---|
| ta_formula-1.1.4-cp39-cp39-musllinux_1_2_x86_64.whl | CPython 3.9 | CPython 3.9 | Linux musl 1.2+ x86-64 | Details for ta_formula-1.1.4-cp39-cp39-musllinux_1_2_x86_64.whl |
| ta_formula-1.1.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.9 | CPython 3.9 | Linux glibc 2.17+ x86-64 | Details for ta_formula-1.1.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
| ta_formula-1.1.4-cp39-cp39-macosx_14_0_arm64.whl | CPython 3.9 | CPython 3.9 | macOS 14.0+ ARM64 | Details for ta_formula-1.1.4-cp39-cp39-macosx_14_0_arm64.whl |
| ta_formula-1.1.4-cp39-cp39-macosx_13_0_x86_64.whl | CPython 3.9 | CPython 3.9 | macOS 13.0+ x86-64 | Details for ta_formula-1.1.4-cp39-cp39-macosx_13_0_x86_64.whl |
Total release size: 5.1 MB
Release files / ta_formula-1.1.4.tar.gz
| Download URL | ta_formula-1.1.4.tar.gz |
|---|---|
| Size | 27.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
31a1904b147308a30151f3994483b6ef452f42c1f73df3a303c71195dcd97d2b
|
|
BLAKE2b-256 checksum How to use checksums |
f21bdf1361f7a1a26930f435fcaee287952e50ff57aaeaa125556bc179a66a91
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.0 CPython/3.12.9
|
Release files / ta_formula-1.1.4-cp39-cp39-musllinux_1_2_x86_64.whl
| Download URL | ta_formula-1.1.4-cp39-cp39-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 2.0 MB |
| Tags | CPython 3.9 Linux musl 1.2+ x86-64 |
|
SHA-256 checksum How to use checksums |
99c03f8bcc0eb1ba38d825845ab99bd32dbde51094546036d957f9bb8534357c
|
|
BLAKE2b-256 checksum How to use checksums |
f4af4f1290d532b1d4d07ebdb91c4de21e372059e512fc1c1655c896869457a2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.0 CPython/3.12.9
|
Release files / ta_formula-1.1.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | ta_formula-1.1.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 2.1 MB |
| Tags | CPython 3.9 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
f6ae12bfd37a4ee7f46bcfb79803cbf08d892007cc01349d115739845027f29e
|
|
BLAKE2b-256 checksum How to use checksums |
ceaa5e626acafb4cbe4d3f4c1bc491a09852e56ee6783f4668b2439c275f44ff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.0 CPython/3.12.9
|
Release files / ta_formula-1.1.4-cp39-cp39-macosx_14_0_arm64.whl
| Download URL | ta_formula-1.1.4-cp39-cp39-macosx_14_0_arm64.whl |
|---|---|
| Size | 456.8 kB |
| Tags | CPython 3.9 macOS 14.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
585120f80a427c9a269b2ec0d653fe18b7458865e8c0ee3b378ea7111a1c9093
|
|
BLAKE2b-256 checksum How to use checksums |
7f6d61222e40fb67bf6cb5b0a8d5877cae81b216a0f87a1f3f42a6ce4bcf2ae7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.0 CPython/3.12.9
|
Release files / ta_formula-1.1.4-cp39-cp39-macosx_13_0_x86_64.whl
| Download URL | ta_formula-1.1.4-cp39-cp39-macosx_13_0_x86_64.whl |
|---|---|
| Size | 523.1 kB |
| Tags | CPython 3.9 macOS 13.0+ x86-64 |
|
SHA-256 checksum How to use checksums |
3b1eb9ae24340bb83880ce59649b7918b16d066be932c576c6b9aad32906f0c4
|
|
BLAKE2b-256 checksum How to use checksums |
571f1ba6f93930c1c81e40800a4f6fc20bf09ad662ae171b144b4eb348d687fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.0 CPython/3.12.9
|
Security for ta-formula 1.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.