智能体交易员 - AI-Trader
续Nof1公司推出Alpha Arena交易赛后,涌现出不少基于大模型的开源交易项目,本文介绍一个质量上佳的项目:
AI-Trader
由香港大学-数据智能实验室开源(HKU-Data Intelligence Lab)
项目主要特点如下:
-
支持三类市场交易
-
美股 (交易标的分别为:NASDAQ 100)
-
A股 (交易标的分别为:A50 指数品种)
-
加密货币 (10个主流加密货币:BTC/ETH/XRP/SOL/ADA/SUI/LINK/AVAX/LTC/DOT)
-
-
调用6个大模型
- Gemini-2.5 Flash
- Qwen3 Max
- Deepseek Chat V3.1
- GPT 5
- Claude-3.7-sonnet
- MiniMax M2
1. 项目结构
1.1 功能架构
项目整体可分为以下部分:
- Agent:大模型配置类
- MCP: 通过langchain调用openai的MCP协议,统一大模型调用函数
- data:数据类,完成数据下载、数据预处理、及保存
- 提示词:保存与大模型交互的提示词模版
- 前端:UI界面
- 其他:
- 系统设置
- 命令行运行脚本
1.2 文件目录
文件按功能划分,细节如下:
AI-Trader
├── 🤖 核心系统
│ ├── main.py # 🎯 主程序入口
│ ├── agent/
│ │ ├── base_agent/ # 🧠 通用AI交易代理(美股)
│ │ │ ├── base_agent.py # 基础代理类
│ │ │ ├── base_agent_hour.py # 美股小时级代理类
│ │ ├── base_agent_astock/ # 🇨🇳 A股专用交易代理
│ │ │ ├── base_agent_astock.py # A股日线代理类
│ │ │ ├── base_agent_astock_hour.py # A股小时级代理类
│ │ └── base_agent_crypto/ # ₿ 加密货币专用交易代理
│ │ ├── base_agent_crypto.py # 加密货币代理类
│ └── configs/ # ⚙️ 配置文件
│
├── 🛠️ MCP工具链
│ ├── agent_tools/
│ │ ├── tool_trade.py # 💰 交易执行(自动适配市场规则)
│ │ ├── tool_get_price_local.py # 📊 价格查询(支持美股+A股)
│ │ ├── tool_jina_search.py # 🔍 信息搜索
│ │ ├── tool_math.py # 🧮 数学计算
│ │ └── start_mcp_services.py # 🚀 MCP服务启动脚本
│ └── tools/ # 🔧 辅助工具
│
├── 📊 数据系统
│ ├── data/
│ │ ├── daily_prices_*.json # 📈 纳斯达克100股票价格数据
│ │ ├── merged.jsonl # 🔄 美股日线统一数据格式
│ │ ├── get_daily_price.py # 📥 美股数据获取脚本
│ │ ├── merge_jsonl.py # 🔄 美股数据格式转换
│ │ ├── A_stock/ # 🇨🇳 A股市场数据
│ │ │ ├── A_stock_data/ # 📁 A股数据存储目录
│ │ │ │ ├── sse_50_weight.csv # 📋 上证50成分股权重
│ │ │ │ ├── daily_prices_sse_50.csv # 📈 日线价格数据(CSV)
│ │ │ │ ├── A_stock_hourly.csv # ⏰ 60分钟K线数据(CSV)
│ │ │ │ └── index_daily_sse_50.json # 📊 上证50指数基准数据
│ │ │ ├── merged.jsonl # 🔄 A股日线统一数据格式
│ │ │ ├── merged_hourly.jsonl # ⏰ A股小时级统一数据格式
│ │ │ ├── get_daily_price_tushare.py # 📥 A股日线数据获取(Tushare API)
│ │ │ ├── get_daily_price_alphavantage.py # 📥 A股日线数据获取(Alpha Vantage API)
│ │ │ ├── get_interdaily_price_astock.py # ⏰ A股小时级数据获取(efinance)
│ │ │ ├── merge_jsonl_tushare.py # 🔄 A股日线数据格式转换(Tushare)
│ │ │ ├── merge_jsonl_alphavantage.py # 🔄 A股日线数据格式转换(Alpha Vantage)
│ │ │ └── merge_jsonl_hourly.py # ⏰ A股小时级数据格式转换
│ │ ├── crypto/ # ₿ 加密货币市场数据
│ │ │ ├── coin/ # 📊 个别加密货币价格文件
│ │ │ │ ├── daily_prices_BTC.json # 比特币价格数据
│ │ │ │ ├── daily_prices_ETH.json # 以太坊价格数据
│ │ │ │ └── ... # 其他加密货币数据
│ │ │ ├── crypto_merged.jsonl # 🔄 加密货币统一数据格式
│ │ │ ├── get_daily_price_crypto.py # 📥 加密货币数据获取脚本
│ │ │ └── merge_crypto_jsonl.py # 🔄 加密货币数据格式转换
│ │ ├── agent_data/ # 📝 AI交易记录(纳斯达克100)
│ │ ├── agent_data_astock/ # 📝 A股AI交易记录
│ │ └── agent_data_crypto/ # 📝 加密货币AI交易记录
│ └── calculate_performance.py # 📈 交易性能分析
│
├── 💬 提示词系统
│ └── prompts/
│ ├── agent_prompt.py # 🌐 通用交易提示词(美股)
│ └── agent_prompt_astock.py # 🇨🇳 A股专用交易提示词
│
├── 🎨 前端界面
│ └── doc/ # 🌐 Web仪表板
│
├── 📋 配置与文档
│ ├── configs/ # ⚙️ 系统配置
│ │ ├── default_config.json # 美股默认配置
│ │ └── astock_config.json # A股配置示例
│ └── calc_perf.sh # 🚀 性能计算脚本
│
└── 🚀 快速启动脚本
└── scripts/ # 🛠️ 便捷启动脚本
├── main.sh # 一键完整流程(美股)
├── main_step1.sh # 美股:数据准备
├── main_step2.sh # 美股:启动MCP服务
├── main_step3.sh # 美股:运行交易代理
├── main_a_stock_step1.sh # A股:数据准备
├── main_a_stock_step2.sh # A股:启动MCP服务
├── main_a_stock_step3.sh # A股:运行交易代理
└── start_ui.sh # 启动Web界面
2.大模型交互
这是一轮api调用的数据记录
json
{"timestamp": "2025-10-30T13:26:25.780804", "signature": "claude-3.7-sonnet", "new_messages": [{"role": "user", "content": "Please analyze and update today's (2025-10-09) positions."}]}
{
"timestamp": "2025-10-30T13:28:12.306050",
"signature": "claude-3.7-sonnet",
"new_messages": [{
"role": "assistant",
"content":
"## Portfolio Analysis Summary\n\n I've invested our 100,000 yuan cash balance into a diversified portfolio with the following allocations:\n\n
1. **Technology and Semiconductors (39.0% of portfolio)**\n
- 688981.SH (中芯国际): 100 shares - Leading Chinese semiconductor company\n
- 688041.SH (海光信息): 100 shares - Technology hardware provider\n\n
2.**Financial Services (29.3% of portfolio)**\n
- 600036.SH (招商银行): 300 shares - Leading Chinese bank\n
- 601318.SH (中国平安): 300 shares - Major insurance company\n\n
3.**Consumer and Retail (22.6% of portfolio)**\n
- 601888.SH (中国中免): 100 shares - Duty-free retail and consumer goods\n
- 600887.SH (伊利股份): 200 shares - Leading dairy producer\n\n4.
**Utilities and Mining (15.9% of portfolio)**\n
- 600900.SH (长江电力): 300 shares - Major utility company\n
- 601899.SH (紫金矿业): 300 shares - Mining company\n\n
The portfolio is now fully invested with 1,700 shares across 8 different companies, with just 794 yuan remaining in cash. This allocation provides:\n\n
1. Exposure to high-growth technology sector, particularly semiconductors which have been outperforming\n
2. Stability through financial companies\n
3. Consumer exposure for defensive positioning\n
4. Commodities and utilities for diversification\n\n
This represents a balanced approach with a slight overweight toward technology given the strong performance of Chinese tech stocks in 2025 as noted in our research. The portfolio is well-positioned to benefit from China's continued technological advancement while maintaining exposure to more traditional sectors.\n\n<FINISH_SIGNAL>"
}]
}
2.1 大模型交易
这里使用langchain调用mcp,并执行动作函数,需要通过mcp启动各函数的服务,具体流程如下:
┌────────┐
│ 1.大模型 (思考-推理)
└────────┘
│
生成决策
│
┌────┴───┐
│ 2.工具调用 MCP (行情/交易)
└────────┘
│ 获取数据-调用数据tool,并返回大模型-继续推理
│ 执行订单-调用交易tool
│
环境反馈(价格/成交/持仓)
┌────┴───┐
│ 3.状态更新 写入position.jsonl
└────────┘
│
日志记录 log.jsonl
│
进入下一轮
2.2 MCP服务
MCP 提供 API 形式的接口,智能体可以在线/本地模型共同使用,执行函数通过mcp.tool()修饰:
python
mcp = FastMCP("TradeTools")
@mcp.tool()
def buy(...)
@mcp.tool()
def sell(...)
这里对交易的执行需要上锁,保证数据的一致性:
Agent ---> buy() or sell() ---> MCP Tool Layer
|
|-- 加锁保护
|-- 读取仓位
|-- 获取开盘价
|-- 风险规则校验
|-- 更新仓位(金额/持仓)
|-- 写入 position.jsonl
|-- 返回最新仓位
3.界面展示
这里展示A股的交易结果,有6个大模型在参与测评,另外1个是A50指数作为基线,可以看到交易是从2025年10月开始的:

截止2025年12月8日的业绩如图左半部分所示,Claude和MiniMax(上海的一家大模型公司)收益为正,其他为负,
右半部分是大模型执行操作时,其输出的决策分析内容。

3.1 Claude-3.7
以下是Claude具体的持仓情况,尽2月共交易163次,总体盈利2.26%:

3.2 Deepseek Chat v3.1
Deepseek的交易要多一些,手持现金也略多,但收益为-49%:

3.3 Gemini 2.5 Flash
这个是亏的比较多的,总首日-8.31%,交易次数也更多一些,可能亏的比较多,当前仓位也较轻,属于止损状态。
