Skip to content

Commit 8985afc

Browse files
Merge pull request #47 from akfamily/dev
feat(plot): 新增专业级可视化模块与交互式回测报告
2 parents 0a3ed13 + b144d1f commit 8985afc

24 files changed

Lines changed: 2362 additions & 593 deletions

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ repos:
77
- id: check-yaml
88
args: [--unsafe]
99
- id: check-added-large-files
10+
args: ['--maxkb=1000']
1011

1112
- repo: https://github.com/astral-sh/ruff-pre-commit
1213
rev: v0.14.14

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "akquant"
3-
version = "0.1.27"
3+
version = "0.1.28"
44
edition = "2024"
55
description = "High-performance quantitative trading framework based on Rust and Python"
66
license = "MIT"

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,25 @@ sqn -0.743693
145145
kelly_criterion -0.080763
146146
```
147147

148+
## 可视化 (Visualization)
149+
150+
AKQuant 内置了基于 **Plotly** 的强大可视化模块,仅需一行代码即可生成包含权益曲线、回撤分析、月度热力图等详细指标的交互式 HTML 报告。
151+
152+
```python
153+
# 生成交互式 HTML 报告,自动在浏览器中打开
154+
result.report(title="我的策略报告", show=True)
155+
156+
# 或者单独绘制仪表盘
157+
import akquant.plot as aqp
158+
aqp.plot_dashboard(result)
159+
```
160+
161+
<p align="center">
162+
<img src="assets/dashboard_preview.png" alt="Strategy Dashboard" width="800">
163+
<br>
164+
👉 <a href="https://akquant.akfamily.xyz/report_demo/">点击查看交互式报表示例 (Interactive Demo)</a>
165+
</p>
166+
148167
## 文档索引
149168

150169
* 📖 **[核心特性与架构](docs/zh/index.md#核心特性)**: 了解 AKQuant 的设计理念与性能优势。

assets/dashboard_preview.png

239 KB
Loading

docs/assets/reports/akquant_report.html

Lines changed: 339 additions & 0 deletions
Large diffs are not rendered by default.

docs/en/backtest_result.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,53 @@
11
# Backtest Results & Metrics
22

3-
This document details the performance metrics in AKQuant backtest results (`metrics_df`), including their meanings, units, and calculation methods.
3+
This document details the performance metrics in AKQuant backtest results (`metrics_df`), including their meanings, units, and calculation methods. It also covers the visualization capabilities for analyzing these results.
4+
5+
## Visualization (Plotting)
6+
7+
AKQuant provides a comprehensive visualization module to analyze backtest results. You can generate professional-grade interactive HTML reports or individual plots directly from the `BacktestResult` object.
8+
9+
### Quick Start
10+
11+
The easiest way to visualize your backtest results is using the `report()` method:
12+
13+
```python
14+
# Generate a full HTML report
15+
result.report(
16+
title="My Strategy Report",
17+
filename="report.html",
18+
show=True # Set to True to open in browser automatically (default is False)
19+
)
20+
```
21+
22+
This generates a consolidated dashboard including:
23+
- **Equity Curve**: Interactive chart of account equity over time.
24+
- **Drawdown**: Historical drawdown analysis.
25+
- **Monthly Heatmap**: Monthly return performance grid.
26+
- **Key Metrics**: A summary of important performance statistics.
27+
28+
### Intraday Support (New)
29+
30+
The plotting module automatically detects and adapts to intraday (minute-level) backtests:
31+
- **Smart Downsampling**: For large datasets (>10k points), it switches to WebGL rendering (`Scattergl`) for high performance.
32+
- **Adaptive X-Axis**: Automatically formats time labels (e.g., `%Y-%m-%d %H:%M`) and prevents label overlap.
33+
- **Adaptive Duration Units**: Trade duration analysis automatically switches units (Days, Hours, or Minutes) based on the strategy's average holding period.
34+
35+
### Advanced Plotting
36+
37+
You can also access individual plotting functions for more granular control:
38+
39+
```python
40+
import akquant.plot as aqp
41+
42+
# 1. Plot Dashboard (Equity, Drawdown, Heatmap)
43+
aqp.plot_dashboard(result)
44+
45+
# 2. Analyze Trade Distribution (PnL vs Duration)
46+
aqp.plot_trades_distribution(result.trades_df)
47+
48+
# 3. Analyze PnL vs Duration
49+
aqp.plot_pnl_vs_duration(result.trades_df)
50+
```
451

552
## Metrics Overview
653

docs/en/report_demo.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Interactive Backtest Report Demo
2+
3+
Below is an interactive backtest report generated by AKQuant. You can interact directly with the charts on this page to view detailed backtest data.
4+
5+
<iframe src="../../assets/reports/akquant_report.html" width="100%" height="1000px" frameborder="0" style="border: 1px solid #eee; border-radius: 4px;"></iframe>

docs/zh/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ Instrument(
205205

206206
**绩效指标详解 (Performance Metrics):**
207207

208-
详细的绩效指标说明、单位及计算公式,请参考 **[绩效指标详解](metrics.md)**
208+
详细的绩效指标说明、单位及计算公式,请参考 **[绩效指标详解](backtest_result.md)**
209209

210210

211211
## 6. 内置指标 (Indicators)

docs/zh/backtest_result.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,62 @@
156156
| `margin` | 占用保证金 | Float | 当前持仓占用的保证金。 |
157157
| `unrealized_pnl` | 未实现盈亏 | Float | 持仓浮动盈亏。 |
158158
| `entry_price` | 持仓均价 | Float | 当前持仓的平均成本价格。 |
159+
160+
## 可视化 (Visualization)
161+
162+
AKQuant 提供了强大的可视化工具,帮助用户直观地分析策略表现。
163+
164+
### 快速开始 (Quick Start)
165+
166+
最简单的方法是直接调用 `BacktestResult` 对象的 `report` 方法,它会生成一个包含所有关键图表的交互式 HTML 报告。
167+
168+
```python
169+
# 生成完整的 HTML 报告
170+
result.report(
171+
title="我的策略报告",
172+
filename="report.html",
173+
show=True # 设为 True 以自动在浏览器中打开 (默认为 False)
174+
)
175+
```
176+
177+
或者使用 `plot` 方法快速预览特定图表:
178+
179+
```python
180+
# 绘制权益曲线
181+
result.plot(kind="equity")
182+
183+
# 绘制月度热力图
184+
result.plot(kind="heatmap")
185+
```
186+
187+
### 进阶绘图 (Advanced Plotting)
188+
189+
对于更细粒度的控制,可以直接使用 `akquant.plot` 模块。
190+
191+
```python
192+
import akquant.plot as aqp
193+
194+
# 1. 绘制仪表盘 (权益, 回撤, 热力图)
195+
aqp.plot_dashboard(result)
196+
197+
# 2. 分析交易分布 (盈亏 vs 持仓时间)
198+
aqp.plot_trades_distribution(result.trades_df)
199+
200+
# 3. 绘制滚动指标 (夏普比率, 波动率)
201+
aqp.plot_rolling_metrics(result)
202+
203+
# 4. 绘制年度回报
204+
aqp.plot_yearly_returns(result)
205+
206+
# 5. 绘制每日回报分布
207+
aqp.plot_daily_returns_distribution(result)
208+
```
209+
210+
### 日内回测支持 (Intraday Support)
211+
212+
AKQuant 的绘图引擎会自动检测日内数据(如分钟级数据)并进行优化:
213+
214+
* **自适应 X 轴**: 自动调整时间刻度格式,避免标签重叠。
215+
* **WebGL 加速**: 对于超过 10,000 个数据点的图表,自动切换到 WebGL 渲染以保证流畅度。
216+
* **智能布局**: 动态调整子图间距,防止高频数据的标签遮挡。
217+
* **自适应单位**: 交易分布图会自动根据平均持仓时间切换单位(天/小时/分钟)。

0 commit comments

Comments
 (0)