expr-codegen


Nameexpr-codegen JSON
Version 0.6.2 PyPI version JSON
download
home_pageNone
Summarysymbol expression to polars expression tool
upload_time2024-04-28 06:58:33
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseBSD 3-Clause License Copyright (c) 2023, wukan Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
keywords polars expression talib
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # expr_codegen 符号表达式代码生成器

表达式转代码工具

## 项目背景

在本人新推出[polars_ta](https://github.com/wukan1986/polars_ta)这个库后,再回头反思`expr_codegen`是什么。

> `expr_cdegen`本质是`DSL`,领域特定语⾔(Domain Specific Language)。但它没有定义新的语法

它解决了两个问题:

1. `polars_ta`已经能很方便的写出特征计算表达式,但遇到`混用时序与截面`的表达式,利用`expr_codegen`能自动分组大大节省工作
2. `expr_codegen`利用了`Common Subexpression Elimination`公共子表达式消除,大量减少重复计算,提高效率

就算在量化领域,初级研究员局限于时序指标,仅用`polars_ta`即可,中高级研究员使用截面指标,推荐用`expr_codegen`

虽然现在此项目与`polars_ta`依赖非常紧密,但也是支持翻译成其它库,如`pandas / cudf.pandas`,只是目前缺乏一个比较简易的库

## 在线演示

https://exprcodegen.streamlit.app

初级用户可以直接访问此链接进行表达式转译,不需要另外安装软件。(此工具免费部署在国外,打开可能有些慢)

更完整示例访问[alpha_examples](https://github.com/wukan1986/alpha_examples)

## 使用方法

运行`demo_cn.py`生成`output.py`,将此文件复制到其它项目中直接`import`使用即可。一般生成的文件不需要再修改。

## 目录结构

```commandline
│  requirements.txt # 通过`pip install -r requirements.txt`安装依赖
├─data
│      prepare_date.py # 准备数据
├─examples
│      alpha101.txt # WorldQuant Alpha101示例,可复制到`streamlit`应用
│      demo_cn.py # 中文注释示例。演示如何将表达式转换成代码
│      demo_exec_pl.py # 演示调用转换后代码并绘图
│      demo_transformer.py # 演示将第三方表达式转成内部表达式
│      output.py # 结果输出。可不修改代码,直接被其它项目导入
│      show_tree.py # 画表达式树形图。可用于分析对比优化结果
│      sympy_define.py # 符号定义,由于太多地方重复使用到,所以统一提取到此处
├─expr_codegen
│   │  expr.py # 表达式处理基本函数
│   │  tool.py # 核心工具代码。一般不需修改
│   ├─polars
│   │  │  code.py # 针对polars语法的代码生成功能
│   │  │  template.py.j2 # `Jinja2`模板。用于生成对应py文件,一般不需修改
│   │  │  printer.py # 继承于`Sympy`中的`StrPrinter`,添加新函数时可能需修改此文件
```

## 工作原理

本项目依赖于`sympy`项目。所用到的主要函数如下:

1. `simplify`: 对复杂表达式进行化简
2. `cse`: `Common Subexpression Elimination`公共子表达式消除
3. `StrPrinter`: 根据不同的函数输出不同字符串。定制此代码可以支持其它语种或库

因为`groupby`,`sort`都比较占用时间。如果提前将公式分类,不同的类别使用不同的`groupby`,可以减少计算时间。

1. `ts_xxx(ts_xxx)`: 可在同一`groupby`中进行计算
2. `cs_xxx(cs_xxx)`: 可在同一`groupby`中进行计算
3. `ts_xxx(cs_xxx)`: 需在不同`groupby`中进行计算
4. `cs_xxx(ts_xxx(cs_xxx))`: 需三不同`groupby`中进行计算
5. `gp_xxx(aa, )+gp_xxx(bb, )`: 因`aa`,`bb`不同,需在两不同`groupby`中进行计算

所以

1. 需要有一个函数能获取当前表达式的类别(`get_current`)和子表达式的类别(`get_children`)
2. 如果当前类别与子类别不同就可以提取出短公式(`extract`)。不同层的同类别表达式有先后关系,不能放同一`groupby`
3. 利用`cse`的特点,将长表达式替换成前期提取出来的短表达式。然后输入到有向无环图(`DAG`)
4. 利用有向无环图的流转,进行分层。同一层的`ts`,`cs`,`gp`不区分先后
5. 同一层对`ts`,`cs`,`gp`分组,然后生成代码(`codegen`)即可

隐含信息

1. `ts`: sort(by=[ASSET, DATE]).groupby(by=[ASSET], maintain_order=True)
2. `cs`: sort(by=[DATE]).groupby(by=[DATE], maintain_order=False)
3. `gp`: sort(by=[DATE, GROUP]).groupby(by=[DATE, GROUP], maintain_order=False)

即

1. 时序函数隐藏了两个字段`ASSET, DATE`,横截面函数了隐藏了一个字段`DATE`
2. 分组函数转入了一个字段`GROUP`,同时隐藏了一个字段`DATE`

两种分类方法

1. 根据算子前缀分类(`get_current_by_prefix`),限制算子必需以`ts_`、`cs_`、`gp_`开头
2. 根据算子全名分类(`get_current_by_name`), 不再限制算子名。比如`cs_rank`可以叫`rank`

## 二次开发

1. 备份后编辑`demo_cn.py`, `import`需要引入的函数
2. 然后`printer.py`有可能需要添加对应函数的打印代码
    - 注意:需要留意是否要加括号`()`,不加时可能优先级混乱,可以每次都加括号,也可用提供的`parenthesize`简化处理

## 贡献代码

1. 还有很多函数没有添加,需要大家提交代码一起完善
2. 目前表达式样式优先向WorldQuant 的 Alpha101 靠齐

## 小技巧

1. `sympy`不支持`==`,而是当成两个对象比较。例如:
    1. `if_else(OPEN==CLOSE, HIGH, LOW)`, 一开始就变成了`if_else(False, HIGH, LOW)`
    2. 可以用`Eq`来代替,`if_else(Eq(OPEN, CLOSE), HIGH, LOW)`。具体示例请参考`Alpha101`中的`alpha_021`

2. `sympy`不支持`bool`转`int`。例如:
    1. `(OPEN < CLOSE) * -1`报错 `TypeError: unsupported operand type(s) for *: 'StrictLessThan' and 'int'`
    2. 可以用`if_else`代替。`if_else(OPEN<CLOSE, 1, 0)*-1`。具体示例请参考`Alpha101`中的`alpha_064`
3. Python不支持`?:`三元表达式,只支持`if else`, 而在本项目中需要转成`if_else`

以上三种问题本项目都使用`ast`进行了处理,可以简化使用

## 示例片段

需要转译的部分公式,详细代码请参考 [Demo](examples/demo_cn.py)

```python
exprs_src = {
    "expr_1": -ts_corr(cs_rank(ts_mean(OPEN, 10)), cs_rank(ts_mean(CLOSE, 10)), 10),
    "expr_2": cs_rank(ts_mean(OPEN, 10)) - abs_(log(ts_mean(CLOSE, 10))) + gp_rank(sw_l1, CLOSE),
    "expr_3": ts_mean(cs_rank(ts_mean(OPEN, 10)), 10),
    "expr_4": cs_rank(ts_mean(cs_rank(OPEN), 10)),
    "expr_5": -ts_corr(OPEN, CLOSE, 10),
}
```

转译后的代码片段,详细代码请参考[Polars版](codes)

```python
def func_0_ts__asset(df: pl.DataFrame) -> pl.DataFrame:
    df = df.sort(by=[_DATE_])
    # ========================================
    df = df.with_columns(
        _x_0=1 / ts_delay(OPEN, -1),
        LABEL_CC_1=(-CLOSE + ts_delay(CLOSE, -1)) / CLOSE,
    )
    # ========================================
    df = df.with_columns(
        LABEL_OO_1=_x_0 * ts_delay(OPEN, -2) - 1,
        LABEL_OO_2=_x_0 * ts_delay(OPEN, -3) - 1,
    )
    return df
```

转译后的代码片段,详细代码请参考[Pandas版](examples/output_pandas.py)

```python
def func_2_cs__date(df: pd.DataFrame) -> pd.DataFrame:
    # expr_4 = cs_rank(x_7)
    df["expr_4"] = (df["x_7"]).rank(pct=True)
    return df


def func_3_ts__asset__date(df: pd.DataFrame) -> pd.DataFrame:
    # expr_5 = -ts_corr(OPEN, CLOSE, 10)
    df["expr_5"] = -(df["OPEN"]).rolling(10).corr(df["CLOSE"])
    # expr_6 = ts_delta(OPEN, 10)
    df["expr_6"] = df["OPEN"].diff(10)
    return df


df = df.sort_values(by=["asset", "date"]).groupby(by=["asset"], group_keys=False).apply(func_0_ts__asset__date)
df = df.groupby(by=["date"], group_keys=False).apply(func_0_cs__date)
df = func_0_cl(df)
```

## 本地部署交互网页

只需运行`streamlit run streamlit_app.py`

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "expr-codegen",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "polars, expression, talib",
    "author": null,
    "author_email": "wukan <wu-kan@163.com>",
    "download_url": "https://files.pythonhosted.org/packages/a7/9e/5aa33f70f76b4f082aab2bda140fc6b09620927beea8378470274b3a8d2d/expr_codegen-0.6.2.tar.gz",
    "platform": null,
    "description": "# expr_codegen \u7b26\u53f7\u8868\u8fbe\u5f0f\u4ee3\u7801\u751f\u6210\u5668\n\n\u8868\u8fbe\u5f0f\u8f6c\u4ee3\u7801\u5de5\u5177\n\n## \u9879\u76ee\u80cc\u666f\n\n\u5728\u672c\u4eba\u65b0\u63a8\u51fa[polars_ta](https://github.com/wukan1986/polars_ta)\u8fd9\u4e2a\u5e93\u540e\uff0c\u518d\u56de\u5934\u53cd\u601d`expr_codegen`\u662f\u4ec0\u4e48\u3002\n\n> `expr_cdegen`\u672c\u8d28\u662f`DSL`\uff0c\u9886\u57df\u7279\u5b9a\u8bed\u2f94(Domain Specific Language)\u3002\u4f46\u5b83\u6ca1\u6709\u5b9a\u4e49\u65b0\u7684\u8bed\u6cd5\n\n\u5b83\u89e3\u51b3\u4e86\u4e24\u4e2a\u95ee\u9898:\n\n1. `polars_ta`\u5df2\u7ecf\u80fd\u5f88\u65b9\u4fbf\u7684\u5199\u51fa\u7279\u5f81\u8ba1\u7b97\u8868\u8fbe\u5f0f\uff0c\u4f46\u9047\u5230`\u6df7\u7528\u65f6\u5e8f\u4e0e\u622a\u9762`\u7684\u8868\u8fbe\u5f0f\uff0c\u5229\u7528`expr_codegen`\u80fd\u81ea\u52a8\u5206\u7ec4\u5927\u5927\u8282\u7701\u5de5\u4f5c\n2. `expr_codegen`\u5229\u7528\u4e86`Common Subexpression Elimination`\u516c\u5171\u5b50\u8868\u8fbe\u5f0f\u6d88\u9664\uff0c\u5927\u91cf\u51cf\u5c11\u91cd\u590d\u8ba1\u7b97\uff0c\u63d0\u9ad8\u6548\u7387\n\n\u5c31\u7b97\u5728\u91cf\u5316\u9886\u57df\uff0c\u521d\u7ea7\u7814\u7a76\u5458\u5c40\u9650\u4e8e\u65f6\u5e8f\u6307\u6807\uff0c\u4ec5\u7528`polars_ta`\u5373\u53ef\uff0c\u4e2d\u9ad8\u7ea7\u7814\u7a76\u5458\u4f7f\u7528\u622a\u9762\u6307\u6807\uff0c\u63a8\u8350\u7528`expr_codegen`\n\n\u867d\u7136\u73b0\u5728\u6b64\u9879\u76ee\u4e0e`polars_ta`\u4f9d\u8d56\u975e\u5e38\u7d27\u5bc6\uff0c\u4f46\u4e5f\u662f\u652f\u6301\u7ffb\u8bd1\u6210\u5176\u5b83\u5e93,\u5982`pandas / cudf.pandas`\uff0c\u53ea\u662f\u76ee\u524d\u7f3a\u4e4f\u4e00\u4e2a\u6bd4\u8f83\u7b80\u6613\u7684\u5e93\n\n## \u5728\u7ebf\u6f14\u793a\n\nhttps://exprcodegen.streamlit.app\n\n\u521d\u7ea7\u7528\u6237\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u6b64\u94fe\u63a5\u8fdb\u884c\u8868\u8fbe\u5f0f\u8f6c\u8bd1\uff0c\u4e0d\u9700\u8981\u53e6\u5916\u5b89\u88c5\u8f6f\u4ef6\u3002(\u6b64\u5de5\u5177\u514d\u8d39\u90e8\u7f72\u5728\u56fd\u5916\uff0c\u6253\u5f00\u53ef\u80fd\u6709\u4e9b\u6162)\n\n\u66f4\u5b8c\u6574\u793a\u4f8b\u8bbf\u95ee[alpha_examples](https://github.com/wukan1986/alpha_examples)\n\n## \u4f7f\u7528\u65b9\u6cd5\n\n\u8fd0\u884c`demo_cn.py`\u751f\u6210`output.py`\uff0c\u5c06\u6b64\u6587\u4ef6\u590d\u5236\u5230\u5176\u5b83\u9879\u76ee\u4e2d\u76f4\u63a5`import`\u4f7f\u7528\u5373\u53ef\u3002\u4e00\u822c\u751f\u6210\u7684\u6587\u4ef6\u4e0d\u9700\u8981\u518d\u4fee\u6539\u3002\n\n## \u76ee\u5f55\u7ed3\u6784\n\n```commandline\n\u2502  requirements.txt # \u901a\u8fc7`pip install -r requirements.txt`\u5b89\u88c5\u4f9d\u8d56\n\u251c\u2500data\n\u2502      prepare_date.py # \u51c6\u5907\u6570\u636e\n\u251c\u2500examples\n\u2502      alpha101.txt # WorldQuant Alpha101\u793a\u4f8b\uff0c\u53ef\u590d\u5236\u5230`streamlit`\u5e94\u7528\n\u2502      demo_cn.py # \u4e2d\u6587\u6ce8\u91ca\u793a\u4f8b\u3002\u6f14\u793a\u5982\u4f55\u5c06\u8868\u8fbe\u5f0f\u8f6c\u6362\u6210\u4ee3\u7801\n\u2502      demo_exec_pl.py # \u6f14\u793a\u8c03\u7528\u8f6c\u6362\u540e\u4ee3\u7801\u5e76\u7ed8\u56fe\n\u2502      demo_transformer.py # \u6f14\u793a\u5c06\u7b2c\u4e09\u65b9\u8868\u8fbe\u5f0f\u8f6c\u6210\u5185\u90e8\u8868\u8fbe\u5f0f\n\u2502      output.py # \u7ed3\u679c\u8f93\u51fa\u3002\u53ef\u4e0d\u4fee\u6539\u4ee3\u7801\uff0c\u76f4\u63a5\u88ab\u5176\u5b83\u9879\u76ee\u5bfc\u5165\n\u2502      show_tree.py # \u753b\u8868\u8fbe\u5f0f\u6811\u5f62\u56fe\u3002\u53ef\u7528\u4e8e\u5206\u6790\u5bf9\u6bd4\u4f18\u5316\u7ed3\u679c\n\u2502      sympy_define.py # \u7b26\u53f7\u5b9a\u4e49\uff0c\u7531\u4e8e\u592a\u591a\u5730\u65b9\u91cd\u590d\u4f7f\u7528\u5230\uff0c\u6240\u4ee5\u7edf\u4e00\u63d0\u53d6\u5230\u6b64\u5904\n\u251c\u2500expr_codegen\n\u2502   \u2502  expr.py # \u8868\u8fbe\u5f0f\u5904\u7406\u57fa\u672c\u51fd\u6570\n\u2502   \u2502  tool.py # \u6838\u5fc3\u5de5\u5177\u4ee3\u7801\u3002\u4e00\u822c\u4e0d\u9700\u4fee\u6539\n\u2502   \u251c\u2500polars\n\u2502   \u2502  \u2502  code.py # \u9488\u5bf9polars\u8bed\u6cd5\u7684\u4ee3\u7801\u751f\u6210\u529f\u80fd\n\u2502   \u2502  \u2502  template.py.j2 # `Jinja2`\u6a21\u677f\u3002\u7528\u4e8e\u751f\u6210\u5bf9\u5e94py\u6587\u4ef6\uff0c\u4e00\u822c\u4e0d\u9700\u4fee\u6539\n\u2502   \u2502  \u2502  printer.py # \u7ee7\u627f\u4e8e`Sympy`\u4e2d\u7684`StrPrinter`\uff0c\u6dfb\u52a0\u65b0\u51fd\u6570\u65f6\u53ef\u80fd\u9700\u4fee\u6539\u6b64\u6587\u4ef6\n```\n\n## \u5de5\u4f5c\u539f\u7406\n\n\u672c\u9879\u76ee\u4f9d\u8d56\u4e8e`sympy`\u9879\u76ee\u3002\u6240\u7528\u5230\u7684\u4e3b\u8981\u51fd\u6570\u5982\u4e0b\uff1a\n\n1. `simplify`: \u5bf9\u590d\u6742\u8868\u8fbe\u5f0f\u8fdb\u884c\u5316\u7b80\n2. `cse`: `Common Subexpression Elimination`\u516c\u5171\u5b50\u8868\u8fbe\u5f0f\u6d88\u9664\n3. `StrPrinter`: \u6839\u636e\u4e0d\u540c\u7684\u51fd\u6570\u8f93\u51fa\u4e0d\u540c\u5b57\u7b26\u4e32\u3002\u5b9a\u5236\u6b64\u4ee3\u7801\u53ef\u4ee5\u652f\u6301\u5176\u5b83\u8bed\u79cd\u6216\u5e93\n\n\u56e0\u4e3a`groupby`,`sort`\u90fd\u6bd4\u8f83\u5360\u7528\u65f6\u95f4\u3002\u5982\u679c\u63d0\u524d\u5c06\u516c\u5f0f\u5206\u7c7b\uff0c\u4e0d\u540c\u7684\u7c7b\u522b\u4f7f\u7528\u4e0d\u540c\u7684`groupby`\uff0c\u53ef\u4ee5\u51cf\u5c11\u8ba1\u7b97\u65f6\u95f4\u3002\n\n1. `ts_xxx(ts_xxx)`: \u53ef\u5728\u540c\u4e00`groupby`\u4e2d\u8fdb\u884c\u8ba1\u7b97\n2. `cs_xxx(cs_xxx)`: \u53ef\u5728\u540c\u4e00`groupby`\u4e2d\u8fdb\u884c\u8ba1\u7b97\n3. `ts_xxx(cs_xxx)`: \u9700\u5728\u4e0d\u540c`groupby`\u4e2d\u8fdb\u884c\u8ba1\u7b97\n4. `cs_xxx(ts_xxx(cs_xxx))`: \u9700\u4e09\u4e0d\u540c`groupby`\u4e2d\u8fdb\u884c\u8ba1\u7b97\n5. `gp_xxx(aa, )+gp_xxx(bb, )`: \u56e0`aa`,`bb`\u4e0d\u540c\uff0c\u9700\u5728\u4e24\u4e0d\u540c`groupby`\u4e2d\u8fdb\u884c\u8ba1\u7b97\n\n\u6240\u4ee5\n\n1. \u9700\u8981\u6709\u4e00\u4e2a\u51fd\u6570\u80fd\u83b7\u53d6\u5f53\u524d\u8868\u8fbe\u5f0f\u7684\u7c7b\u522b(`get_current`)\u548c\u5b50\u8868\u8fbe\u5f0f\u7684\u7c7b\u522b(`get_children`)\n2. \u5982\u679c\u5f53\u524d\u7c7b\u522b\u4e0e\u5b50\u7c7b\u522b\u4e0d\u540c\u5c31\u53ef\u4ee5\u63d0\u53d6\u51fa\u77ed\u516c\u5f0f(`extract`)\u3002\u4e0d\u540c\u5c42\u7684\u540c\u7c7b\u522b\u8868\u8fbe\u5f0f\u6709\u5148\u540e\u5173\u7cfb\uff0c\u4e0d\u80fd\u653e\u540c\u4e00`groupby`\n3. \u5229\u7528`cse`\u7684\u7279\u70b9\uff0c\u5c06\u957f\u8868\u8fbe\u5f0f\u66ff\u6362\u6210\u524d\u671f\u63d0\u53d6\u51fa\u6765\u7684\u77ed\u8868\u8fbe\u5f0f\u3002\u7136\u540e\u8f93\u5165\u5230\u6709\u5411\u65e0\u73af\u56fe(`DAG`)\n4. \u5229\u7528\u6709\u5411\u65e0\u73af\u56fe\u7684\u6d41\u8f6c\uff0c\u8fdb\u884c\u5206\u5c42\u3002\u540c\u4e00\u5c42\u7684`ts`,`cs`,`gp`\u4e0d\u533a\u5206\u5148\u540e\n5. \u540c\u4e00\u5c42\u5bf9`ts`,`cs`,`gp`\u5206\u7ec4\uff0c\u7136\u540e\u751f\u6210\u4ee3\u7801(`codegen`)\u5373\u53ef\n\n\u9690\u542b\u4fe1\u606f\n\n1. `ts`: sort(by=[ASSET, DATE]).groupby(by=[ASSET], maintain_order=True)\n2. `cs`: sort(by=[DATE]).groupby(by=[DATE], maintain_order=False)\n3. `gp`: sort(by=[DATE, GROUP]).groupby(by=[DATE, GROUP], maintain_order=False)\n\n\u5373\n\n1. \u65f6\u5e8f\u51fd\u6570\u9690\u85cf\u4e86\u4e24\u4e2a\u5b57\u6bb5`ASSET, DATE`\uff0c\u6a2a\u622a\u9762\u51fd\u6570\u4e86\u9690\u85cf\u4e86\u4e00\u4e2a\u5b57\u6bb5`DATE`\n2. \u5206\u7ec4\u51fd\u6570\u8f6c\u5165\u4e86\u4e00\u4e2a\u5b57\u6bb5`GROUP`\uff0c\u540c\u65f6\u9690\u85cf\u4e86\u4e00\u4e2a\u5b57\u6bb5`DATE`\n\n\u4e24\u79cd\u5206\u7c7b\u65b9\u6cd5\n\n1. \u6839\u636e\u7b97\u5b50\u524d\u7f00\u5206\u7c7b(`get_current_by_prefix`)\uff0c\u9650\u5236\u7b97\u5b50\u5fc5\u9700\u4ee5`ts_`\u3001`cs_`\u3001`gp_`\u5f00\u5934\n2. \u6839\u636e\u7b97\u5b50\u5168\u540d\u5206\u7c7b(`get_current_by_name`), \u4e0d\u518d\u9650\u5236\u7b97\u5b50\u540d\u3002\u6bd4\u5982`cs_rank`\u53ef\u4ee5\u53eb`rank`\n\n## \u4e8c\u6b21\u5f00\u53d1\n\n1. \u5907\u4efd\u540e\u7f16\u8f91`demo_cn.py`, `import`\u9700\u8981\u5f15\u5165\u7684\u51fd\u6570\n2. \u7136\u540e`printer.py`\u6709\u53ef\u80fd\u9700\u8981\u6dfb\u52a0\u5bf9\u5e94\u51fd\u6570\u7684\u6253\u5370\u4ee3\u7801\n    - \u6ce8\u610f\uff1a\u9700\u8981\u7559\u610f\u662f\u5426\u8981\u52a0\u62ec\u53f7`()`\uff0c\u4e0d\u52a0\u65f6\u53ef\u80fd\u4f18\u5148\u7ea7\u6df7\u4e71\uff0c\u53ef\u4ee5\u6bcf\u6b21\u90fd\u52a0\u62ec\u53f7\uff0c\u4e5f\u53ef\u7528\u63d0\u4f9b\u7684`parenthesize`\u7b80\u5316\u5904\u7406\n\n## \u8d21\u732e\u4ee3\u7801\n\n1. \u8fd8\u6709\u5f88\u591a\u51fd\u6570\u6ca1\u6709\u6dfb\u52a0\uff0c\u9700\u8981\u5927\u5bb6\u63d0\u4ea4\u4ee3\u7801\u4e00\u8d77\u5b8c\u5584\n2. \u76ee\u524d\u8868\u8fbe\u5f0f\u6837\u5f0f\u4f18\u5148\u5411WorldQuant \u7684 Alpha101 \u9760\u9f50\n\n## \u5c0f\u6280\u5de7\n\n1. `sympy`\u4e0d\u652f\u6301`==`\uff0c\u800c\u662f\u5f53\u6210\u4e24\u4e2a\u5bf9\u8c61\u6bd4\u8f83\u3002\u4f8b\u5982\uff1a\n    1. `if_else(OPEN==CLOSE, HIGH, LOW)`, \u4e00\u5f00\u59cb\u5c31\u53d8\u6210\u4e86`if_else(False, HIGH, LOW)`\n    2. \u53ef\u4ee5\u7528`Eq`\u6765\u4ee3\u66ff\uff0c`if_else(Eq(OPEN, CLOSE), HIGH, LOW)`\u3002\u5177\u4f53\u793a\u4f8b\u8bf7\u53c2\u8003`Alpha101`\u4e2d\u7684`alpha_021`\n\n2. `sympy`\u4e0d\u652f\u6301`bool`\u8f6c`int`\u3002\u4f8b\u5982\uff1a\n    1. `(OPEN < CLOSE) * -1`\u62a5\u9519 `TypeError: unsupported operand type(s) for *: 'StrictLessThan' and 'int'`\n    2. \u53ef\u4ee5\u7528`if_else`\u4ee3\u66ff\u3002`if_else(OPEN<CLOSE, 1, 0)*-1`\u3002\u5177\u4f53\u793a\u4f8b\u8bf7\u53c2\u8003`Alpha101`\u4e2d\u7684`alpha_064`\n3. Python\u4e0d\u652f\u6301`?:`\u4e09\u5143\u8868\u8fbe\u5f0f\uff0c\u53ea\u652f\u6301`if else`, \u800c\u5728\u672c\u9879\u76ee\u4e2d\u9700\u8981\u8f6c\u6210`if_else`\n\n\u4ee5\u4e0a\u4e09\u79cd\u95ee\u9898\u672c\u9879\u76ee\u90fd\u4f7f\u7528`ast`\u8fdb\u884c\u4e86\u5904\u7406\uff0c\u53ef\u4ee5\u7b80\u5316\u4f7f\u7528\n\n## \u793a\u4f8b\u7247\u6bb5\n\n\u9700\u8981\u8f6c\u8bd1\u7684\u90e8\u5206\u516c\u5f0f\uff0c\u8be6\u7ec6\u4ee3\u7801\u8bf7\u53c2\u8003 [Demo](examples/demo_cn.py)\n\n```python\nexprs_src = {\n    \"expr_1\": -ts_corr(cs_rank(ts_mean(OPEN, 10)), cs_rank(ts_mean(CLOSE, 10)), 10),\n    \"expr_2\": cs_rank(ts_mean(OPEN, 10)) - abs_(log(ts_mean(CLOSE, 10))) + gp_rank(sw_l1, CLOSE),\n    \"expr_3\": ts_mean(cs_rank(ts_mean(OPEN, 10)), 10),\n    \"expr_4\": cs_rank(ts_mean(cs_rank(OPEN), 10)),\n    \"expr_5\": -ts_corr(OPEN, CLOSE, 10),\n}\n```\n\n\u8f6c\u8bd1\u540e\u7684\u4ee3\u7801\u7247\u6bb5\uff0c\u8be6\u7ec6\u4ee3\u7801\u8bf7\u53c2\u8003[Polars\u7248](codes)\n\n```python\ndef func_0_ts__asset(df: pl.DataFrame) -> pl.DataFrame:\n    df = df.sort(by=[_DATE_])\n    # ========================================\n    df = df.with_columns(\n        _x_0=1 / ts_delay(OPEN, -1),\n        LABEL_CC_1=(-CLOSE + ts_delay(CLOSE, -1)) / CLOSE,\n    )\n    # ========================================\n    df = df.with_columns(\n        LABEL_OO_1=_x_0 * ts_delay(OPEN, -2) - 1,\n        LABEL_OO_2=_x_0 * ts_delay(OPEN, -3) - 1,\n    )\n    return df\n```\n\n\u8f6c\u8bd1\u540e\u7684\u4ee3\u7801\u7247\u6bb5\uff0c\u8be6\u7ec6\u4ee3\u7801\u8bf7\u53c2\u8003[Pandas\u7248](examples/output_pandas.py)\n\n```python\ndef func_2_cs__date(df: pd.DataFrame) -> pd.DataFrame:\n    # expr_4 = cs_rank(x_7)\n    df[\"expr_4\"] = (df[\"x_7\"]).rank(pct=True)\n    return df\n\n\ndef func_3_ts__asset__date(df: pd.DataFrame) -> pd.DataFrame:\n    # expr_5 = -ts_corr(OPEN, CLOSE, 10)\n    df[\"expr_5\"] = -(df[\"OPEN\"]).rolling(10).corr(df[\"CLOSE\"])\n    # expr_6 = ts_delta(OPEN, 10)\n    df[\"expr_6\"] = df[\"OPEN\"].diff(10)\n    return df\n\n\ndf = df.sort_values(by=[\"asset\", \"date\"]).groupby(by=[\"asset\"], group_keys=False).apply(func_0_ts__asset__date)\ndf = df.groupby(by=[\"date\"], group_keys=False).apply(func_0_cs__date)\ndf = func_0_cl(df)\n```\n\n## \u672c\u5730\u90e8\u7f72\u4ea4\u4e92\u7f51\u9875\n\n\u53ea\u9700\u8fd0\u884c`streamlit run streamlit_app.py`\n",
    "bugtrack_url": null,
    "license": "BSD 3-Clause License  Copyright (c) 2023, wukan  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ",
    "summary": "symbol expression to polars expression tool",
    "version": "0.6.2",
    "project_urls": null,
    "split_keywords": [
        "polars",
        " expression",
        " talib"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "dd75c782da2d7c8f282951326654b41e5cd777da67a6eac1021d2b4a3909d516",
                "md5": "990fe0d9acd38ae84c15205180dcd3ce",
                "sha256": "a770551888e547a269e96a95a7bc417c6d85808127c067c40abec8f7ea6c2a42"
            },
            "downloads": -1,
            "filename": "expr_codegen-0.6.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "990fe0d9acd38ae84c15205180dcd3ce",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 32912,
            "upload_time": "2024-04-28T06:58:31",
            "upload_time_iso_8601": "2024-04-28T06:58:31.280667Z",
            "url": "https://files.pythonhosted.org/packages/dd/75/c782da2d7c8f282951326654b41e5cd777da67a6eac1021d2b4a3909d516/expr_codegen-0.6.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a79e5aa33f70f76b4f082aab2bda140fc6b09620927beea8378470274b3a8d2d",
                "md5": "6155ba18caf8b322e7ee03ae99f3b8ba",
                "sha256": "99e97bea80c37a95f79c62526d3fd4b54e144c6f3b009285b097f6cc2e03e327"
            },
            "downloads": -1,
            "filename": "expr_codegen-0.6.2.tar.gz",
            "has_sig": false,
            "md5_digest": "6155ba18caf8b322e7ee03ae99f3b8ba",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 28893,
            "upload_time": "2024-04-28T06:58:33",
            "upload_time_iso_8601": "2024-04-28T06:58:33.688667Z",
            "url": "https://files.pythonhosted.org/packages/a7/9e/5aa33f70f76b4f082aab2bda140fc6b09620927beea8378470274b3a8d2d/expr_codegen-0.6.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-28 06:58:33",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "expr-codegen"
}
        
Elapsed time: 0.24625s