alphainspect


Namealphainspect JSON
Version 0.3.1 PyPI version JSON
download
home_pageNone
Summaryfactor performance visualization
upload_time2024-04-18 03:38:05
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT License Copyright (c) 2024 wukan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords quant alpha performance visualization
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # AlphaInspect

仿`alphalens`的单因子分析工具

## 安装

```commandline
pip install -i https://pypi.org/simple --upgrade alphainspect  # 官方源
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade alphainspect  # 国内镜像源
```

## 使用

1. 准备数据。运行`data/prepare_data.py`
    1. date, asset。必需的两个字段
    2. factor因子值。放在因子发生时刻
    3. forward return远期收益率。计算收益率后需要回移到开始位置。为何是`shift(-n)收益率`,而不是`shift(n)因子`呢?
        1. 多期收期率。如果移动因子,会导致一个因子就要移动多次
        2. 因子一般成百上千,全移动要的工作量非常大,而收益率则少很多
    4. 推荐大家使用`expr_codegen`和`polars_ta`等项目
2. 运行`examples/demo1.py`示例弹出简易图表
3. 运行`examples/demo2.py`示例弹出完整图表
4. 运行`examples/demo3.py`示例多进程并行输出HTML网页报表
5. 运行`examples/demo4.py`示例事件图表

## 部分图示

![2x2](docs/img/2x2.png)
![3x2](docs/img/3x2.png)
![ic](docs/img/ic.png)
![returns](docs/img/returns.png)
![cum_returns](docs/img/cum_returns.png)
![spread](docs/img/spread.png)
![turnover](docs/img/turnover.png)
![events](docs/img/events.png)

## 累计收益的计算方法

参考 [cum_returns.md](cum_returns.md)

## `alphainspect`与`alphalens`的不同

1. 不自动计算`forward_returns`。
    1. `alphalens`设`periods=(1, 5, 10)`,然后内部计算持有1、5、10期数的收益率
    2. `alphainspect`由用户外部生成,用户可以比较同因子,不同交易方式产生的差异。例如:
        - `RETURN_OC_1`: T+1开盘入场,T+1收盘出场
        - `RETURN_CC_1`: T+0收盘入场,T+1收盘出场
        - `RETURN_OO_1`: T+1开盘入场,T+2开盘出场
        - `RETURN_OO_5`: T+1开盘入场,T+6开盘出场
2. 不做去极值、标准化、行业中性化等操作
    1. `alphalens`的各参数要弄懂还是很麻烦的,初学者如绩效达不到要求就得深入研究源代码找原因
    2. `alphainspect`用户在外可以一次性全计算好,如`F1_ORG, F1_ZS, F1_NEUT`,然后在分别传不同因子进行比较即可
3. 资金分配只用等权
    1. `alphalens`有因子加权、多空等设置
    2. `alphainspect`只提供等权一种计算方法,实现简单
4. 收益率计算方法不同
    1. `alphalens`多期简单收益率几何平均成1期,然后+1累乘
    2. `alphainspect`由用户提供1期简单收益率,然后根据要求持有或调仓,得到新的权益,循环迭代下去。更精确

## `alphainspect`与`alphalens`的相同

1. 数据组织方式相同。都是长表,都是因子不移动,收益率计算,然后后移到与因子产生时间对齐
2. 累计收益计算模式类似。每期产生因子,持有时长超过一期,都是将资金分成多份分别入场
3. 不考虑滑点和手续费。单因子是用来合成多因子的,因手续费和滑点而错过部分单因子就可惜了,应当在因子合成后的回测阶段才考虑手续费

## 二次开发

