策略回测
运行量化策略回测并获取绩效分析
Documentation Index
Fetch the complete documentation index at: https://docs.reportify.cn/llms.txt
Use this file to discover all available pages before exploring further.
运行策略回测
执行策略回测,返回绩效指标和交易记录。请求参数
YYYY-MM-DDYYYY-MM-DDcn(A股), hk(港股), us(美股)filter_formula 过滤不符合条件的股票。strategy_code)。支持技术指标与基本面因子。handle_data(context, datas) 的函数作为回测引擎的入口点。buy_commission / sell_commission,则以后者为准commission(如 0.0003 表示万三)commission(如 0.0013 表示万三佣金 + 千一印花税)null(不限制)null(不限制)false 为 T+1 开盘执行strategy_code 模式下提前计算买卖所需的指标,或在回测中返回额外的值(可选)。可通过 factors 接口获取可用因子列表。benchmark 必须是对象,不能直接传字符串代码。响应参数
响应示例
策略公式示例
均线金叉死叉策略
5 日均线上穿 10 日均线时买入,下穿时卖出:RSI 超买超卖策略
RSI 低于 30 时买入,高于 70 时卖出:MACD 金叉死叉策略
MACD DIF 上穿 DEA 时买入,下穿时卖出:布林带突破策略
价格突破布林带上轨时买入,跌破下轨时卖出:组合条件策略
RSI 超卖且价格在 20 日均线上方买入,RSI 超买或价格跌破均线卖出:当前市场选股与过滤策略
如果不指定symbols,系统将在当前市场(由 market 字段决定,默认为A股 cn)执行回测。可以利用 filter_formula 在当前市场范围内进行过滤筛选,排除不满足基本面或状态要求的股票:
带 signal_factors 和自定义策略的回测
使用signal_factors 预计算因子并在自定义策略 strategy_code 中调用:
参数说明
佣金设置
| 参数 | 值 | 描述 |
|---|---|---|
commission | 0 | 不计算佣金 |
commission | 0.0003 | 买卖统一万三(0.03%) |
buy_commission | 0.0003 | 买入万三 |
sell_commission | 0.0013 | 卖出万三佣金 + 千一印花税(A股推荐) |
min_commission_amount | 5 | 每笔最低 5 元手续费 |
滑点设置
| 值 | 描述 |
|---|---|
0 | 不考虑滑点 |
0.001 | 千一滑点(买入价上浮 0.1%,卖出价下浮 0.1%) |
0.002 | 千二滑点 |
止损设置
| 值 | 描述 |
|---|---|
0 | 不设止损 |
0.05 | 5% 止损 |
0.1 | 10% 止损 |
仓位设置
| 值 | 描述 |
|---|---|
0.99 | 使用 99% 资金(预留部分应对价格波动) |
0.5 | 使用 50% 资金(半仓操作) |
0.3 | 使用 30% 资金(轻仓试探) |
上传 Excel 文件进行回测
上传包含 OHLCV 数据的 Excel 文件进行回测,无需依赖系统内置数据源,使用自有行情数据执行策略回测。multipart/form-data 格式上传,不支持 JSON body。Excel 文件格式要求
.xlsx 或 .xls 格式),包含回测所需的行情数据。必须包含的列:| 列名 | 类型 | 说明 |
|---|---|---|
date | 日期 | 交易日期 |
open | 数字 | 开盘价 |
high | 数字 | 最高价 |
low | 数字 | 最低价 |
close | 数字 | 收盘价 |
| 列名 | 类型 | 说明 |
|---|---|---|
symbol | 字符串 | 股票代码(多标的回测时必须提供,单标的时缺省为 "UPLOAD") |
volume | 数字 | 成交量 |
amount | 数字 | 成交额 |
pe、market_cap、pb_ratio 等。这些字段可在公式中直接引用。中文列名支持: 系统会自动将常见的中文列名映射为对应的英文列名:| 中文列名 | 映射到 |
|---|---|
| 日期 / 时间 / 交易日期 / 交易日 | date |
| 开盘 / 开盘价 | open |
| 最高 / 最高价 | high |
| 最低 / 最低价 | low |
| 收盘 / 收盘价 | close |
| 成交量 / 交易量 | volume |
| 成交额 / 交易额 | amount |
| 代码 / 股票代码 / 证券代码 | symbol |
请求参数 (params)
params 为表单字段,值为 JSON 字符串,支持以下参数(与标准回测接口一致,但不含 market/symbols/filter_formula/start_date/end_date):
strategy_code)entry_formula 互斥响应参数
响应格式与标准回测接口完全一致,参见上方 响应参数 章节。Excel 上传回测示例
单标的回测
Excel 文件包含单只股票数据(无需symbol 列):
| date | open | high | low | close | volume |
|---|---|---|---|---|---|
| 2024-01-02 | 10.50 | 10.80 | 10.40 | 10.75 | 5000000 |
| 2024-01-03 | 10.70 | 10.90 | 10.60 | 10.85 | 4500000 |
多标的回测
Excel 文件包含多只股票数据(必须有symbol 列):
| date | symbol | open | high | low | close | volume |
|---|---|---|---|---|---|---|
| 2024-01-02 | 000001 | 10.50 | 10.80 | 10.40 | 10.75 | 5000000 |
| 2024-01-02 | 600519 | 1800 | 1815 | 1790 | 1810 | 2000000 |
带自定义字段的回测
Excel 文件包含自定义基本面字段(如pe、pb):
| date | symbol | open | high | low | close | volume | pe | pb |
|---|---|---|---|---|---|---|---|---|
| 2024-01-02 | 000001 | 10.50 | 10.80 | 10.40 | 10.75 | 5000000 | 15.2 | 1.3 |
注意事项
- 回测结果仅供参考,历史收益不代表未来表现
- 可通过
slippage参数模拟滑点,buy_commission/sell_commission分别设置买卖手续费以更贴近真实交易成本 - 建议使用合理的佣金和止损参数以更接近真实交易
- 如果不设置
exit_formula,则只有买入信号,卖出依赖止损或auto_close
Authorizations
Enter your Bearer token
Body
Backtest input parameters for strategy execution.
Backtest start date (required)
Backtest end date (required)
Stock market (cn, hk, us), default cn
cn, hk, us List of stock codes. If empty, defaults to full market (must provide filter_formula)
Pre-filter formula, e.g., 'NOT(IS_ST)'
Buy/entry formula, triggers buy when result > 0
Sell/exit formula, triggers sell when result > 0 (optional)
Backtest strategy python code
Initial capital, default 100000.0
Commission rate, default 0.0
Buy commission rate, takes priority over commission (e.g., 0.0003 for 0.03%)
Sell commission rate, takes priority over commission (e.g., 0.0013 for A-share sell including stamp duty)
Minimum commission per trade in CNY (e.g., 5.0 means at least 5 yuan per trade)
Slippage ratio (e.g., 0.001 for 0.1%, buy price up, sell price down)
Auto close position at end, default true
Signal factors dictionary for custom strategy pre-computation
Max position size ratio per stock, default 0.2
Max number of holding stocks, default 5
Minimum trading volume (e.g., 100 for A-shares), default 100
Response
Successful Response
Backtest result containing performance metrics and trade details.
Whether backtest completed successfully
Initial capital
Commission rate
Final capital after backtest
Total profit/loss amount
Total return percentage
Maximum drawdown percentage
Gross profit / Gross loss ratio
Winning trades percentage
Total number of completed trades
Number of profitable trades
Number of losing trades
List of trade details
Daily portfolio values (for equity curve)
Daily benchmark values scaled to initial capital
Benchmark total return percentage
Annualized Sharpe ratio with risk-free rate = 0
Annualized CAPM Jensen's Alpha with risk-free rate = 0
Strategy beta relative to benchmark
Start date of maximum drawdown period (peak date)
End date of maximum drawdown period (trough date)
Daily return series (decimal values)
Daily drawdown series (positive decimal values)
Error message when success is false
