QuantDigger 旨在为量化交易研究者提供一个简洁、灵活的回测环境,支持 股票、期货回测,并涵盖 选股、套利、择时、组合策略 等应用场景。它的策略语法设计参考了主流量化软件,使用户可以快速上手,同时利用 Python 的强大数据处理能力克服传统软件的局限性。
核心特点
-
简洁的策略语法:比 Zipline、Pyalgotrade 更符合交易员习惯。
-
支持多种回测功能:包括股票、期货回测,支持选股、套利、择时、组合策略。
-
图形化界面:基于
matplotlib,提供直观的策略和 K 线可视化。 -
兼顾实盘交易:未来可能加入交易接口。
-
强大的 Python 生态支持:兼容 Pandas、Numpy、Matplotlib 等数据分析工具。
二、安装与环境配置
QuantDigger 依赖多个 Python 库,如 matplotlib、numpy、pandas 等,建议使用 pip 或 conda 进行安装。
1. 安装 QuantDigger
pip install git+https://github.com/QuantFans/quantdigger.git
如果遇到 TA-Lib 相关错误,可以先安装 TA-Lib:
conda install -c quantopian ta-lib
2. 配置环境
import quantdiggerprint("QuantDigger 安装成功!")
三、快速上手:实现一个简单的交易策略
下面,我们将使用 QuantDigger 编写一个简单的均线策略,并回测其在 A 股市场的表现。
1. 策略逻辑
-
当短期均线(5 日)上穿长期均线(20 日)时,买入。
-
当短期均线下穿长期均线时,卖出。
2. 完整代码示例
from quantdigger import *class DemoStrategy(Strategy):def on_init(self, ctx):ctx.short_ma = MA(ctx.close, 5, 'short_ma', plot=True)ctx.long_ma = MA(ctx.close, 20, 'long_ma', plot=True)def on_bar(self, ctx):if ctx.short_ma[0] > ctx.long_ma[0]:ctx.buy()elif ctx.short_ma[0] < ctx.long_ma[0]:ctx.sell()set_symbols(['000001.SZ'], timeframe='D1')add_strategy([DemoStrategy(name='均线策略')])run()
3. 策略回测结果
执行上述代码后,QuantDigger 会自动绘制回测结果,包括买卖点标记、资金曲线、收益统计等,帮助交易者评估策略效果。
四、更多高级功能
1. 组合策略
QuantDigger 支持多个策略的组合回测,例如同时运行均线策略和动量策略。
add_strategy([DemoStrategy(name='均线策略'), MomentumStrategy(name='动量策略')])
2. 交易信号可视化
ctx.signal.plot()
3. 选股与套利策略
from quantdigger import (Strategy,MA,DateTimeSeries,NumberSeries,set_config,add_strategies,Profile)class DemoStrategy(Strategy):""" 策略A1 """def on_init(self, ctx):"""初始化数据"""ctx.ma10 = MA(ctx.close, 10, 'ma10', 'y', 1)ctx.ma20 = MA(ctx.close, 20, 'ma20', 'b', 1)ctx.dt = DateTimeSeries()ctx.month_price = NumberSeries()def on_bar(self, ctx):ctx.dt.update(ctx.datetime)if ctx.dt[1].month != ctx.dt[0].month:ctx.month_price.update(ctx.close)if ctx.curbar > 20:if ctx.pos() == 0 and ctx.ma10[2] < ctx.ma20[2] and ctx.ma10[1] > ctx.ma20[1]:ctx.buy(ctx.close, 1)ctx.plot_text("buy", 1, ctx.curbar, ctx.close, "buy", 'black', 15)elif ctx.pos() > 0 and ctx.ma10[2] > ctx.ma20[2] andctx.ma10[1] < ctx.ma20[1]:ctx.plot_text("sell", 1, ctx.curbar, ctx.close, "sell", 'blue', 15)ctx.sell(ctx.close, ctx.pos())ctx.plot_line("month_price", 1, ctx.curbar, ctx.month_price, 'y--', lw=2)returndef on_exit(self, ctx):returnclass DemoStrategy2(Strategy):""" 策略A2 """def on_init(self, ctx):"""初始化数据"""ctx.ma50 = MA(ctx.close, 50, 'ma50', 'y', 2)ctx.ma100 = MA(ctx.close, 100, 'ma100', 'black', 2)def on_symbol(self, ctx):passdef on_bar(self, ctx):if ctx.curbar > 100:if ctx.pos() == 0 and ctx.ma50[2] < ctx.ma100[2] and ctx.ma50[1] > ctx.ma100[1]:ctx.buy(ctx.close, 1)elif ctx.pos() > 0 and ctx.ma50[2] > ctx.ma100[2] andctx.ma50[1] < ctx.ma100[1]:ctx.sell(ctx.close, ctx.pos())returndef on_exit(self, ctx):returnif __name__ == '__main__':import timeitstart = timeit.default_timer()set_config({'source': 'csv'})profiles = add_strategies(['BB.SHFE-1.Day'], [{'strategy': DemoStrategy('A1'),'capital': 50000.0 * 0.5,},{'strategy': DemoStrategy2('A2'),'capital': 50000.0 * 0.5,}])stop = timeit.default_timer()print("运行耗时: %d秒" % ((stop - start)))# 绘制k线,交易信号线from quantdigger.digger import finance, plottings = 0# 绘制策略A1, 策略A2, 组合的资金曲线curve0 = finance.create_equity_curve(profiles[0].all_holdings())curve1 = finance.create_equity_curve(profiles[1].all_holdings())curve = finance.create_equity_curve(Profile.all_holdings_sum(profiles))plotting.plot_strategy(profiles[0].data(), profiles[0].technicals(),profiles[0].deals(), curve0.equity.values,profiles[0].marks())# 绘制净值曲线plotting.plot_curves([curve.networth])# 打印统计信息print(finance.summary_stats(curve, 252))
-
k线和信号线

-
2个策略和组合的资金曲线。

五、总结
QuantDigger 作为一款专注于量化交易回测的开源框架,凭借其简洁语法、高效回测、多策略支持等特点,成为量化研究者不可多得的工具。无论你是初学者,还是希望在 Python 生态下进行回测的资深交易者,QuantDigger 都能助你一臂之力。
项目链接
-
GitHub:https://github.com/QuantFans/quantdigger
-
文档:https://quantdigger.readthedocs.io











