polars expressions
Project description
polars_ta
基于polars的算子库。实现量化投研中常用的技术指标、数据处理等函数。对于不易翻译成Expr的库(如:TA-Lib)也提供了函数式调用的封装
安装
在线安装
pip install -i https://pypi.org/simple --upgrade polars_ta # 官方源
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade polars_ta # 国内镜像源
源码安装
git clone --depth=1 https://github.com/wukan1986/polars_ta.git
cd polars_ta
python -m build
cd dist
pip install polars_ta-0.1.2-py3-none-any.whl
TA-Lib安装
Windows用户不会安装可从https://github.com/cgohlke/talib-build/releases 下载对应版本whl文件
使用方法
参考examples目录即可,例如:
# 如果需要在`expr_codegen`中使用,需要有`ts_`等前权,这里导入提供了前缀
from polars_ta.prefix.tdx import *
# 导入wq公式
from polars_ta.prefix.wq import *
# 演示生成大量指标
df = df.with_columns([
# 从wq中导入指标
*[ts_returns(CLOSE, i).alias(f'ROCP_{i:03d}') for i in (1, 3, 5, 10, 20, 60, 120)],
*[ts_mean(CLOSE, i).alias(f'SMA_{i:03d}') for i in (5, 10, 20, 60, 120)],
*[ts_std_dev(CLOSE, i).alias(f'STD_{i:03d}') for i in (5, 10, 20, 60, 120)],
*[ts_max(HIGH, i).alias(f'HHV_{i:03d}') for i in (5, 10, 20, 60, 120)],
*[ts_min(LOW, i).alias(f'LLV_{i:03d}') for i in (5, 10, 20, 60, 120)],
# 从tdx中导入指标
*[ts_RSI(CLOSE, i).alias(f'RSI_{i:03d}') for i in (6, 12, 24)],
])
设计原则
- 调用方法由
成员函数换成独立函数。输入输出使用Expr,避免使用Series - 优先实现
wq公式,它仿WorldQuant Alpha公式,与官网尽量保持一致。如果部分功能实现在此更合适将放在此处 - 其次实现
ta公式,它相当于TA-Lib的polars风格的版本。优先从wq中导入更名 - 最后实现
tdx公式,它也是优先从wq和ta中导入 talib的函数名与参数与原版TA-Lib完全一致- 如果出现了命名冲突,建议调用优先级为
wq、ta、tdx、talib。因为优先级越高,实现方案越接近于Expr
指标区别
请参考compare
空值处理
请参考nan_to_null
TA-Lib封装的演化
Expr.map_batches可以实现调用第三方库,如TA-Lib, bottleneck。但因为对输入与输出格式有要求,所以还需要用函数对第三方API封装一下。- 输入输出都只能是一列,如要支持多列需转换成
pl.Struct。事后pl.Struct要拆分需使用unnest - 输出必须是
pl.Series
- 输入输出都只能是一列,如要支持多列需转换成
- 参数多,代码长。开始使用
register_expr_namespace来简化代码- 实现代码helper.py
- 使用演示demo_ta1.py
- 优点:使用简单
- 不足:
成员函数调用模式不便于输入到遗传算法中进行因子挖掘 - 不足:
__getattribute__动态方法调用非常灵活,但失去了IDE智能提示
- 前缀表达式。将所有的成员函数都转换成公式
- 实现代码wrapper.py
- 使用演示demo_ta2.py
- 优点:可以输入到遗传算法
- 不足:
__getattribute__动态方法调用非常灵活,但失去了IDE智能提示
- 代码自动生成。
- 实现代码codegen_talib2.py
- 生成结果__init__.py
- 使用演示demo_ta3.py
- 优点:即可以输入到遗传算法,
IDE还有智能提示
开发调试
git clone --depth=1 https://github.com/wukan1986/polars_ta.git
cd polars_ta
pip install -e .
注意:如果你在ta或tdx中添加了新的函数,请再运行tools下的prefix_ta.py或prefix_tdx.py,用于生成对应的前缀文件。前缀文件方便在expr_codegen中使用
参考
- https://github.com/pola-rs/polars
- https://github.com/TA-Lib/ta-lib
- https://github.com/twopirllc/pandas-ta
- https://github.com/bukosabino/ta
- https://github.com/peerchemist/finta
- https://github.com/wukan1986/ta_cn
- https://support.worldquantbrain.com/hc/en-us/community/posts/20278408956439-从价量看技术指标总结-Technical-Indicator-
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file polars_ta-0.2.0.tar.gz.
File metadata
- Download URL: polars_ta-0.2.0.tar.gz
- Upload date:
- Size: 37.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8013c37911ff4b1b2dcb6ded5637dab1acf8f6787380918d37d838d2f054dd8d
|
|
| MD5 |
485d04a51ac3f54eedac6fd3d3ff615a
|
|
| BLAKE2b-256 |
103f4f59e6b4e999f746b03f5a32d8a54fded5ab1d9519eb0d53ca5184528587
|
File details
Details for the file polars_ta-0.2.0-py3-none-any.whl.
File metadata
- Download URL: polars_ta-0.2.0-py3-none-any.whl
- Upload date:
- Size: 48.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
947f9c1fe02c324e7483ba6697cb3865c87d1f89c2b883f880c581d1d5526e13
|
|
| MD5 |
ce0572cb06199c8963e00554269f377d
|
|
| BLAKE2b-256 |
6aa071b4895e7fde824fc7794da8d79541953f4f5b4245172809bda9ea6f79d0
|