🤖 MCP Multi-Agent Trading Simulation
An autonomous AI trading floor where 4 LLM-powered agents research, analyze, and trade stocks in real-time using the Model Context Protocol (MCP)
______________________________________________________________________
📖 概述
该项目模拟了 完全自主的交易大厅 拥有4名人工智能交易员代理,每个代理都受到传奇投资者的启发。每位交易员:
- 🔍 研究 利用网络搜索获取财经新闻和市场机会
- 📊 分析 实时股票价格和市场数据
- 💼 执行 基于其独特投资策略的买入/卖出交易
- 🧠 记得 过去使用持久知识图的研究
- 📱 报告 通过推送通知进行交易活动
全部由 44台MCP服务器 提供工具、资源和数据源——具有漂亮的 Gradio仪表板 用于实时监控。
______________________________________________________________________
🌟 实时交易大厅仪表板
Real-time dashboard showing 3 of 4 AI traders with portfolio values, performance charts, live trace logs, and P&L tracking
Holdings tables, transaction history, and Cathie's crypto-focused portfolio — all autonomously managed
______________________________________________________________________
🏗️ 系统架构
┌───────────────────────────────────────────────────────────────┐
│ TRADING FLOOR ENGINE │
│ (trading_floor.py — Scheduler Loop) │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Warren │ │ George │ │ Ray │ │ Cathie │ │
│ │ (Value) │ │ (Macro) │ │ (Quant) │ │ (Crypto) │ │
│ └─────┬────┘ └─────┬────┘ └────┬─────┘ └─────┬────┘ │
│ │ │ │ │ │
│ ┌─────▼──────────────▼────────────▼──────────────▼─────┐ │
│ │ MCP SERVER LAYER (11 per trader) │ │
│ │ │ │
│ │ ┌─────────────┐ ┌────────────┐ ┌───────────────┐ │ │
│ │ │ Accounts │ │ Market │ │ Push │ │ │
│ │ │ Server │ │ Server │ │ Notification │ │ │
│ │ └──────┬──────┘ └─────┬──────┘ └───────────────┘ │ │
│ │ │ │ │ │
│ │ ┌──────▼──────┐ ┌─────▼──────┐ │ │
│ │ │ SQLite DB │ │ Polygon.io │ │ │
│ │ │ (accounts) │ │ (market) │ │ │
│ │ └─────────────┘ └────────────┘ │ │
│ │ │ │
│ │ ┌──── RESEARCHER AGENT ──────────────────────────┐ │ │
│ │ │ ┌──────────┐ ┌──────────┐ ┌────────────────┐ │ │ │
│ │ │ │ Fetch │ │ Brave │ │ Memory │ │ │ │
│ │ │ │ Server │ │ Search │ │ (LibSQL) │ │ │ │
│ │ │ └──────────┘ └──────────┘ └────────────────┘ │ │ │
│ │ └────────────────────────────────────────────────┘ │ │
│ └───────────────────────────────────────────────────────┘ │
│ │
│ ┌───────────────────────────────────────────────────────┐ │
│ │ GRADIO DASHBOARD (app.py — Port 7860) │ │
│ │ Portfolio Charts │ Holdings │ Transactions │ Logs │ │
│ └───────────────────────────────────────────────────────┘ │
└───────────────────────────────────────────────────────────────┘______________________________________________________________________
🎭 认识交易员
| 交易者 | 策略 | 灵感来源 | 个性 |
|---|---|---|---|
| 沃伦 | 📈 价值投资 | 沃伦·巴菲特 | 耐心,购买被低估的高质量公司,长期持有 |
| 乔治 | 🌍 宏观交易 | 乔治·索罗斯 | 对地缘政治/经济事件大胆、逆向押注 |
| 雷 | ⚖️ 风险平价 | Ray Dalio | 系统化,跨资产类别多样化,宏观意识强 |
| 凯茜 | 🚀 颠覆性创新 | Cathie Wood | 积极关注加密货币ETF,押注技术颠覆 |
每位交易员都与 $10,000 启动资金,并根据其独特的战略自主管理其投资组合。
______________________________________________________________________
📁 项目结构
MCP-Multi-Agent-Trading-Simulation/
│
├── trading_floor.py # 🏭 Main engine — scheduler loop running all traders
├── traders.py # 🤖 Trader agent class — creates LLM agents with MCP tools
├── accounts.py # 💰 Account & Transaction models — portfolio management
├── accounts_server.py # 🔧 MCP server exposing account tools (buy/sell/balance)
├── accounts_client.py # 📡 MCP client for reading account data
├── market.py # 📊 Market data layer via Polygon.io API
├── market_server.py # 🔧 MCP server exposing share price lookup
├── push_server.py # 📱 MCP server for Pushover push notifications
├── mcp_params.py # ⚙️ MCP server configurations for traders & researchers
├── templates.py # 📝 LLM prompt templates for trader/researcher instructions
├── tracers.py # 📋 Custom tracing — logs agent activity to database
├── database.py # 🗃️ SQLite database layer (accounts, logs, market cache)
├── reset.py # 🔄 Reset all 4 trader accounts with initial strategies
├── app.py # 🖥️ Gradio dashboard UI — live monitoring interface
├── util.py # 🎨 CSS/JS utilities and Color enum for the UI
│
├── pyproject.toml # 📦 Python dependencies (managed by uv)
├── .env.example # 🔑 Template for required API keys
├── .gitignore # 🚫 Git exclusion rules
└── assets/ # 🖼️ Screenshots and images
├── banner.png
├── trading_floor_dashboard.png
└── trading_floor_holdings.png______________________________________________________________________
🔄 工作流程
flowchart TD
A[Trading Floor Engine Starts] --> B{Is Market Open?}
B -->|Yes or RUN_EVEN_WHEN_CLOSED=true| C[Launch All 4 Traders Concurrently]
B -->|No| Z[Sleep N Minutes]
C --> D1[Warren Agent]
C --> D2[George Agent]
C --> D3[Ray Agent]
C --> D4[Cathie Agent]
D1 & D2 & D3 & D4 --> E[Initialize MCP Servers]
E --> F[Read Current Account & Strategy]
F --> G[Research Phase]
G --> G1[🔍 Brave Search — Financial News]
G --> G2[🌐 Fetch — Web Page Content]
G --> G3[🧠 Memory — Recall Past Research]
G1 & G2 & G3 --> H[Analysis & Decision Phase]
H --> I{Trade Decision}
I -->|Buy| J[📈 Execute Buy via Accounts MCP]
I -->|Sell| K[📉 Execute Sell via Accounts MCP]
I -->|Hold| L[No Action]
J & K & L --> M[📱 Send Push Notification Summary]
M --> N[Update Portfolio Value & Logs]
N --> Z
Z --> A
style A fill:#1a1a2e,color:#fff
style C fill:#16213e,color:#fff
style H fill:#0f3460,color:#fff
style M fill:#533483,color:#fff______________________________________________________________________
⚡ 快速开始
先决条件
1.克隆存储库
git clone https://github.com/PreetMhala/MCP-Multi-Agent-Trading-Simulation.git
cd MCP-Multi-Agent-Trading-Simulation2.配置环境变量
cp .env.example .env
# Edit .env and add your API keys3.安装依赖项
uv sync这将安装所有必需的软件包,包括:
openai-agents--支持MCP的OpenAI代理SDKmcp--模型上下文协议框架gradio--Web仪表板UIpolygon-api-client--股票市场数据plotly+pandas--图表和数据处理
4.初始化交易者账户
uv run reset.py创建4个交易者账户(Warren、George、Ray、Cathie),每个账户10000美元,并制定独特的策略。
5.启动仪表板
# Terminal 1 — Start the Gradio UI
uv run app.py打开 http://localhost:7860 在您的浏览器中。
6.启动交易引擎
# Terminal 2 — Start the autonomous traders
uv run trading_floor.py观察你的人工智能交易团队自主研究、分析和交易股票! 🚀
______________________________________________________________________
🔑 配置
必需的API密钥
| 关键 | 目的 | 免费获得 |
|---|---|---|
OPENAI_API_KEY | 交易员代理法学硕士(GPT-4o-mini) | OpenAI平台 |
POLYGON_API_KEY | 股票市场价格数据 | Polygon.io |
BRAVE_API_KEY | 网络搜索研究 | 勇敢搜索API |
可选API密钥
| 关键 | 目的 | 免费获得 |
|---|---|---|
PUSHOVER_USER / PUSHOVER_TOKEN | 交易推送通知 | 窝囊废 |
DEEPSEEK_API_KEY | DeepSeek LLM(多模型模式) | 深度求索 |
GOOGLE_API_KEY | Gemini LLM(多型号模式) | 谷歌AI工作室 |
GROQ_API_KEY | Grok LLM(多型号模式) | Groq |
交易大厅设置
RUN_EVERY_N_MINUTES=60 # Trading cycle interval
RUN_EVEN_WHEN_MARKET_IS_CLOSED=true # Run during off-hours
USE_MANY_MODELS=false # Use different LLMs per trader______________________________________________________________________
🛠️ 技术栈
| 技术 | 角色 |
|---|---|
| OpenAI代理SDK | 带有工具调用和跟踪的代理框架 |
| 模型上下文协议(MCP) | 用于代理的标准化工具/资源服务器 |
| GPT-4o-mini | 所有4个交易商代理的默认LLM |
| Polygon.io | 实时和历史股市数据 |
| 勇敢搜索 | 金融研究的网络搜索 |
| LibSQL(SQLite) | 每个交易者的持久记忆/知识图 |
| 度 | 实时监控仪表板 |
| Plotly | 交互式投资组合图表 |
| SQLite | 账户存储、交易日志、市场缓存 |
| 窝囊废 | 移动推送通知 |
______________________________________________________________________
🧩 MCP服务器(每个交易者)
每个交易者都可以访问 11台MCP服务器 提供专业工具:
交易商MCP服务器
| 服务器 | 提供的工具 |
|---|---|
| 帐户服务器 | buy_shares, sell_shares, get_balance, get_holdings, change_strategy |
| 市场服务器 | lookup_share_price (通过Polygon.io) |
| 推送服务器 | push (发送手机通知) |
研究员MCP服务器
| 服务器 | 提供的工具 |
|---|---|
| mcp服务器获取 | fetch (检索网页内容) |
| 勇敢搜索 | brave_web_search (搜索网页) |
| 内存(LibSQL) | create_entities, create_relations, search_nodes (知识图谱) |
______________________________________________________________________
📊 运作原理
trading_floor.py启动一个调度程序,每N分钟运行一次所有4名交易员- 每
Trader初始化其MCP服务器,并使用以下命令创建代理:
- A. 研究员分代理人 (使用网络搜索、获取和内存工具) - 账户工具 (通过MCP买入/卖出/结算) - 市场工具 (股票价格查询)
- 代理人收到其 策略 和 当前投资组合 作为上下文
- 它 研究 机会, 分析 位置,以及 执行 交易
- 交易后,它会发送一个 推送通知 总结
- 所有活动都是 追踪 并显示在 Gradio仪表板 实时的
______________________________________________________________________
📝 许可证
该项目用于教育和研究目的。负责任地使用。
______________________________________________________________________
Built with ❤️ using OpenAI Agents SDK & MCP
