Files
knowledge-base/2 - Projects/Trading-Agents/Trading Agents 部署记录.md

531 lines
17 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
tags: [trading, multi-agent, openclaw, deployment, discord]
---
# Trading Agents 部署记录
本文记录 Trading Agents 多 Agent 辩论系统的完整部署过程,从 Discord Bot 创建到 OpenClaw 配置更新和 Skill 文件部署。
---
## 一、部署概览
| 项目 | 详情 |
|------|------|
| 部署日期 | 2026-03-21 |
| 远程服务器 | `yiukai@192.168.68.108` (yiukai-ubuntu) |
| OpenClaw 版本 | 2026.3.2 (Node.js v24.13.1) |
| openclaw 二进制位置 | `/home/yiukai/.nvm/versions/node/v24.13.1/bin/openclaw` |
| 需 nvm 加载 | `export NVM_DIR="$HOME/.nvm"; [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"` |
| 配置文件 | `~/.openclaw/openclaw.json` |
| API 数据源 | `https://invest-api.k8s.home` (192.168.68.240:8000) |
| Stock Guild ID | `1479926167141355560` |
| Stock Channel ID | `1479926167736942774` |
---
## 二、Discord Bot 创建
### 创建流程(每个 Bot 重复)
1. 打开 https://discord.com/developers/applications
2. 点击 **"New Application"** → 输入名称 → 创建
3. 左侧菜单 **"Bot"** → 开启 **"Message Content Intent"**Privileged Gateway Intents 下)
4.**"Reset Token"** → 复制 token仅显示一次
5. 左侧 **"OAuth2"** → URL Generator → 勾选 `bot` scope
6. Bot Permissions 勾选:`Send Messages``Read Message History``View Channels`
7. 复制生成的 URL → 浏览器打开 → 邀请到 Stock Guild
### 4 个新 Bot 信息
| Bot 名称 | Discord Account ID | Bot User ID | Token 前缀 |
|-----------|-------------------|-------------|-----------|
| invest-bull | invest-bull | `1484895948860817449` | `MTQ4NDg5NTk0ODg2MDgxNzQ0OQ...` |
| invest-bear | invest-bear | `1484896838506254427` | `MTQ4NDg5NjgzODUwNjI1NDQyNw...` |
| invest-hawk | invest-hawk | `1484897321526362316` | `MTQ4NDg5NzMyMTUyNjM2MjMxNg...` |
| invest-dove | invest-dove | `1484897784024006826` | `MTQ4NDg5Nzc4NDAyNDAwNjgyNg...` |
> **Bot User ID 解码方法**Discord token 的第一段(第一个 `.` 前)是 base64 编码的 User ID。
> ```python
> import base64
> token_prefix = "MTQ4NDg5NTk0ODg2MDgxNzQ0OQ"
> padded = token_prefix + "=" * (4 - len(token_prefix) % 4)
> user_id = base64.b64decode(padded).decode() # → "1484895948860817449"
> ```
### 所有 Bot User ID 汇总(用于 users allowlist
```python
all_bot_ids = [
"964122056163721286", # owner (你)
"1475235860013125826", # ds-commander
"1475236767782146070", # ds-strategist
"1475237428624101518", # ds-builder
"1475237948936159314", # ds-creator
"1475238503729201222", # ds-guardian
"1479925669852086353", # invest-analyst
"1484895948860817449", # invest-bull (新)
"1484896838506254427", # invest-bear (新)
"1484897321526362316", # invest-hawk (新)
"1484897784024006826", # invest-dove (新)
]
```
---
## 三、openclaw.json 配置变更
### 3.1 备份原始配置
```bash
ssh yiukai@192.168.68.108
cp ~/.openclaw/openclaw.json ~/.openclaw/openclaw.json.bak.$(date +%Y%m%d%H%M%S)
```
### 3.2 新增 4 个 Agentagents.list
`agents.list` 数组末尾追加:
```json
{
"id": "invest-bull",
"name": "invest-bull",
"workspace": "/home/yiukai/.openclaw/workspace-invest-bull",
"agentDir": "/home/yiukai/.openclaw/agents/invest-bull/agent",
"model": "kimi-coding/k2p5",
"identity": { "name": "Bull", "emoji": "🐂" }
},
{
"id": "invest-bear",
"name": "invest-bear",
"workspace": "/home/yiukai/.openclaw/workspace-invest-bear",
"agentDir": "/home/yiukai/.openclaw/agents/invest-bear/agent",
"model": "kimi-coding/k2p5",
"identity": { "name": "Bear", "emoji": "🐻" }
},
{
"id": "invest-hawk",
"name": "invest-hawk",
"workspace": "/home/yiukai/.openclaw/workspace-invest-hawk",
"agentDir": "/home/yiukai/.openclaw/agents/invest-hawk/agent",
"model": "kimi-coding/k2p5",
"identity": { "name": "Hawk", "emoji": "🦅" }
},
{
"id": "invest-dove",
"name": "invest-dove",
"workspace": "/home/yiukai/.openclaw/workspace-invest-dove",
"agentDir": "/home/yiukai/.openclaw/agents/invest-dove/agent",
"model": "kimi-coding/k2p5",
"identity": { "name": "Dove", "emoji": "🕊️" }
}
```
变更后 Agent 总数:**11 个**7 个原有 + 4 个新增)
### 3.3 更新 Agent 间通信agentToAgent
将 invest-* 系列全部加入 allow 列表:
```json
"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"
]
}
}
```
> **重要变更**invest-analyst 之前不在 A2A allow 列表中(独立运作),现在加入了,使其能通过 `sessions_send` 触发辩论 agent。
### 3.4 添加 Ping-Pong 轮次配置
新增 `session` 顶级配置:
```json
"session": {
"agentToAgent": {
"maxPingPongTurns": 5
}
}
```
- Bull/Bear 辩论最多 5 轮 ping-pong
- 任何 agent 回复 `REPLY_SKIP` 可提前终止
### 3.5 新增 4 个 Discord Bot 账户
`channels.discord.accounts` 中追加(每个结构相同,仅 token 不同):
```json
"invest-bull": {
"name": "Bull",
"enabled": true,
"token": "MTQ4NDg5NTk0ODg2MDgxNzQ0OQ.Gm081-...",
"groupPolicy": "open",
"streaming": "off",
"guilds": {
"1479926167141355560": {
"requireMention": true,
"users": ["964122056163721286", "1475235860013125826", ...全部 11 ID],
"channels": {
"1479926167736942774": { "allow": true }
}
}
}
}
```
关键配置说明:
| 字段 | 值 | 原因 |
|------|-----|------|
| `requireMention: true` | 所有 4 个新 bot | 防止自动回复用户消息,只在被 A2A 调用时工作 |
| `users` | 11 个 ID | 所有 bot 互相可见owner 可见 |
| `guilds` | Stock Guild only | 只在投资频道活动 |
| `streaming: "off"` | 关闭 | 与现有配置保持一致 |
### 3.6 更新 invest-analyst 的 users 列表
将 4 个新 bot ID 追加到 invest-analyst 的 `guilds.users` 数组,确保 invest-analyst 能在 Stock Guild 中看到新 bot 的消息。
### 3.7 新增 4 个 Binding
`bindings` 数组末尾追加:
```json
{ "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" } }
```
变更后 Binding 总数:**10 个**
---
## 四、Workspace 目录创建
在远程服务器上创建 4 个 agent 的 workspace 和 agent 目录:
```bash
ssh yiukai@192.168.68.108
for agent in invest-bull invest-bear invest-hawk invest-dove; do
mkdir -p ~/.openclaw/workspace-$agent/skills
mkdir -p ~/.openclaw/workspace-$agent/memory
mkdir -p ~/.openclaw/agents/$agent/agent
done
```
### 最终目录结构
```
~/.openclaw/
├── openclaw.json # 主配置(已更新)
├── openclaw.json.bak.20260321XXXXXX # 配置备份
├── workspace-invest-analyst/ # 编排者 workspace
│ └── skills/
│ ├── invest-api/SKILL.md # 原有 API skill
│ ├── trade-analyze/SKILL.md # 新 — 主编排 skill
│ ├── market-analysis/SKILL.md # 新 — 技术分析
│ ├── fundamental-analysis/SKILL.md # 新 — 基本面分析
│ ├── sentiment-analysis/SKILL.md # 新 — 情感分析
│ └── macro-analysis/SKILL.md # 新 — 宏观分析
├── workspace-invest-bull/ # 多方研究员 workspace
│ ├── SOUL.md # 人格:坚定的价值发现者
│ ├── AGENTS.md # 操作指令 + 回声防护
│ ├── skills/
│ └── memory/
├── workspace-invest-bear/ # 空方研究员 workspace
│ ├── SOUL.md # 人格:谨慎的风险猎手
│ ├── AGENTS.md # 操作指令 + 回声防护
│ ├── skills/
│ └── memory/
├── workspace-invest-hawk/ # 激进风控 workspace
│ ├── SOUL.md # 人格:积极进取的交易者
│ ├── AGENTS.md # 操作指令 + 回声防护
│ ├── skills/
│ └── memory/
├── workspace-invest-dove/ # 保守风控 workspace
│ ├── SOUL.md # 人格:稳健的资产守护者
│ ├── AGENTS.md # 操作指令 + 回声防护
│ ├── skills/
│ └── memory/
└── agents/
├── invest-bull/agent/
├── invest-bear/agent/
├── invest-hawk/agent/
└── invest-dove/agent/
```
---
## 五、Skill 文件部署
### 部署来源
所有 skill 文件在本地 `openbb-invest-api` 仓库的 `openclaw-skills/` 目录中开发和版本管理。
### 部署命令
```bash
REMOTE="yiukai@192.168.68.108"
CLAW="/home/yiukai/.openclaw"
SKILLS="/Users/yiukai/Documents/git/openbb-invest-api/openclaw-skills"
# 分析 Skills → invest-analyst workspace
for skill in trade-analyze market-analysis fundamental-analysis sentiment-analysis macro-analysis; do
ssh $REMOTE "mkdir -p $CLAW/workspace-invest-analyst/skills/$skill"
scp "$SKILLS/$skill/SKILL.md" "$REMOTE:$CLAW/workspace-invest-analyst/skills/$skill/SKILL.md"
done
# SOUL.md + AGENTS.md → 辩论 agent workspace
for agent in invest-bull invest-bear invest-hawk invest-dove; do
scp "$SKILLS/souls/$agent.md" "$REMOTE:$CLAW/workspace-$agent/SOUL.md"
scp "$SKILLS/agents/$agent.md" "$REMOTE:$CLAW/workspace-$agent/AGENTS.md"
done
```
### 部署的文件清单
| 文件 | 部署到 | 用途 |
|------|--------|------|
| `trade-analyze/SKILL.md` | invest-analyst/skills/ | 主编排4 分析 → 辩论 → 风控 → 裁决 |
| `market-analysis/SKILL.md` | invest-analyst/skills/ | 技术分析RSI, MACD, Ichimoku, ATR, OBV |
| `fundamental-analysis/SKILL.md` | invest-analyst/skills/ | 基本面PE, ROE, 财报, 分析师目标价) |
| `sentiment-analysis/SKILL.md` | invest-analyst/skills/ | 情感(复合评分, 内幕交易, 新闻情感) |
| `macro-analysis/SKILL.md` | invest-analyst/skills/ | 宏观(利率, CPI, 收益率曲线, 做空压力) |
| `souls/invest-bull.md` | workspace-invest-bull/SOUL.md | 多方人格定义 |
| `souls/invest-bear.md` | workspace-invest-bear/SOUL.md | 空方人格定义 |
| `souls/invest-hawk.md` | workspace-invest-hawk/SOUL.md | 激进风控人格定义 |
| `souls/invest-dove.md` | workspace-invest-dove/SOUL.md | 保守风控人格定义 |
| `agents/invest-bull.md` | workspace-invest-bull/AGENTS.md | 操作指令 + REPLY_SKIP 回声防护 |
| `agents/invest-bear.md` | workspace-invest-bear/AGENTS.md | 操作指令 + REPLY_SKIP 回声防护 |
| `agents/invest-hawk.md` | workspace-invest-hawk/AGENTS.md | 操作指令 + REPLY_SKIP 回声防护 |
| `agents/invest-dove.md` | workspace-invest-dove/AGENTS.md | 操作指令 + REPLY_SKIP 回声防护 |
### 快捷部署脚本
仓库中包含 `openclaw-skills/deploy.sh`,后续更新 skill 只需运行:
```bash
cd /Users/yiukai/Documents/git/openbb-invest-api
./openclaw-skills/deploy.sh
```
---
## 六、Gateway 重启与验证
### 重启命令
```bash
ssh yiukai@192.168.68.108 'export NVM_DIR="$HOME/.nvm"; [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"; openclaw gateway restart'
```
> **注意**SSH 非交互式 shell 不会加载 nvm需要手动 source。
### 重启输出
- Config warning: `google-antigravity-auth` 插件已移除(无影响)
- Doctor: 自动迁移了 Discord single-account 配置
- Doctor warning: Telegram groupPolicy 为 allowlist 但无 allowFrom已知非本次变更
- **重启成功**: `Restarted systemd service: openclaw-gateway.service`
### 状态验证
```bash
ssh yiukai@192.168.68.108 'export NVM_DIR="$HOME/.nvm"; [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"; openclaw status'
```
验证结果:
| 项目 | 期望 | 实际 |
|------|------|------|
| Agents 数量 | 11 | ✅ 11 |
| Discord accounts | 10/10 | ✅ 10/10 |
| Gateway 状态 | running | ✅ active (pid 622452) |
| 新 bot 心跳 | disabled | ✅ disabled辩论 bot 不需要主动心跳) |
---
## 七、配置更新脚本Python
用于批量更新 openclaw.json 的 Python 脚本(在远程通过 SSH 执行):
```python
import json
with open("/home/yiukai/.openclaw/openclaw.json", "r") as f:
config = json.load(f)
# 1. 添加新 agent
new_agents = [
{"id": "invest-bull", "name": "invest-bull",
"workspace": "/home/yiukai/.openclaw/workspace-invest-bull",
"agentDir": "/home/yiukai/.openclaw/agents/invest-bull/agent",
"model": "kimi-coding/k2p5",
"identity": {"name": "Bull", "emoji": "🐂"}},
# ... invest-bear, invest-hawk, invest-dove 同理
]
existing_ids = {a["id"] for a in config["agents"]["list"]}
for agent in new_agents:
if agent["id"] not in existing_ids:
config["agents"]["list"].append(agent)
# 2. 更新 A2A allow
config["tools"]["agentToAgent"]["allow"] = [
"ds-commander", "ds-strategist", "ds-builder", "ds-creator", "ds-guardian",
"invest-analyst", "invest-bull", "invest-bear", "invest-hawk", "invest-dove"
]
# 3. 设置 ping-pong 轮次
config.setdefault("session", {}).setdefault("agentToAgent", {})["maxPingPongTurns"] = 5
# 4. 添加 Discord bot 账户(每个包含 token、guild、channel 配置)
# 5. 更新 invest-analyst 的 users 列表
# 6. 添加 bindings
with open("/home/yiukai/.openclaw/openclaw.json", "w") as f:
json.dump(config, f, indent=2, ensure_ascii=False)
```
> 完整脚本通过 `ssh yiukai@192.168.68.108 'python3 << "PYEOF" ... PYEOF'` 远程执行。
> 脚本包含幂等检查(检查 existing_ids重复运行不会产生重复条目。
---
## 八、Gateway WebSocket 修复(关键补丁)
### 问题
所有内部 RPCsessions_send/spawn、gateway call、status --deep超时原因
1. `bind: "lan"``127.0.0.1:18789` 连不通
2. v2026.3.13 的 WebSocket handshake timeout 只有 3 秒
### 修复 1Systemd 环境变量
`~/.config/systemd/user/openclaw-gateway.service``[Service]` 中添加:
```ini
Environment=OPENCLAW_GATEWAY_URL=ws://192.168.68.108:18789
Environment=OPENCLAW_ALLOW_INSECURE_PRIVATE_WS=1
```
```bash
systemctl --user daemon-reload
```
### 修复 2Patch handshake timeout
文件:`~/.nvm/versions/node/v24.13.1/lib/node_modules/openclaw/dist/gateway-cli-CuZs0RlJ.js`(和 `Ol-vpIk7.js`
```javascript
// 第 7588 行3e3 → 10e3
const DEFAULT_HANDSHAKE_TIMEOUT_MS = 10e3;
```
### 修复 3Patch scope grant
同一文件,第 22605 行:
```javascript
// 原始
if (!device && (!isControlUi || decision.kind !== "allow")) clearUnboundScopes();
// 改为
if (!device && (!isControlUi || decision.kind !== "allow")) { clearUnboundScopes(); } else if (!device && decision.kind === "allow") { scopes = ["operator.read"]; connectParams.scopes = scopes; }
```
> 来源:[PR #47388](https://github.com/openclaw/openclaw/pull/47388)、[PR #48950](https://github.com/openclaw/openclaw/pull/48950)
> **注意**`npm update openclaw` 后 patch 会被覆盖,需要重新打
### 修复 4辩论 Agent Discord 配置
辩论 agent 的 Discord 已完全禁用(不在频道输出):
```json
"invest-bull": { "enabled": false, "groupPolicy": "disabled" }
```
invest-analyst 通过 `sessions_spawn`(非 sessions_send在后台调用辩论 agent。
### 修复 5subagents allowAgents
invest-analyst 需要允许 spawn 辩论 agent
```json
{
"id": "invest-analyst",
"subagents": {
"allowAgents": ["invest-bull", "invest-bear", "invest-hawk", "invest-dove"]
}
}
```
---
## 九、其他已知问题
| 问题 | 影响 | 处理 |
|------|------|------|
| Telegram groupPolicy allowlist 无 allowFrom | 群组消息被丢弃 | 非本次范围 |
| `google-antigravity-auth` 插件 | 已删除 | ✅ |
| openbb-invest-api 偶尔 503 | K8s pod 可能重启 | 检查 pod 状态 |
---
## 十、测试步骤
### 1. 快速分析
```
帮我看看 AAPL 的行情
```
期望invest-analyst 直接用 invest-api skill 回答。
### 2. 深度辩论分析
```
/trade-analyze AMZN
```
期望:
1. 收集数据curl API
2. `sessions_spawn` → invest-bullBull Case
3. `sessions_spawn` → invest-bearBear Case
4. `sessions_spawn` → invest-bullFinal Rebuttal
5. `sessions_spawn` → invest-hawk激进风控
6. `sessions_spawn` → invest-dove保守风控
7. invest-analyst 输出最终 BUY/SELL/HOLD 裁决
### 3. 验证辩论 agent 参与
```bash
for a in invest-bull invest-bear invest-hawk invest-dove; do
echo "$a: $(ls ~/.openclaw/agents/$a/sessions/*.jsonl 2>/dev/null | wc -l) sessions"
done
```
---
## Related
- [[Trading Agents 混合架构方案]]
- [[Trading Agents 调试与优化记录]]
- [[TradingAgents 原始架构分析]]
- [[OpenClaw 部署配置分析]]
- [[OpenClaw Stock Agent 配置详情]]