Hummingbot 第17章:Dashboard 与监控
Hummingbot Dashboard 提供了强大的 Web 界面,用于实时监控多机器人运行状态、查看交易历史和回测结果。本章详细介绍 Dashboard 的部署、配置和使用。
Dashboard Web 界面
概述
Hummingbot Dashboard 是一个基于 React 构建的 Web 应用,提供了直观的图形化界面来管理交易机器人。
安装 Dashboard
# 使用 Docker 安装(推荐)
docker pull hummingbot/hummingbot-dashboard:latest
# 运行 Dashboard 容器
docker run -d \
--name hummingbot-dashboard \
-p 3000:3000 \
-p 8501:8501 \
-v ~/dashboard-data:/app/data \
hummingbot/hummingbot-dashboard:latest
手动安装
# 克隆仓库
git clone https://github.com/hummingbot/dashboard.git
cd dashboard
# 安装前端依赖
cd frontend && npm install
# 安装后端依赖
cd ../backend && pip install -r requirements.txt
# 启动服务(开发模式)
cd ../ && docker-compose up
访问 Dashboard
启动后,通过浏览器访问以下地址:
- Web 界面:http://localhost:3000
- API 后端:http://localhost:8501
- Streamlit 面板:http://localhost:8501
多机器人仪表盘
连接到机器人
在 Dashboard 中配置 Hummingbot 实例连接:
# dashboard-config.yml
instances:
- name: binance-maker-1
host: 192.168.1.10
api_port: 15871
api_key: "key-1"
api_secret: "secret-1"
- name: binance-maker-2
host: 192.168.1.11
api_port: 15871
api_key: "key-2"
api_secret: "secret-2"
- name: okx-arbitrage
host: 192.168.1.12
api_port: 15871
api_key: "key-3"
api_secret: "secret-3"
仪表盘视图
Dashboard 提供多个预设视图:
| 视图名称 | 显示内容 | 适用场景 |
|---|---|---|
| Overview | 所有机器人的健康状态和关键指标 | 日常巡检 |
| Trading | 实时交易活动和订单状态 | 交易监控 |
| Performance | PnL 曲线和性能对比 | 收益分析 |
| Risk | 风险敞口和资金分布 | 风险管理 |
| Logs | 集中日志查看 | 故障排查 |
实时性能图表
PnL 曲线
Dashboard 自动绘制每个机器人的累计收益曲线:
# Dashboard 后端数据接口示例
# GET /api/v1/performance/pnl
{
"bots": [
{
"name": "binance-maker-1",
"pnl_total": 1250.50,
"pnl_percentage": 12.5,
"data_points": [
{"timestamp": "2025-01-01T00:00:00Z", "pnl": 0},
{"timestamp": "2025-01-02T00:00:00Z", "pnl": 50.20},
{"timestamp": "2025-01-03T00:00:00Z", "pnl": -10.30}
]
}
]
}
Sharpe 比率和回撤
仪表盘提供量化分析指标:
Sharpe Ratio: 2.35 (年化)
Max Drawdown: -8.5% (过去 30 天)
Win Rate: 67.3% (最近 100 笔交易)
Avg Profit: 0.12% (每笔交易)
Avg Loss: -0.08% (每笔交易)
自定义图表
用户可以自定义图表组合:
# 自定义指标面板配置
custom_panels:
- title: "做市商表现"
metrics:
- "pnl_daily"
- "spread_avg"
- "filled_orders_24h"
chart_type: "line"
refresh_interval: 5 # 秒
- title: "市场深度分布"
metrics:
- "orderbook_depth_bid"
- "orderbook_depth_ask"
chart_type: "area"
refresh_interval: 10
交易历史查询
历史数据浏览
# 通过 Dashboard API 查询交易历史
import requests
response = requests.get(
"http://localhost:8501/api/v1/history",
params={
"bot_name": "binance-maker-1",
"start_date": "2025-01-01",
"end_date": "2025-01-31",
"limit": 100,
"offset": 0
}
)
trades = response.json()
for trade in trades["trades"]:
print(
f"{trade['timestamp']} | "
f"{trade['side']} | "
f"{trade['pair']} | "
f"{trade['price']} | "
f"{trade['quantity']}"
)
高级过滤和排序
# Dashboard UI 支持的高级过滤
过滤条件:
- 时间范围 (日期选择器)
- 交易对 (多选)
- 方向 (买入/卖出)
- 策略名称
- 盈亏状态 (盈利/亏损)
排序方式:
- 时间 (默认)
- 交易金额
- 盈亏金额
- 手续费
导出交易记录
# 导出为 CSV
curl "http://localhost:8501/api/v1/history/export?format=csv&start=2025-01-01" \
-o trading_history.csv
# 导出为 JSON
curl "http://localhost:8501/api/v1/history/export?format=json" \
-o trading_history.json
策略回测可视化
回测结果概览
Dashboard 提供直观的回测结果展示:
┌─────────────────────────────────────────────────┐
│ 回测报告: eth_pmm_strategy │
├─────────────────────────────────────────────────┤
│ 总收益率: +15.3% 年化收益率: +45.2% │
│ 最大回撤: -12.1% 夏普比率: 2.15 │
│ 总交易数: 1,245 胜率: 62.4% │
│ 平均盈利: 0.08% 平均亏损: -0.05% │
└─────────────────────────────────────────────────┘
导入回测数据
# 运行回测
hummingbot backtest --strategy pmm_strategy --config pmm_config.yml
# 导入回测结果到 Dashboard
curl -X POST "http://localhost:8501/api/v1/backtest/import" \
-F "file=@backtest_results/backtest_result_20250101.csv"
回测对比分析
Dashboard 支持并排对比多个回测结果:
| 对比项目 | 策略 A(窄价差) | 策略 B(宽价差) | 策略 C(动态价差) |
|---|---|---|---|
| 日收益率 | 0.08% | 0.12% | 0.15% |
| 最大回撤 | -5.2% | -12.1% | -8.5% |
| 换手率 | 高 | 低 | 中 |
| 夏普比率 | 1.85 | 2.15 | 2.42 |
通知配置
配置通知渠道
# dashboard-notifications.yml
notifications:
telegram:
enabled: true
bot_token: "your-bot-token"
chat_ids:
- "-100123456789"
- "987654321"
email:
enabled: true
smtp_server: smtp.gmail.com
smtp_port: 587
username: "[email protected]"
password: "your-app-password"
recipients:
- "[email protected]"
discord:
enabled: false
webhook_url: "https://discord.com/api/webhooks/xxx"
slack:
enabled: false
webhook_url: "https://hooks.slack.com/services/xxx"
通知触发条件
alert_rules:
- name: "daily_pnl_alert"
condition: "daily_pnl < -5%"
channels: ["telegram", "email"]
cooldown: 3600 # 同一告警 1 小时内不重复发送
- name: "bot_crash"
condition: "bot.status == 'error'"
channels: ["telegram", "discord"]
cooldown: 0 # 立即发送
- name: "balance_threshold"
condition: "balance.USDT.total < 500"
channels: ["email"]
cooldown: 86400 # 每天最多一次
性能指标参考
Dashboard 响应时间参考
| 机器人数量 | 页面加载时间 | 数据刷新频率 | 建议刷新间隔 |
|---|---|---|---|
| 1-5 | < 1 秒 | 实时 | 1 秒 |
| 5-20 | 1-2 秒 | 近实时 | 5 秒 |
| 20-50 | 2-5 秒 | 延迟 | 10 秒 |
| 50+ | 5 秒+ | 定期刷新 | 30 秒 |
优化建议
- 减少连接数:合并不活跃的机器人连接
- 调整刷新频率:根据实际需要调整数据轮询间隔
- 使用本地缓存:Dashboard 支持前端缓存减少 API 调用
- 数据归档:将超过 90 天的历史数据归档到外部存储