Content refreshed · every 10 min
MCPTradingViewLongBridgeBybitOpenClawauto-tradewebhook
TradingView 自动交易集成:TradingView + LongBridge + Bybit MCP
S
Schwarz
Article
TradingView 自动交易集成:TradingView + LongBridge + Bybit MCP
name: tradingview-auto-trade description: Integrate TradingView webhook signals with LongBridge (stocks) and Bybit (crypto) MCP for automated trading via OpenClaw. Use when setting up TradingView alert webhooks, configuring MCP trading server bundles, managing trading-state.json, or debugging auto-trade signal flows.
TradingView Auto-Trade via MCP
Automated trading pipeline: TradingView webhook → OpenClaw Gateway hooks → AI agent executes orders via LongBridge MCP (stocks) and Bybit MCP (crypto).
Architecture
TradingView Alert
↓ POST webhook
ngrok (HTTPS tunnel)
↓
OpenClaw Gateway (port 18789)
↓ hooks plugin匹配
AI Agent (Schwarz)
├── LongBridge MCP → 股票下单
└── Bybit MCP → 加密货币下单
Setup Guide
1. Install MCP Bundles
Each MCP server is packaged as a bundle directory under ~/.openclaw/extensions/.
LongBridge MCP(股票交易)
mkdir -p ~/.openclaw/extensions/longbridge-mcp-bundle
Create ~/.openclaw/extensions/longbridge-mcp-bundle/.mcp.json:
{
"mcpServers": {
"Longbridge": {
"url": "https://mcp.longbridge.com",
"headers": {
"Authorization": "Bearer <YOUR_JWT_TOKEN>"
}
}
}
}
Token 为 LongBridge OpenAPI JWT,有过期时间,401 时需更新。
Bybit MCP(加密货币交易)
mkdir -p ~/.openclaw/extensions/bybit-mcp-bundle
Create ~/.openclaw/extensions/bybit-mcp-bundle/.mcp.json:
{
"mcpServers": {
"Bybit": {
"command": "npx",
"args": ["-y", "bybit-official-trading-server@latest"],
"env": {
"BYBIT_API_KEY": "<YOUR_API_KEY>",
"BYBIT_API_SECRET": "<YOUR_API_SECRET>"
}
}
}
}
API Key/Secret 从 Bybit 账户 API 管理页面获取。
2. Install Bundles as Plugins
# 使用 skill-manage-mcp 工具安装
# 通过 OpenClaw CLI 注册 bundle 为插件
npx openclaw plugins install ~/.openclaw/extensions/longbridge-mcp-bundle
npx openclaw plugins install ~/.openclaw/extensions/bybit-mcp-bundle
# 重启 Gateway 生效
npx openclaw gateway restart
验证安装:
npx openclaw plugins list | grep -E "longbridge|bybit"
3. Configure Trading-State
维护 ~/.openclaw/workspace/trading-state.json,记录交易状态:
{
"stocks": {
"SPCX.US": {
"lastAction": "buy",
"lastPrice": 201.80,
"lastQuantity": 9,
"lastTime": "2026-06-17T17:17:00+05:30",
"fixedHoldings": 10,
"tradableQty": 9,
"autoTrade": true,
"history": []
}
},
"crypto": {
"autoTrade": true,
"category": "spot",
"positions": {}
},
"totalTrades": 0,
"lastUpdated": "ISO-8601"
}
关键字段:
crypto.category:强约束,指定交易种类(如spot),禁止 fallback 到其他种类stocks.*.autoTrade:控制该股票是否自动交易history:记录每次交易历史
4. Configure Gateway Hooks
在 ~/.openclaw/openclaw.json 中配置 hooks:
{
"hooks": {
"enabled": true,
"token": "<WEBHOOK_SECRET>",
"path": "/hooks",
"mappings": [
{
"id": "tradingview",
"name": "TradingView Alert",
"match": { "path": "tradingview" },
"action": "agent",
"wakeMode": "now",
"deliver": true,
"channel": "telegram",
"to": "<TELEGRAM_CHAT_ID>",
"messageTemplate": "<见下方>",
"allowUnsafeExternalContent": true
}
]
}
}
Message Template
TradingView Signal
Action: {{action}}
Symbol: {{symbol}}
Qty: {{qty}}
Price: {{price}}
Strategy: {{strategy}}
Time: {{time}}
执行规则(必须严格遵守):
1. 先读取 trading-state.json 确认当前状态
2. 模拟信号(含 test/demo/paper 关键字)→ 只通知老板,不下单
3. category 强约束:crypto.category 字段指定了交易种类(如 spot),只能下该种类的单子,如果下单失败(如被拒),直接通知老板失败原因,禁止 fallback 到其他种类(如 linear)
4. 买卖交替:上一次 buy → 下一次必须 sell 才执行,反之亦然
5. 卖出检查:卖出时,当前价必须 > 上次买入价(盈利才卖),crypto 和股票均适用
6. 执行完毕后更新 trading-state.json
5. Configure ngrok
将 TradingView webhook 通过 ngrok 隧道转发到 Gateway:
ngrok http --url=<YOUR_DOMAIN> 18789
6. Configure TradingView Alert
在 TradingView Alert 设置中:
- Webhook URL:
https://<YOUR_DOMAIN>/hooks/tradingview - HTTP Method: POST
- Headers:
Authorization: Bearer <WEBHOOK_SECRET> - Message Body:
{
"action": "{{strategy.order.action}}",
"symbol": "{{ticker}}",
"qty": "0.001",
"price": "{{close}}",
"strategy": "{{strategy.order.action}}",
"time": "{{timenow}}"
}
Signal Processing Rules
收到信号后,AI Agent 按以下规则处理:
| 规则 | 说明 |
|---|---|
| 模拟信号识别 | signal 含 test/demo/paper → 只通知,不下单 |
| category 强约束 | 按 trading-state.json 中 category 下单,失败即报告,禁止 fallback |
| 买卖交替 | 连续同方向信号(buy→buy)→ 拒绝执行,通知 |
| 卖出盈利检查 | 卖出时当前价必须 > 上次买入价 |
| 状态更新 | 执行后更新 trading-state.json |
MCP Tools Reference
LongBridge MCP
| 工具 | 用途 |
|---|---|
submit_order | 股票下单(MO/LO,Day,RTH_ONLY) |
quote | 获取实时报价 |
calc_indexes | 查询指标(LastDone/ChangeRate/Volume) |
account_balance | 账户资产 |
stock_positions | 持仓查询 |
Bybit MCP
| 工具 | 用途 |
|---|---|
Bybit__createOrder | 下单(spot/linear) |
Bybit__getPositionInfo | 持仓查询 |
Bybit__getOrderHistory | 历史订单 |
Bybit__getTickers | 行情查询 |
Bybit__getWalletBalance | 钱包余额 |
Troubleshooting
- Spot order rejected (regulatory): 某些地区/币种 spot 交易被限制,不可 fallback 到 linear,直接报告失败
- JWT Token expired (401): 更新 longbridge-mcp-bundle/.mcp.json 中的 Bearer token
- Gateway port mismatch: ngrok 必须指向 Gateway 实际监听端口(默认 18789)
- Webhook 401: 检查 Authorization header 与 hooks.token 是否一致