Files
knowledge-base/2 - Projects/Trading-Agents/Trading Agents 混合架构方案.md

344 lines
14 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
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]]133+ 端点K8s 部署),通过 OpenClaw 的 `sessions_spawn` + `sessions_send` 混合架构实现多 Agent 辩论。
---
## 一、现有环境
| 组件 | 详情 |
|------|------|
| OpenClaw | v2026.2.13`192.168.68.108`yiukai-ubuntusystemd 服务 |
| openbb-invest-api | K8s 集群,`invest-api.k8s.home``192.168.68.240:8000` |
| 已有 Agent | 5 个 ds-* agent主 Guild+ 1 个 invest-analystStock Guild |
| 已有 Skill | `invest-api`(调用 openbb-invest-api |
| 渠道 | Discord6 bot+ Telegram |
| 模型 | kimi-coding/k2p5claude-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。
### 通信模式
```
用户: /trade-analyze NVDA
v
invest-analyst (编排者)
├── 执行 4 个分析 skill数据采集 + LLM 生成报告)
│ ├── /market-analysis NVDA → 技术分析报告
│ ├── /fundamental-analysis NVDA → 基本面报告
│ ├── /sentiment-analysis NVDA → 情感分析报告
│ └── /macro-analysis NVDA → 宏观环境报告
├── sessions_send → invest-bull发送 4 份报告,要求出 bull case
│ invest-bull ⇄ invest-bearping-pong 3-5 轮辩论)
│ invest-analyst 收到完整辩论记录announce
├── sessions_send → invest-hawk发送交易方案要求评估风险
│ invest-hawk ⇄ invest-doveping-pong 2-3 轮辩论)
│ invest-analyst 收到风控辩论记录announce
└── 综合所有报告 + 辩论记录 → 最终 BUY/SELL/HOLD 决策
投递到 Stock Guild 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编排 Skilltrade-analyze
`trade-analyze/SKILL.md` 核心编排逻辑:
1. 执行 4 个分析 skill → 4 份报告
2. `sessions_send` → invest-bullping-pong 到 invest-bear3-5 轮)
3. `sessions_send` → invest-hawkping-pong 到 invest-dove2-3 轮)
4. 综合裁决 → BUY/SELL/HOLD + 仓位 + 止损 + 理由
5. 存入 `memory/YYYY-MM-DD.md`
### Phase 4记忆 + 复盘
- 每次决策存 `memory/YYYY-MM-DD.md`(决策、置信度、价格、理由、辩论要点)
- `MEMORY.md` 持久存储(持仓、胜率、教训)
- 每周 cron 复盘:对比预期 vs 实际收益,更新教训
### Phase 5优化 + 扩展
- 加入做空/期权/DeFi 数据
- 日报集成(纳入现有 daily-invest-briefing cron
- 回测验证 agent 决策历史表现
---
## 五、配置变更清单
### 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 发现)
### 风险 1sessions_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 到最新版后此问题可能已修复。
### 风险 2Token 成本失控
每次完整辩论 = 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 轮即收敛)
> - 分析师用便宜模型Haiku辩论用强模型Opus/Sonnet
### 风险 3Agent 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-api133+ 端点) |
| 交互 | 纯 CLI/API | Discord/Telegram 直接对话 |
---
## Related
- [[TradingAgents 原始架构分析]]
- [[openbb-invest-api]]
- [[OpenClaw 部署配置分析]]
- [[OpenClaw Stock Agent 配置详情]]