357 lines
15 KiB
Markdown
357 lines
15 KiB
Markdown
---
|
||
created: "2026-03-21"
|
||
type: project
|
||
status: active
|
||
deadline: ""
|
||
tags: [trading, multi-agent, openclaw, openbb, architecture]
|
||
---
|
||
|
||
# Trading Agents 混合架构方案
|
||
|
||
## 目标
|
||
|
||
基于 [[TradingAgents 原始架构分析|TradingAgents]] 的多角色辩论架构,在现有 OpenClaw 环境上实现 AI 驱动的交易决策系统。数据层使用 [[openbb-invest-api]](132+ 端点,K8s 部署),通过 OpenClaw 的 `sessions_spawn` 实现多 Agent 后台辩论。API 覆盖率 79%(104/132 端点)。
|
||
|
||
---
|
||
|
||
## 一、现有环境
|
||
|
||
| 组件 | 详情 |
|
||
|------|------|
|
||
| OpenClaw | v2026.2.13,`192.168.68.108`(yiukai-ubuntu),systemd 服务 |
|
||
| openbb-invest-api | K8s 集群,`invest-api.k8s.home`(`192.168.68.240:8000`) |
|
||
| 已有 Agent | 5 个 ds-* agent(主 Guild)+ 1 个 invest-analyst(Stock Guild) |
|
||
| 已有 Skill | `invest-api`(调用 openbb-invest-api) |
|
||
| 渠道 | Discord(6 bot)+ Telegram |
|
||
| 模型 | kimi-coding/k2p5(主),claude-opus-4-6-thinking(备选) |
|
||
| Agent 间通信 | ds-* 系列已启用,invest-analyst 独立 |
|
||
| Skill 开发 | 在本地 openbb-invest-api 仓库,deploy 脚本推送到远程 |
|
||
|
||
---
|
||
|
||
## 二、架构设计
|
||
|
||
### Agent 规划(新增 4 个 agent)
|
||
|
||
在现有 invest-analyst 基础上扩展,不影响 ds-* 系列:
|
||
|
||
| Agent ID | 角色 | 用途 | Discord 账户 |
|
||
|----------|------|------|-------------|
|
||
| `invest-analyst` (已有) | 编排者/裁判 | 调度分析、主持辩论、最终裁决 | invest-analyst (已有) |
|
||
| `invest-bull` (新) | 多方研究员 | 看多论证 | invest-bull (新 bot) |
|
||
| `invest-bear` (新) | 空方研究员 | 看空论证 | invest-bear (新 bot) |
|
||
| `invest-hawk` (新) | 激进风控 | 高风险高回报视角 | invest-hawk (新 bot) |
|
||
| `invest-dove` (新) | 保守风控 | 资产保护视角 | invest-dove (新 bot) |
|
||
|
||
> 复用 invest-analyst 作为编排者(已有 invest-api skill 和 Stock Guild 绑定)。
|
||
> 分析师做成 skill 而非独立 agent(省资源,数据采集不需要对话)。
|
||
> 只有需要"对话"的辩论角色才需要独立 agent。
|
||
> **经实测确认**:辩论 agent 通过 `sessions_spawn` 调用(@ mention 会导致无限循环,`sessions_send` 会 gateway 死锁,详见 [[Trading Agents 调试与优化记录]])。
|
||
> 辩论 agent 的 Discord 已禁用(`enabled: false`),不在频道输出,只在后台运行。
|
||
|
||
### 通信模式(最终确认)
|
||
|
||
```
|
||
用户: /trade-analyze NVDA
|
||
│
|
||
v
|
||
invest-analyst (编排者)
|
||
│
|
||
├── exec curl 收集 4 类数据(technical, metrics, sentiment, macro)
|
||
│
|
||
├── sessions_spawn → invest-bull(Bull Case)
|
||
│ 等待 announce → Bull 结果返回
|
||
│
|
||
├── sessions_spawn → invest-bear(Bear Case,附带 Bull 论点)
|
||
│ 等待 announce → Bear 结果返回
|
||
│
|
||
├── sessions_spawn → invest-bull(Final Rebuttal)
|
||
│ 等待 announce → Bull 最终反驳返回
|
||
│
|
||
├── sessions_spawn → invest-hawk(激进风控评估)
|
||
│ 等待 announce → Hawk 结果返回
|
||
│
|
||
├── sessions_spawn → invest-dove(保守风控评估)
|
||
│ 等待 announce → Dove 结果返回
|
||
│
|
||
└── 综合所有结果 → 最终 BUY/SELL/HOLD 裁决
|
||
只有 invest-analyst 输出到 Discord
|
||
```
|
||
|
||
### 为什么不用群聊辩论
|
||
|
||
OpenClaw Multi-Agent Group Chat [尚未实现](https://github.com/openclaw/openclaw/issues/18869)。即使实现了,`sessions_send` 方案也更优:
|
||
|
||
| 维度 | 群聊辩论 | sessions_send 辩论 |
|
||
|------|---------|-------------------|
|
||
| 流程控制 | 混乱,谁先说不确定 | 结构化,轮次可控 |
|
||
| 循环风险 | 高(可能死循环) | 无(maxPingPongTurns 硬限制) |
|
||
| 上下文 | 所有人共享一个上下文窗口 | 每个 agent 独立上下文,更专注 |
|
||
| Token 成本 | N 个 agent × 完整群聊历史 | 仅辩论双方共享必要上下文 |
|
||
| 当前可用性 | ❌ 未实现 | ✅ 已可用 |
|
||
|
||
### 学术依据
|
||
|
||
- [ICLR 2025 MAD 研究](https://d2jud02ci9yv69.cloudfront.net/2025-04-28-mad-159/blog/mad/):**异构 agent + 结构化拓扑**效果最好
|
||
- [Adaptive HMAD](https://link.springer.com/article/10.1007/s44443-025-00353-3):异构辩论准确率高 4-6%,事实错误减少 30%+
|
||
- 无结构多 agent 网络会放大错误达 17.2 倍
|
||
|
||
---
|
||
|
||
## 三、openbb-invest-api 数据覆盖
|
||
|
||
> openbb-invest-api 已完全覆盖 TradingAgents 所有数据需求,且更丰富。
|
||
|
||
| TradingAgents 数据需求 | openbb-invest-api 端点 | 额外优势 |
|
||
|---|---|---|
|
||
| 股票行情 OHLCV | `/stock/{s}/historical` | ✅ |
|
||
| 技术指标 (RSI, MACD, BB, ATR) | `/stock/{s}/technical/*` | 14种 + Ichimoku, Fibonacci |
|
||
| 基本面 (财报、资产负债表) | `/stock/{s}/financials`, `/metrics` | ✅ |
|
||
| 公司新闻 | `/stock/{s}/news` | ✅ |
|
||
| 宏观新闻 | `/macro/overview`, `/economy/*` | CPI, GDP, FOMC 等 |
|
||
| 内幕交易 | `/stock/{s}/insider-trades` | ✅ |
|
||
| 情感分析 | `/stock/{s}/sentiment` | 多源复合 (Finnhub+AV+Reddit) |
|
||
|
||
### 独有数据(TradingAgents 没有)
|
||
|
||
- 做空数据 `/shorts/*` — 空头量、FTD、暗池
|
||
- 期权数据 `/market/options/*` — Greeks、IV
|
||
- 固收数据 `/fixed-income/*` — 收益率曲线、SOFR
|
||
- DeFi 数据 `/defi/*` — TVL、收益池
|
||
- A股/港股 `/cn/*` — 实时行情 + 历史
|
||
- 投资组合 `/portfolio/*` — HRP、风险平价、t-SNE 聚类
|
||
- 回测 `/backtest/*` — SMA/RSI/动量策略
|
||
|
||
---
|
||
|
||
## 四、实现步骤
|
||
|
||
### Phase 0:环境准备(SSH 远程操作)
|
||
|
||
1. **升级 OpenClaw**: `npm install -g openclaw@latest` (2026.2.13 → 2026.3.13)
|
||
2. **创建 4 个 Discord bot**: invest-bull, invest-bear, invest-hawk, invest-dove
|
||
- 加入 Stock Guild (`1479926167141355560`),开启 Message Content Intent
|
||
3. **创建 4 个 agent workspace**:
|
||
```bash
|
||
openclaw agents add invest-bull --workspace ~/.openclaw/workspace-invest-bull
|
||
openclaw agents add invest-bear --workspace ~/.openclaw/workspace-invest-bear
|
||
openclaw agents add invest-hawk --workspace ~/.openclaw/workspace-invest-hawk
|
||
openclaw agents add invest-dove --workspace ~/.openclaw/workspace-invest-dove
|
||
```
|
||
4. **更新 openclaw.json**: agent list、Discord accounts、bindings、agentToAgent allow、maxPingPongTurns: 5
|
||
|
||
### Phase 1:分析师 Skills
|
||
|
||
在 openbb-invest-api 仓库的 `openclaw-skills/` 目录开发,部署到 invest-analyst workspace。
|
||
|
||
| Skill | 调用的 API 端点 | 输出 |
|
||
|-------|----------------|------|
|
||
| market-analysis | `/stock/{s}/historical`, `/technical/composite`, `/technical/ichimoku` | 技术趋势、动量、波动率 |
|
||
| fundamental-analysis | `/stock/{s}/metrics`, `/financials`, `/price-targets`, `/upgrades` | 估值、成长性、分析师共识 |
|
||
| sentiment-analysis | `/stock/{s}/sentiment`, `/insider-trades`, `/reddit/trending` | 多源情感、内幕交易动向 |
|
||
| macro-analysis | `/macro/overview`, `/fixed-income/yield-curve`, `/economy/cpi`, `/shorts/volume` | 宏观环境、利率、做空压力 |
|
||
|
||
API 基础 URL: `http://invest-api.k8s.home:8000/api/v1`
|
||
|
||
### Phase 2:辩论者 SOUL.md
|
||
|
||
| Agent | 人格 | 行为 |
|
||
|-------|------|------|
|
||
| invest-bull | 坚定的价值发现者 | 引用数据看多,承认风险但强调上行空间 |
|
||
| invest-bear | 谨慎的风险猎手 | 引用数据看空,质疑乐观假设 |
|
||
| invest-hawk | 积极进取的交易者 | 更大仓位、更紧止损、积极入场 |
|
||
| invest-dove | 稳健的资产守护者 | 分批建仓、更宽止损、保守仓位 |
|
||
|
||
### Phase 3:编排 Skill(trade-analyze)
|
||
|
||
`trade-analyze/SKILL.md` 核心编排逻辑(使用 `sessions_spawn`):
|
||
1. `curl` 收集 8 类数据(summary, technical, sentiment, macro, upgrades, shorts, CAPM, Sortino)
|
||
2. `sessions_spawn` → invest-bull(Bull Case,等待 announce)
|
||
3. `sessions_spawn` → invest-bear(Bear Case,等待 announce)
|
||
4. `sessions_spawn` → invest-bull(Final Rebuttal,等待 announce)
|
||
5. `sessions_spawn` → invest-hawk(激进风控,等待 announce)
|
||
6. `sessions_spawn` → invest-dove(保守风控,等待 announce)
|
||
7. 综合裁决 → BUY/SELL/HOLD + 仓位 + 止损 + 理由
|
||
8. 存入 `memory/YYYY-MM-DD.md`
|
||
|
||
### Phase 4:记忆 + 复盘
|
||
|
||
- 每次决策存 `memory/YYYY-MM-DD.md`(决策、置信度、价格、理由、辩论要点)
|
||
- `MEMORY.md` 持久存储(持仓、胜率、教训)
|
||
- 每周 cron 复盘:对比预期 vs 实际收益,更新教训
|
||
|
||
### Phase 5:优化 + 扩展(✅ 已完成)
|
||
|
||
- ✅ 做空/暗池数据已加入 sentiment-analysis
|
||
- ✅ 量化统计层已加入 fundamental-analysis(normality, unitroot, rolling skew/kurtosis)
|
||
- ✅ 新增 portfolio-review skill(HRP 优化、相关性、聚类、相似度搜索)
|
||
- ✅ 新增 strategy-backtest skill(SMA/RSI/买入持有/动量回测)
|
||
- ✅ API 覆盖率从 19% 提升到 79%
|
||
- 待办:日报集成 trade-analyze 结果
|
||
|
||
---
|
||
|
||
## 五、配置变更清单
|
||
|
||
### openclaw.json 变更
|
||
|
||
```json5
|
||
{
|
||
agents: {
|
||
list: [
|
||
// ... 现有 6 个保持不变 ...
|
||
{ id: "invest-bull", workspace: "~/.openclaw/workspace-invest-bull" },
|
||
{ id: "invest-bear", workspace: "~/.openclaw/workspace-invest-bear" },
|
||
{ id: "invest-hawk", workspace: "~/.openclaw/workspace-invest-hawk" },
|
||
{ id: "invest-dove", workspace: "~/.openclaw/workspace-invest-dove" }
|
||
]
|
||
},
|
||
tools: {
|
||
agentToAgent: {
|
||
enabled: true,
|
||
allow: [
|
||
"ds-commander", "ds-strategist", "ds-builder", "ds-creator", "ds-guardian",
|
||
"invest-analyst", "invest-bull", "invest-bear", "invest-hawk", "invest-dove"
|
||
]
|
||
}
|
||
},
|
||
session: { agentToAgent: { maxPingPongTurns: 5 } },
|
||
channels: {
|
||
discord: {
|
||
accounts: {
|
||
// ... 现有 6 个保持不变 ...
|
||
"invest-bull": { token: "BOT_TOKEN_BULL" },
|
||
"invest-bear": { token: "BOT_TOKEN_BEAR" },
|
||
"invest-hawk": { token: "BOT_TOKEN_HAWK" },
|
||
"invest-dove": { token: "BOT_TOKEN_DOVE" }
|
||
}
|
||
}
|
||
},
|
||
bindings: [
|
||
// ... 现有保持不变 ...
|
||
{ agentId: "invest-bull", match: { channel: "discord", accountId: "invest-bull" } },
|
||
{ agentId: "invest-bear", match: { channel: "discord", accountId: "invest-bear" } },
|
||
{ agentId: "invest-hawk", match: { channel: "discord", accountId: "invest-hawk" } },
|
||
{ agentId: "invest-dove", match: { channel: "discord", accountId: "invest-dove" } }
|
||
]
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
## 六、文件清单
|
||
|
||
### 本地开发(openbb-invest-api 仓库)
|
||
|
||
```
|
||
openclaw-skills/
|
||
├── trade-analyze/SKILL.md # 主编排 skill
|
||
├── market-analysis/SKILL.md # 技术分析 skill
|
||
├── fundamental-analysis/SKILL.md # 基本面分析 skill
|
||
├── sentiment-analysis/SKILL.md # 情感分析 skill
|
||
├── macro-analysis/SKILL.md # 宏观环境分析 skill
|
||
├── souls/
|
||
│ ├── invest-bull.md # Bull SOUL.md
|
||
│ ├── invest-bear.md # Bear SOUL.md
|
||
│ ├── invest-hawk.md # Hawk SOUL.md
|
||
│ └── invest-dove.md # Dove SOUL.md
|
||
├── agents/
|
||
│ ├── invest-bull.md # Bull AGENTS.md
|
||
│ ├── invest-bear.md # Bear AGENTS.md
|
||
│ ├── invest-hawk.md # Hawk AGENTS.md
|
||
│ └── invest-dove.md # Dove AGENTS.md
|
||
└── deploy.sh # SCP 部署脚本
|
||
```
|
||
|
||
### 部署脚本
|
||
|
||
```bash
|
||
#!/bin/bash
|
||
REMOTE="yiukai@192.168.68.108"
|
||
CLAW="~/.openclaw"
|
||
|
||
# Skills → invest-analyst workspace
|
||
for s in trade-analyze market-analysis fundamental-analysis sentiment-analysis macro-analysis; do
|
||
scp -r "openclaw-skills/$s" "$REMOTE:$CLAW/workspace-invest-analyst/skills/"
|
||
done
|
||
|
||
# SOUL.md + AGENTS.md → 各辩论 agent workspace
|
||
for a in invest-bull invest-bear invest-hawk invest-dove; do
|
||
scp "openclaw-skills/souls/$a.md" "$REMOTE:$CLAW/workspace-$a/SOUL.md"
|
||
scp "openclaw-skills/agents/$a.md" "$REMOTE:$CLAW/workspace-$a/AGENTS.md"
|
||
done
|
||
|
||
ssh $REMOTE "openclaw gateway restart"
|
||
```
|
||
|
||
---
|
||
|
||
## 七、验证方案
|
||
|
||
1. **网络连通**: SSH → `curl http://invest-api.k8s.home:8000/health`
|
||
2. **单个 Skill**: Discord `/market-analysis NVDA` → 技术分析报告
|
||
3. **辩论 Ping-Pong**: `sessions_send` invest-bull → ping-pong invest-bear
|
||
4. **完整流程**: `/trade-analyze AAPL` → 4 报告 + 辩论 + 裁决
|
||
5. **回归检查**: invest-analyst 日报 cron + ds-* agents 正常
|
||
|
||
---
|
||
|
||
## 八、风险与应对(Review 发现)
|
||
|
||
### 风险 1:sessions_send 消息回声 Bug
|
||
|
||
[Issue #7804](https://github.com/openclaw/openclaw/issues/7804):`sessions_send` 的 A2A flow 会把 assistant 回复作为新的 `role=user` 消息回注,导致 agent 看到自己的输出并再次回复,形成混乱循环。
|
||
|
||
> **应对**:在辩论 agent 的 AGENTS.md 中加入明确指令——如果收到的消息与自己上一条回复内容相同,回复 `REPLY_SKIP` 终止循环。升级 OpenClaw 到最新版后此问题可能已修复。
|
||
|
||
### 风险 2:Token 成本失控
|
||
|
||
每次完整辩论 = 4 次分析 + 6-10 轮辩论 + 4-6 轮风控 + 裁决 ≈ **15-20 次 LLM 调用**。[研究显示](https://sparkco.ai/blog/optimize-llm-api-costs-token-strategies-for-2025)输出 token 价格是输入的 4-8 倍。
|
||
|
||
> **应对**:
|
||
> - 分析 skill 要求 LLM 输出**结构化 JSON**([省 15% token](https://sparkco.ai/blog/optimize-llm-api-costs-token-strategies-for-2025))
|
||
> - 辩论 agent SOUL.md 加入字数限制(每轮 300-500 字)
|
||
> - 风控辩论设 `maxPingPongTurns: 3`(风控观点 2-3 轮即收敛)
|
||
> - 当前所有 agent 统一使用 kimi-coding/k2p5,后续可考虑分析师用便宜模型、辩论用强模型
|
||
|
||
### 风险 3:Agent Stuck 导致流程卡死
|
||
|
||
知识库记录 invest-analyst 曾出现 stuck 并被 health-monitor 重启。如果辩论 agent stuck,整个流程会卡住。
|
||
|
||
> **应对**:
|
||
> - `sessions_send` 使用 `timeoutSeconds` 参数(建议 120s)
|
||
> - 超时后跳过辩论,直接基于分析报告出裁决
|
||
> - 输出中标注"⚠️ 辩论未完成,置信度降低"
|
||
> - 依赖 OpenClaw health-monitor 自动重启 stuck agent
|
||
|
||
### 最佳实践参考
|
||
|
||
- [Hub-and-Spoke 优于 Mesh](https://www.onabout.ai/p/mastering-multi-agent-orchestration-architectures-patterns-roi-benchmarks-for-2025-2026):中心编排者模式在生产环境更可预测、更易调试
|
||
- [3-7 个 Agent 最佳](https://dev.to/eira-wexford/how-to-build-multi-agent-systems-complete-2026-guide-1io6):低于 3 个不需要多 agent,高于 7 个协调开销超过收益
|
||
- [异构 Agent 效果更好](https://link.springer.com/article/10.1007/s44443-025-00353-3):不同角色用不同人格和模型,比同构 agent 准确率高 4-6%
|
||
- [结构化输出优于散文](https://tradingagents-ai.github.io/):JSON 报告比自然语言更高效传递信息
|
||
|
||
---
|
||
|
||
## 九、与 TradingAgents 的差异
|
||
|
||
| 维度 | TradingAgents (LangGraph) | OpenClaw 混合架构 |
|
||
|------|---|---|
|
||
| 编排 | 显式状态机,条件路由 | sessions_spawn + sessions_send |
|
||
| 并行 | 4 分析师真并行 | Skill 串行(分析师不需要并行,数据采集很快) |
|
||
| 辩论 | 多轮循环,独立 LLM 调用 | ping-pong 机制,最多 5 轮 |
|
||
| 记忆 | 自建 BM25 | OpenClaw 内建(BM25 + 向量 + 时间衰减) |
|
||
| 数据 | yfinance 直调 | openbb-invest-api(133+ 端点) |
|
||
| 交互 | 纯 CLI/API | Discord/Telegram 直接对话 |
|
||
|
||
---
|
||
|
||
## Related
|
||
|
||
- [[TradingAgents 原始架构分析]]
|
||
- [[Trading Agents 部署记录]]
|
||
- [[Trading Agents 调试与优化记录]]
|
||
- [[openbb-invest-api]]
|
||
- [[OpenClaw 部署配置分析]]
|
||
- [[OpenClaw Stock Agent 配置详情]]
|