```commandline
git --clone https://github.com/wukan1986/alphainspect.git
cd alphainspect
pip install -e .
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "alphainspect",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "quant, alpha, performance, visualization",
    "author": null,
    "author_email": "wukan <wu-kan@163.com>",
    "download_url": "https://files.pythonhosted.org/packages/0c/4a/20bc2ba1c3e16b26ada4032c5061a8d965cd60d16444bedff7a5f9216f4f/alphainspect-0.3.1.tar.gz",
    "platform": null,
    "description": "# AlphaInspect\n\n\u4eff`alphalens`\u7684\u5355\u56e0\u5b50\u5206\u6790\u5de5\u5177\n\n## \u5b89\u88c5\n\n```commandline\npip install -i https://pypi.org/simple --upgrade alphainspect  # \u5b98\u65b9\u6e90\npip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade alphainspect  # \u56fd\u5185\u955c\u50cf\u6e90\n```\n\n## \u4f7f\u7528\n\n1. \u51c6\u5907\u6570\u636e\u3002\u8fd0\u884c`data/prepare_data.py`\n    1. date, asset\u3002\u5fc5\u9700\u7684\u4e24\u4e2a\u5b57\u6bb5\n    2. factor\u56e0\u5b50\u503c\u3002\u653e\u5728\u56e0\u5b50\u53d1\u751f\u65f6\u523b\n    3. forward return\u8fdc\u671f\u6536\u76ca\u7387\u3002\u8ba1\u7b97\u6536\u76ca\u7387\u540e\u9700\u8981\u56de\u79fb\u5230\u5f00\u59cb\u4f4d\u7f6e\u3002\u4e3a\u4f55\u662f`shift(-n)\u6536\u76ca\u7387`\uff0c\u800c\u4e0d\u662f`shift(n)\u56e0\u5b50`\u5462\uff1f\n        1. \u591a\u671f\u6536\u671f\u7387\u3002\u5982\u679c\u79fb\u52a8\u56e0\u5b50\uff0c\u4f1a\u5bfc\u81f4\u4e00\u4e2a\u56e0\u5b50\u5c31\u8981\u79fb\u52a8\u591a\u6b21\n        2. \u56e0\u5b50\u4e00\u822c\u6210\u767e\u4e0a\u5343\uff0c\u5168\u79fb\u52a8\u8981\u7684\u5de5\u4f5c\u91cf\u975e\u5e38\u5927\uff0c\u800c\u6536\u76ca\u7387\u5219\u5c11\u5f88\u591a\n    4. \u63a8\u8350\u5927\u5bb6\u4f7f\u7528`expr_codegen`\u548c`polars_ta`\u7b49\u9879\u76ee\n2. \u8fd0\u884c`examples/demo1.py`\u793a\u4f8b\u5f39\u51fa\u7b80\u6613\u56fe\u8868\n3. \u8fd0\u884c`examples/demo2.py`\u793a\u4f8b\u5f39\u51fa\u5b8c\u6574\u56fe\u8868\n4. \u8fd0\u884c`examples/demo3.py`\u793a\u4f8b\u591a\u8fdb\u7a0b\u5e76\u884c\u8f93\u51faHTML\u7f51\u9875\u62a5\u8868\n5. \u8fd0\u884c`examples/demo4.py`\u793a\u4f8b\u4e8b\u4ef6\u56fe\u8868\n\n## \u90e8\u5206\u56fe\u793a\n\n![2x2](docs/img/2x2.png)\n![3x2](docs/img/3x2.png)\n![ic](docs/img/ic.png)\n![returns](docs/img/returns.png)\n![cum_returns](docs/img/cum_returns.png)\n![spread](docs/img/spread.png)\n![turnover](docs/img/turnover.png)\n![events](docs/img/events.png)\n\n## \u7d2f\u8ba1\u6536\u76ca\u7684\u8ba1\u7b97\u65b9\u6cd5\n\n\u53c2\u8003 [cum_returns.md](cum_returns.md)\n\n## `alphainspect`\u4e0e`alphalens`\u7684\u4e0d\u540c\n\n1. \u4e0d\u81ea\u52a8\u8ba1\u7b97`forward_returns`\u3002\n    1. `alphalens`\u8bbe`periods=(1, 5, 10)`\uff0c\u7136\u540e\u5185\u90e8\u8ba1\u7b97\u6301\u67091\u30015\u300110\u671f\u6570\u7684\u6536\u76ca\u7387\n    2. `alphainspect`\u7531\u7528\u6237\u5916\u90e8\u751f\u6210\uff0c\u7528\u6237\u53ef\u4ee5\u6bd4\u8f83\u540c\u56e0\u5b50\uff0c\u4e0d\u540c\u4ea4\u6613\u65b9\u5f0f\u4ea7\u751f\u7684\u5dee\u5f02\u3002\u4f8b\u5982\uff1a\n        - `RETURN_OC_1`: T+1\u5f00\u76d8\u5165\u573a\uff0cT+1\u6536\u76d8\u51fa\u573a\n        - `RETURN_CC_1`: T+0\u6536\u76d8\u5165\u573a\uff0cT+1\u6536\u76d8\u51fa\u573a\n        - `RETURN_OO_1`: T+1\u5f00\u76d8\u5165\u573a\uff0cT+2\u5f00\u76d8\u51fa\u573a\n        - `RETURN_OO_5`: T+1\u5f00\u76d8\u5165\u573a\uff0cT+6\u5f00\u76d8\u51fa\u573a\n2. \u4e0d\u505a\u53bb\u6781\u503c\u3001\u6807\u51c6\u5316\u3001\u884c\u4e1a\u4e2d\u6027\u5316\u7b49\u64cd\u4f5c\n    1. `alphalens`\u7684\u5404\u53c2\u6570\u8981\u5f04\u61c2\u8fd8\u662f\u5f88\u9ebb\u70e6\u7684\uff0c\u521d\u5b66\u8005\u5982\u7ee9\u6548\u8fbe\u4e0d\u5230\u8981\u6c42\u5c31\u5f97\u6df1\u5165\u7814\u7a76\u6e90\u4ee3\u7801\u627e\u539f\u56e0\n    2. `alphainspect`\u7528\u6237\u5728\u5916\u53ef\u4ee5\u4e00\u6b21\u6027\u5168\u8ba1\u7b97\u597d\uff0c\u5982`F1_ORG, F1_ZS, F1_NEUT`\uff0c\u7136\u540e\u5728\u5206\u522b\u4f20\u4e0d\u540c\u56e0\u5b50\u8fdb\u884c\u6bd4\u8f83\u5373\u53ef\n3. \u8d44\u91d1\u5206\u914d\u53ea\u7528\u7b49\u6743\n    1. `alphalens`\u6709\u56e0\u5b50\u52a0\u6743\u3001\u591a\u7a7a\u7b49\u8bbe\u7f6e\n    2. `alphainspect`\u53ea\u63d0\u4f9b\u7b49\u6743\u4e00\u79cd\u8ba1\u7b97\u65b9\u6cd5\uff0c\u5b9e\u73b0\u7b80\u5355\n4. \u6536\u76ca\u7387\u8ba1\u7b97\u65b9\u6cd5\u4e0d\u540c\n    1. `alphalens`\u591a\u671f\u7b80\u5355\u6536\u76ca\u7387\u51e0\u4f55\u5e73\u5747\u62101\u671f\uff0c\u7136\u540e+1\u7d2f\u4e58\n    2. `alphainspect`\u7531\u7528\u6237\u63d0\u4f9b1\u671f\u7b80\u5355\u6536\u76ca\u7387\uff0c\u7136\u540e\u6839\u636e\u8981\u6c42\u6301\u6709\u6216\u8c03\u4ed3\uff0c\u5f97\u5230\u65b0\u7684\u6743\u76ca\uff0c\u5faa\u73af\u8fed\u4ee3\u4e0b\u53bb\u3002\u66f4\u7cbe\u786e\n\n## `alphainspect`\u4e0e`alphalens`\u7684\u76f8\u540c\n\n1. \u6570\u636e\u7ec4\u7ec7\u65b9\u5f0f\u76f8\u540c\u3002\u90fd\u662f\u957f\u8868\uff0c\u90fd\u662f\u56e0\u5b50\u4e0d\u79fb\u52a8\uff0c\u6536\u76ca\u7387\u8ba1\u7b97\uff0c\u7136\u540e\u540e\u79fb\u5230\u4e0e\u56e0\u5b50\u4ea7\u751f\u65f6\u95f4\u5bf9\u9f50\n2. \u7d2f\u8ba1\u6536\u76ca\u8ba1\u7b97\u6a21\u5f0f\u7c7b\u4f3c\u3002\u6bcf\u671f\u4ea7\u751f\u56e0\u5b50\uff0c\u6301\u6709\u65f6\u957f\u8d85\u8fc7\u4e00\u671f\uff0c\u90fd\u662f\u5c06\u8d44\u91d1\u5206\u6210\u591a\u4efd\u5206\u522b\u5165\u573a\n3. \u4e0d\u8003\u8651\u6ed1\u70b9\u548c\u624b\u7eed\u8d39\u3002\u5355\u56e0\u5b50\u662f\u7528\u6765\u5408\u6210\u591a\u56e0\u5b50\u7684\uff0c\u56e0\u624b\u7eed\u8d39\u548c\u6ed1\u70b9\u800c\u9519\u8fc7\u90e8\u5206\u5355\u56e0\u5b50\u5c31\u53ef\u60dc\u4e86\uff0c\u5e94\u5f53\u5728\u56e0\u5b50\u5408\u6210\u540e\u7684\u56de\u6d4b\u9636\u6bb5\u624d\u8003\u8651\u624b\u7eed\u8d39\n\n## \u4e8c\u6b21\u5f00\u53d1\n\n```commandline\ngit --clone https://github.com/wukan1986/alphainspect.git\ncd alphainspect\npip install -e .\n```\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 wukan  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "factor performance visualization",
    "version": "0.3.1",
    "project_urls": null,
    "split_keywords": [
        "quant",
        " alpha",
        " performance",
        " visualization"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "87d8f030ca63a23293ab2b53418bebbb7d311b2a40f336e9eb64e7b5b6e4424e",
                "md5": "65f631d6e7a583ba0c4e6347812f28f9",
                "sha256": "588792c4737d51750baa16c082298180421b76b5c7880324e441099f8b98c851"
            },
            "downloads": -1,
            "filename": "alphainspect-0.3.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "65f631d6e7a583ba0c4e6347812f28f9",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 25113,
            "upload_time": "2024-04-18T03:38:04",
            "upload_time_iso_8601": "2024-04-18T03:38:04.058510Z",
            "url": "https://files.pythonhosted.org/packages/87/d8/f030ca63a23293ab2b53418bebbb7d311b2a40f336e9eb64e7b5b6e4424e/alphainspect-0.3.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0c4a20bc2ba1c3e16b26ada4032c5061a8d965cd60d16444bedff7a5f9216f4f",
                "md5": "efa58fdd6fdc5ddb4f232f3627a707a9",
                "sha256": "a90792a45466d7f941727608056fa618651af8fca037e3498691c46835c19441"
            },
            "downloads": -1,
            "filename": "alphainspect-0.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "efa58fdd6fdc5ddb4f232f3627a707a9",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 22002,
            "upload_time": "2024-04-18T03:38:05",
            "upload_time_iso_8601": "2024-04-18T03:38:05.709545Z",
            "url": "https://files.pythonhosted.org/packages/0c/4a/20bc2ba1c3e16b26ada4032c5061a8d965cd60d16444bedff7a5f9216f4f/alphainspect-0.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-18 03:38:05",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "alphainspect"
}
        
Elapsed time: 0.27145s