vnrs


Namevnrs JSON
Version 0.1.0 PyPI version JSON
download
home_pageNone
Summaryvnpy backtest module written in Rust and Python
upload_time2025-01-09 04:11:31
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords quant quantitative investment trading
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # vnpyrs 和vnpy完全兼容但更快的回测框架

vnpyrs是以提升性能为目的,部分代码用Rust语言重新实现的vnpy。已实现回测和参数调优。

## 背景

众所周知,Python生态强大,编码灵活,但是有个缺点,就是慢。2024年,我实验性的用Rust重写了vnpy的回测模块,惊喜的发现,运行速度提升了近20倍。
显然,移植到Rust这件事的价值是巨大的。但还有个问题,由于它100%是用Rust编写的,包括策略也是用Rust编写的,那用户就无法使用Python的各种库了。况且,Rust是出了名的难入门的语言。上述的两点会降低它的实用性。
因此,在2025年我重新设定了目标,该项目必须完全兼容Python生态,100%兼容已经为vnpy编写的策略,同时要提升性能。
由于用户代码是用Python编写的,那性能提升就不会像第一次用纯Rust编写时那样夸张了,但省下的时间依然不少。如果用examples文件夹里面的案例测试,在不改一行策略代码的情况下,综合速度提升了一倍。(具体提升多少和策略的具体实现紧密相关)

## 环境准备

vnpyrs对python包的依赖和vnpy几乎一样,但去掉了UI相关的包。Python版本需要3.8以上,推荐3.10以上

## 安装步骤

**Windows**

```
pip install vnrs
```

**Linux**

```
pip install vnrs
```

**Macos**

需要git clone并源码编译


## 脚本运行

vnpyrs仅支持脚本运行,在任意目录下创建run.py,写入以下示例代码:

```Python
from vnrs.backtesting import BacktestingEngine, BacktestingMode
from vnrs.trader.optimize import OptimizationSetting

#double_ma_strategy模块是vnpy自带的,复制到当前目录即可。对策略的导入必须在导入vnrs之后,除非将其中的vnpy_ctastrategy替换为vnrs
from double_ma_strategy import DoubleMaStrategy

from datetime import datetime
import time

def main():
    engine=BacktestingEngine()

    engine.set_parameters(vt_symbol="300.LOCAL",
                        interval="1m",
                        start=datetime(2009,1,5),
                        end=datetime(2020,8,18),
                        rate=2.3e-5,
                        slippage=0.2,
                        size=1,
                        pricetick=0.2,
                        capital=10000000)

    start = time.perf_counter()
    engine.add_strategy(DoubleMaStrategy,{'fast_window': 10, 'slow_window': 20})
    engine.load_data()
    engine.run_backtesting()
    df=engine.calculate_result()
    print(df)
    engine.calculate_statistics()
    engine.show_chart().show()

    setting=OptimizationSetting()
    setting.set_target("sharpe_ratio")
    setting.add_parameter("fast_window",8,12,1)
    setting.add_parameter("slow_window",12,20,1)
    engine.run_bf_optimization(setting)
    engine.run_ga_optimization(setting)
    print(time.perf_counter()-start) #这里用于统计耗时,可以看到vnpyrs耗时只有vnpy的一半

if __name__ == '__main__':
    main()
```

vnpyrs使用的数据库和json配置文件和vnpy完全一样,二者是共用数据库的。标的300.LOCAL的数据在examples下,导入300_1min_vnpy.csv到vnpy即可。
在该目录下打开CMD(按住Shift->点击鼠标右键->在此处打开命令窗口/PowerShell)后运行下列命令启动vnpyrs:
    python run.py


## 开发路线图

