{"id":34090778,"url":"https://github.com/sric0880/ta_formula","last_synced_at":"2026-03-08T21:37:59.855Z","repository":{"id":241907644,"uuid":"807435668","full_name":"sric0880/ta_formula","owner":"sric0880","description":"talib extension for formula computation in pure cython in your custom strategies.","archived":false,"fork":false,"pushed_at":"2025-03-10T01:36:26.000Z","size":3697,"stargazers_count":2,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-16T17:41:57.819Z","etag":null,"topics":["formula","quant","quantum-computing","ta","ta-lib","talib","technical-analysis","technical-analysis-indicators"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sric0880.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-05-29T05:23:29.000Z","updated_at":"2025-03-10T01:00:29.000Z","dependencies_parsed_at":"2024-06-12T08:29:16.957Z","dependency_job_id":"c050cecc-fe0e-4d8e-aadd-555dace64385","html_url":"https://github.com/sric0880/ta_formula","commit_stats":null,"previous_names":["sric0880/ta_formula"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/sric0880/ta_formula","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sric0880%2Fta_formula","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sric0880%2Fta_formula/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sric0880%2Fta_formula/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sric0880%2Fta_formula/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sric0880","download_url":"https://codeload.github.com/sric0880/ta_formula/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sric0880%2Fta_formula/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30274829,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-08T20:45:49.896Z","status":"ssl_error","status_checked_at":"2026-03-08T20:45:49.525Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["formula","quant","quantum-computing","ta","ta-lib","talib","technical-analysis","technical-analysis-indicators"],"created_at":"2025-12-14T14:25:31.286Z","updated_at":"2026-03-08T21:37:59.848Z","avatar_url":"https://github.com/sric0880.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ta_formula\n\n[![PyPI](https://github.com/sric0880/ta_formula/actions/workflows/python-publish.yml/badge.svg)](https://github.com/sric0880/ta_formula/actions/workflows/python-publish.yml) [![PyPI](https://img.shields.io/pypi/v/ta_formula)](https://pypi.org/project/ta-formula/)\n\n## 依赖库\n\n- [ta-lib底层lib库](https://anaconda.org/conda-forge/libta-lib/files)==0.4.0\n\n## 安装使用\n\n```sh\npip install ta_formula\n```\n\n参考案例 [examples.ipynb](https://github.com/sric0880/ta_formula/blob/main/examples.ipynb)\n\n- [matype.md](https://github.com/sric0880/ta_formula/blob/main/matype.md) 所有移动平均算法参考\n- [cdl_pattern_recognition.md](https://github.com/sric0880/ta_formula/blob/main/cdl_pattern_recognition.md) 所有TA-Lib中的K线形态识别算法参考\n\n## 特性\n\n- 自定义指标，在[TA-Lib(0.4.29)](https://pypi.org/project/TA-Lib/)库([Github](https://github.com/TA-Lib/ta-lib-python))的基础上扩展自己的指标，完全在Cython中实现。\n- 自定义区间指标。\n- 自定义策略（同样纯Cython实现），根据参数和输入数据即时编译成动态链接库，实现微秒级信号发现\n  - 普通台式机CPU i5-10400 @2.9GHz 大概一个指标计算在0.5~5微秒（timeit结果，实际运行考虑到缓存缺失，会慢很多，可以尝试多进程方案）\n  - 支持传入不同市场多个标的物\n  - 支持传参\n  - 支持自定义返回字段\n  - 一次计算中，相同指标计算结果缓存，不重复计算\n- 数据流入、信号流出框架，自定义数据流，数据流支持asyncio，多线程，相同策略相同数据去重，避免重复计算。\n\n## 策略文件示例\n\n```py\ncimport ta_formula.indicators as ta\ncimport numpy as np\n\n# define datas intervals\ndatas = [['1m']]\n\n# define constant params\nkdj_minvalue = 10\nkdj_maxvalue = 90\n\n# define datas params\nCLOSE = datas[0][0]['close']\nHIGH = datas[0][0]['high']\nLOW = datas[0][0]['low']\n\n# define indicators\nma5 = ta.SMA(CLOSE, 5)\nma250 = ta.SMA(CLOSE, 250)\nskd = ta.stream_SLOW_KD(HIGH, LOW, CLOSE, 69, 3)\n\n# define signals\nret = {\n    'open_long_condition1': ta.kup(ma250,-1) and ta.crossdown(ma5, ma250, -1),\n    'open_short_condition1': ta.kdown(ma250,-1) and ta.crossup(ma5, ma250, -1),\n    'open_long_condition2': skd[0] \u003c= kdj_minvalue and CLOSE[-1] \u003e ma250[-1],\n    'open_short_condition2': skd[0] \u003e= kdj_maxvalue and CLOSE[-1] \u003c ma250[-1],\n    'close_long': skd[0] \u003e= kdj_maxvalue,\n    'close_short': skd[0] \u003c= kdj_minvalue,\n    'last_close_price': CLOSE[-1],\n    'last_ma250': ma250[-1],\n}\n```\n\n见 [test_strategy.pyx](https://github.com/sric0880/ta_formula/blob/main/test_strategy.pyx)\n\n返回示例：\n\n```json\n{\n  \"symbols\": [[\"shanghai001\", \"ag2412\"]],\n  \"data_update_time\": 0,\n  \"data_rec_time\": 17174862888170903,\n  \"calc_time\": 269900,\n  \"open_long_condition1\": false,\n  \"open_short_condition1\": false,\n  \"open_long_condition2\": false,\n  \"open_short_condition2\": false,\n  \"close_long\": false,\n  \"close_short\": false,\n  \"last_close_price\": 7952.0,\n  \"last_ma250\": 7922.2\n}\n```\n\n其中`symbols`, `data_update_time`, `data_rec_time`、`calc_time`为附加返回字段，分别表示:\n\n- symbols返回当前策略计算用的金融标的组合\n- data_update_time数据更新的时间，由数据服务器发送过来，单位以数据后台为准\n- 接收到数据的时间戳，单位微妙，int，系统时间可能有误差\n- 策略从接收数据，到计算完成，发送信号经过的时间，单位纳秒，int\n\n通过比较三者的时间差，可以大致知道计算延迟和网络延迟\n\n其他字段为自定义返回字段\n\n## TODO\n\n1. 所有`stream_XXX`指标函数，需要显式注明返回类型，比如int, double，或者tuple类型，比如(double, double)。如果不标明，返回的不是c类型，而是python类型，比如int返回的是PyInt。目前只有部分函数修改了。Cython不支持python对象的tuple，比如(np.ndarray, np.ndarray)。\n2. 将所有ndarray传参修改为double[::1]，可以避免动态类型检查，避免调用Python的引用计数，提高效率。目前只有部分函数改了。\n3. ZIG、PERIOD_MAX_BIAS 没有stream和recent函数\n4. 多进程数据后台支持\n\n## 指标\n\n一个指标有三个版本，比如MACD：\n\n1. `MACD`: 从头到尾计算所有指标，返回ndarray。\n2. `stream_MACD`: 只计算最后一天的指标，返回double,或者tuple(double,double)等。\n3. `recent_MACD`: 计算最近`calc_length`天的指标，返回ndarray，当`calc_length==1`时，效果和`stream_MACD`一样。\n\n## 已扩展的方法\n\n指标含义及用法见代码`indicators.pyx`注释\n\n```c\n// 一般指标\nSMA, BIAS, MACD, STOCH, KD, KDJ, SLOW_KD AMPLITUDE, ZIG,\n\n// 区间指标\nPERIOD_MAX_BIAS\n\n// numpy 函数\nshift_inplace shift replace ffill rolling_sum\n```\n\n## 扩展TA-Lib\n\n`_ta_lib_xxx`文件是从TA-Lib源码复制过来的。安装完TA-Lib之后，不会安装对应的pxd和pxi文件，所以这里的pxd和pxi直接从TA-Lib源码复制过来。\n\n复制过来的方法，是`def`定义的，全部改成了`cdef`，只允许c内部调用。如果要用python测试，可以封装成`strategy`。\n\n如果要改写TA-Lib的方法，`_func.pxi`源文件、或`_stream.pxi`源文件已经复制到对应`_ta_lib_xxx.pxi`，直接在里面改写就行。但是要添加好注释。\n\n添加`recent_xxx`方法：\n\n从TA-Lib`_stream.pxi`源文件直接复制到`indicators.pyx`改写，并改名为`recent_xxx`\n\n## stream_xxx 和 recent_xxx 函数计算精度的问题\n\n使用任何talib库中的stream函数时，都要测试他和非stream函数的返回是否一致\n\ntalib计算stream指标时，只计算最后一天的值，但是会往前查看历史数据，一般长度为timeperiod+不稳定期限。\n\n比如EMA(3), 3天前的数据也会影响最后结果，不稳定期限越长，最后结果越精确。\n\ntalib所有指标默认不稳定期限为0，要满足自己的精度要求，需要自己设置不稳定期限。\n\n不同的指标，不同的timeperiod，需要设置不同的不确定长度，才能达到相同的精度。\n\n设置不确定长度的方法为：__ta_set_unstable_period(非线程安全)\n\ntalib计算精度受历史数据长度影响的指标有:\n\n```c\nADX, ADXR, ATR, CMO, DX, EMA, HT_DCPERIOD, HT_DCPHASE, HT_PHASOR,\nHT_SINE, HT_TRENDLINE, HT_TRENDMODE, KAMA, MAMA, MFI, MINUS_DI,\nMINUS_DM, NATR, PLUS_DI, PLUS_DM, RSI, STOCHRSI, T3\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsric0880%2Fta_formula","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsric0880%2Fta_formula","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsric0880%2Fta_formula/lists"}