Skip to main content

vnpy backtest module written in Rust and Python

Project description

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,写入以下示例代码:

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倍也是有可能的。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

vnrs-0.1.0-cp311-cp311-win_amd64.whl (1.8 MB view details)

Uploaded CPython 3.11Windows x86-64

vnrs-0.1.0-cp310-cp310-manylinux_2_34_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

File details

Details for the file vnrs-0.1.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: vnrs-0.1.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.7.8

File hashes

Hashes for vnrs-0.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 897677f40971d373776dff96dfcff17f1d0e705325f69951cfc77af17038d598
MD5 cf84c56cd754a27d75a7fd2783f4710f
BLAKE2b-256 c4d0d4fb820c6a4f588153acb90c220a43e1b7f56ee136e070589aad35992ebe

See more details on using hashes here.

File details

Details for the file vnrs-0.1.0-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for vnrs-0.1.0-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 0bb17d75d6ad7ffc6de768812145de154b1affa360adb9f7480a30f83b938824
MD5 4cdb628bddda8433313bcb3c901cf5f1
BLAKE2b-256 49b105653a438c098f480fc62c27f14c69e61313edd2f297545bb46a6f99cb55

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page