不会完全移植vnpy。
一个原因是vnpy用到了很多动态语言的特性,以实现插件化,这部分在静态语言上实现很困难,或者需要大量的unsafe;
另一个原因是没必要,UI和网络模块即使用Rust、C++重写,体验也不会明显变好。
但是未来会支持用Rust、C、C++写策略,这样的话回测性能提升10倍也是有可能的。

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "vnrs",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "quant, quantitative, investment, trading",
    "author": null,
    "author_email": "Liehan Wu <vnpyrs@hotmail.com>",
    "download_url": null,
    "platform": null,
    "description": "# vnpyrs \u548cvnpy\u5b8c\u5168\u517c\u5bb9\u4f46\u66f4\u5feb\u7684\u56de\u6d4b\u6846\u67b6\r\n\r\nvnpyrs\u662f\u4ee5\u63d0\u5347\u6027\u80fd\u4e3a\u76ee\u7684\uff0c\u90e8\u5206\u4ee3\u7801\u7528Rust\u8bed\u8a00\u91cd\u65b0\u5b9e\u73b0\u7684vnpy\u3002\u5df2\u5b9e\u73b0\u56de\u6d4b\u548c\u53c2\u6570\u8c03\u4f18\u3002\r\n\r\n## \u80cc\u666f\r\n\r\n\u4f17\u6240\u5468\u77e5\uff0cPython\u751f\u6001\u5f3a\u5927\uff0c\u7f16\u7801\u7075\u6d3b\uff0c\u4f46\u662f\u6709\u4e2a\u7f3a\u70b9\uff0c\u5c31\u662f\u6162\u30022024\u5e74\uff0c\u6211\u5b9e\u9a8c\u6027\u7684\u7528Rust\u91cd\u5199\u4e86vnpy\u7684\u56de\u6d4b\u6a21\u5757\uff0c\u60ca\u559c\u7684\u53d1\u73b0\uff0c\u8fd0\u884c\u901f\u5ea6\u63d0\u5347\u4e86\u8fd120\u500d\u3002\r\n\u663e\u7136\uff0c\u79fb\u690d\u5230Rust\u8fd9\u4ef6\u4e8b\u7684\u4ef7\u503c\u662f\u5de8\u5927\u7684\u3002\u4f46\u8fd8\u6709\u4e2a\u95ee\u9898\uff0c\u7531\u4e8e\u5b83100%\u662f\u7528Rust\u7f16\u5199\u7684\uff0c\u5305\u62ec\u7b56\u7565\u4e5f\u662f\u7528Rust\u7f16\u5199\u7684\uff0c\u90a3\u7528\u6237\u5c31\u65e0\u6cd5\u4f7f\u7528Python\u7684\u5404\u79cd\u5e93\u4e86\u3002\u51b5\u4e14\uff0cRust\u662f\u51fa\u4e86\u540d\u7684\u96be\u5165\u95e8\u7684\u8bed\u8a00\u3002\u4e0a\u8ff0\u7684\u4e24\u70b9\u4f1a\u964d\u4f4e\u5b83\u7684\u5b9e\u7528\u6027\u3002\r\n\u56e0\u6b64\uff0c\u57282025\u5e74\u6211\u91cd\u65b0\u8bbe\u5b9a\u4e86\u76ee\u6807\uff0c\u8be5\u9879\u76ee\u5fc5\u987b\u5b8c\u5168\u517c\u5bb9Python\u751f\u6001\uff0c100%\u517c\u5bb9\u5df2\u7ecf\u4e3avnpy\u7f16\u5199\u7684\u7b56\u7565\uff0c\u540c\u65f6\u8981\u63d0\u5347\u6027\u80fd\u3002\r\n\u7531\u4e8e\u7528\u6237\u4ee3\u7801\u662f\u7528Python\u7f16\u5199\u7684\uff0c\u90a3\u6027\u80fd\u63d0\u5347\u5c31\u4e0d\u4f1a\u50cf\u7b2c\u4e00\u6b21\u7528\u7eafRust\u7f16\u5199\u65f6\u90a3\u6837\u5938\u5f20\u4e86\uff0c\u4f46\u7701\u4e0b\u7684\u65f6\u95f4\u4f9d\u7136\u4e0d\u5c11\u3002\u5982\u679c\u7528examples\u6587\u4ef6\u5939\u91cc\u9762\u7684\u6848\u4f8b\u6d4b\u8bd5\uff0c\u5728\u4e0d\u6539\u4e00\u884c\u7b56\u7565\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u7efc\u5408\u901f\u5ea6\u63d0\u5347\u4e86\u4e00\u500d\u3002\uff08\u5177\u4f53\u63d0\u5347\u591a\u5c11\u548c\u7b56\u7565\u7684\u5177\u4f53\u5b9e\u73b0\u7d27\u5bc6\u76f8\u5173\uff09\r\n\r\n## \u73af\u5883\u51c6\u5907\r\n\r\nvnpyrs\u5bf9python\u5305\u7684\u4f9d\u8d56\u548cvnpy\u51e0\u4e4e\u4e00\u6837\uff0c\u4f46\u53bb\u6389\u4e86UI\u76f8\u5173\u7684\u5305\u3002Python\u7248\u672c\u9700\u89813.8\u4ee5\u4e0a\uff0c\u63a8\u83503.10\u4ee5\u4e0a\r\n\r\n## \u5b89\u88c5\u6b65\u9aa4\r\n\r\n**Windows**\r\n\r\n```\r\npip install vnrs\r\n```\r\n\r\n**Linux**\r\n\r\n```\r\npip install vnrs\r\n```\r\n\r\n**Macos**\r\n\r\n\u9700\u8981git clone\u5e76\u6e90\u7801\u7f16\u8bd1\r\n\r\n\r\n## \u811a\u672c\u8fd0\u884c\r\n\r\nvnpyrs\u4ec5\u652f\u6301\u811a\u672c\u8fd0\u884c\uff0c\u5728\u4efb\u610f\u76ee\u5f55\u4e0b\u521b\u5efarun.py\uff0c\u5199\u5165\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\uff1a\r\n\r\n```Python\r\nfrom vnrs.backtesting import BacktestingEngine, BacktestingMode\r\nfrom vnrs.trader.optimize import OptimizationSetting\r\n\r\n#double_ma_strategy\u6a21\u5757\u662fvnpy\u81ea\u5e26\u7684\uff0c\u590d\u5236\u5230\u5f53\u524d\u76ee\u5f55\u5373\u53ef\u3002\u5bf9\u7b56\u7565\u7684\u5bfc\u5165\u5fc5\u987b\u5728\u5bfc\u5165vnrs\u4e4b\u540e\uff0c\u9664\u975e\u5c06\u5176\u4e2d\u7684vnpy_ctastrategy\u66ff\u6362\u4e3avnrs\r\nfrom double_ma_strategy import DoubleMaStrategy\r\n\r\nfrom datetime import datetime\r\nimport time\r\n\r\ndef main():\r\n    engine=BacktestingEngine()\r\n\r\n    engine.set_parameters(vt_symbol=\"300.LOCAL\",\r\n                        interval=\"1m\",\r\n                        start=datetime(2009,1,5),\r\n                        end=datetime(2020,8,18),\r\n                        rate=2.3e-5,\r\n                        slippage=0.2,\r\n                        size=1,\r\n                        pricetick=0.2,\r\n                        capital=10000000)\r\n\r\n    start = time.perf_counter()\r\n    engine.add_strategy(DoubleMaStrategy,{'fast_window': 10, 'slow_window': 20})\r\n    engine.load_data()\r\n    engine.run_backtesting()\r\n    df=engine.calculate_result()\r\n    print(df)\r\n    engine.calculate_statistics()\r\n    engine.show_chart().show()\r\n\r\n    setting=OptimizationSetting()\r\n    setting.set_target(\"sharpe_ratio\")\r\n    setting.add_parameter(\"fast_window\",8,12,1)\r\n    setting.add_parameter(\"slow_window\",12,20,1)\r\n    engine.run_bf_optimization(setting)\r\n    engine.run_ga_optimization(setting)\r\n    print(time.perf_counter()-start) #\u8fd9\u91cc\u7528\u4e8e\u7edf\u8ba1\u8017\u65f6\uff0c\u53ef\u4ee5\u770b\u5230vnpyrs\u8017\u65f6\u53ea\u6709vnpy\u7684\u4e00\u534a\r\n\r\nif __name__ == '__main__':\r\n    main()\r\n```\r\n\r\nvnpyrs\u4f7f\u7528\u7684\u6570\u636e\u5e93\u548cjson\u914d\u7f6e\u6587\u4ef6\u548cvnpy\u5b8c\u5168\u4e00\u6837\uff0c\u4e8c\u8005\u662f\u5171\u7528\u6570\u636e\u5e93\u7684\u3002\u6807\u7684300.LOCAL\u7684\u6570\u636e\u5728examples\u4e0b\uff0c\u5bfc\u5165300_1min_vnpy.csv\u5230vnpy\u5373\u53ef\u3002\r\n\u5728\u8be5\u76ee\u5f55\u4e0b\u6253\u5f00CMD\uff08\u6309\u4f4fShift->\u70b9\u51fb\u9f20\u6807\u53f3\u952e->\u5728\u6b64\u5904\u6253\u5f00\u547d\u4ee4\u7a97\u53e3/PowerShell\uff09\u540e\u8fd0\u884c\u4e0b\u5217\u547d\u4ee4\u542f\u52a8vnpyrs\uff1a\r\n    python run.py\r\n\r\n\r\n## \u5f00\u53d1\u8def\u7ebf\u56fe\r\n\r\n\u4e0d\u4f1a\u5b8c\u5168\u79fb\u690dvnpy\u3002\r\n\u4e00\u4e2a\u539f\u56e0\u662fvnpy\u7528\u5230\u4e86\u5f88\u591a\u52a8\u6001\u8bed\u8a00\u7684\u7279\u6027\uff0c\u4ee5\u5b9e\u73b0\u63d2\u4ef6\u5316\uff0c\u8fd9\u90e8\u5206\u5728\u9759\u6001\u8bed\u8a00\u4e0a\u5b9e\u73b0\u5f88\u56f0\u96be\uff0c\u6216\u8005\u9700\u8981\u5927\u91cf\u7684unsafe\uff1b\r\n\u53e6\u4e00\u4e2a\u539f\u56e0\u662f\u6ca1\u5fc5\u8981\uff0cUI\u548c\u7f51\u7edc\u6a21\u5757\u5373\u4f7f\u7528Rust\u3001C++\u91cd\u5199\uff0c\u4f53\u9a8c\u4e5f\u4e0d\u4f1a\u660e\u663e\u53d8\u597d\u3002\r\n\u4f46\u662f\u672a\u6765\u4f1a\u652f\u6301\u7528Rust\u3001C\u3001C++\u5199\u7b56\u7565\uff0c\u8fd9\u6837\u7684\u8bdd\u56de\u6d4b\u6027\u80fd\u63d0\u534710\u500d\u4e5f\u662f\u6709\u53ef\u80fd\u7684\u3002\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "vnpy backtest module written in Rust and Python",
    "version": "0.1.0",
    "project_urls": {
        "Changelog": "https://github.com/vnpyrs/vnpyrs/releases",
        "Documentation": "https://github.com/vnpyrs/vnpyrs",
        "Homepage": "https://github.com/vnpyrs/vnpyrs",
        "Repository": "https://github.com/vnpyrs/vnpyrs"
    },
    "split_keywords": [
        "quant",
        " quantitative",
        " investment",
        " trading"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "49b105653a438c098f480fc62c27f14c69e61313edd2f297545bb46a6f99cb55",
                "md5": "4cdb628bddda8433313bcb3c901cf5f1",
                "sha256": "0bb17d75d6ad7ffc6de768812145de154b1affa360adb9f7480a30f83b938824"
            },
            "downloads": -1,
            "filename": "vnrs-0.1.0-cp310-cp310-manylinux_2_34_x86_64.whl",
            "has_sig": false,
            "md5_digest": "4cdb628bddda8433313bcb3c901cf5f1",
            "packagetype": "bdist_wheel",
            "python_version": "cp310",
            "requires_python": ">=3.8",
            "size": 2169976,
            "upload_time": "2025-01-09T04:11:31",
            "upload_time_iso_8601": "2025-01-09T04:11:31.017079Z",
            "url": "https://files.pythonhosted.org/packages/49/b1/05653a438c098f480fc62c27f14c69e61313edd2f297545bb46a6f99cb55/vnrs-0.1.0-cp310-cp310-manylinux_2_34_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "c4d0d4fb820c6a4f588153acb90c220a43e1b7f56ee136e070589aad35992ebe",
                "md5": "cf84c56cd754a27d75a7fd2783f4710f",
                "sha256": "897677f40971d373776dff96dfcff17f1d0e705325f69951cfc77af17038d598"
            },
            "downloads": -1,
            "filename": "vnrs-0.1.0-cp311-cp311-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "cf84c56cd754a27d75a7fd2783f4710f",
            "packagetype": "bdist_wheel",
            "python_version": "cp311",
            "requires_python": ">=3.8",
            "size": 1819635,
            "upload_time": "2025-01-09T03:37:45",
            "upload_time_iso_8601": "2025-01-09T03:37:45.274304Z",
            "url": "https://files.pythonhosted.org/packages/c4/d0/d4fb820c6a4f588153acb90c220a43e1b7f56ee136e070589aad35992ebe/vnrs-0.1.0-cp311-cp311-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-09 04:11:31",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "vnpyrs",
    "github_project": "vnpyrs",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "vnrs"
}
        
Elapsed time: 0.47840s