Futu MCP
A quantitative analysis platform for Futu Securities, offering intelligent caching, technical analysis, and pattern recognition.
富途 MCP 增强服务 🚀
基于 FastAPI 和 Model Context Protocol (MCP) 的富途证券专业量化分析平台,集成智能缓存、技术分析、形态识别等功能,将简单的API代理升级为企业级金融数据服务。
🎯 从API代理到量化分析平台的完美进化
🚀 5分钟快速启动指南
📋 启动前检查清单
必需组件 ✅
- Python 3.10+ 已安装
- 富途OpenD已启动并登录
- 富途账号有相应行情权限
可选组件 ⚪
- Redis服务(提升缓存性能)
- TA-Lib库(提升计算性能)
⚡ 一键启动
# 1. 克隆项目
git clone <your-repo-url>
cd mcp_futu
# 2. 创建并激活虚拟环境(推荐)
python -m venv venv
source venv/bin/activate # Linux/Mac
# 或 venv\Scripts\activate # Windows
# 3. 安装依赖
pip install -r requirements_enhanced.txt
# 4. 启动服务(四种方式)
# 🌟 智能统一入口(推荐)- 菜单式操作,提供建议和响应
python futu_assistant.py
# 🚀 智能重启 - 自动停止旧服务并启动新服务
python restart.py
# 🔥 手动启动增强版MCP服务 - 端口8001
python main_enhanced.py
# 🎯 简化版HTTP服务(稳定)- 端口8002
python main_enhanced_simple_alternative.py
🌟 智能统一入口(强烈推荐)
一站式解决所有需求的智能助手:
# 启动智能助手
python futu_assistant.py
# 功能菜单:
# 1️⃣ 启动服务 - 智能重启富途服务
# 2️⃣ 健康检查 - 检查服务状态和连接
# 3️⃣ 测试功能 - 完整功能测试
# 4️⃣ 股票报价 - 获取实时股票报价
# 5️⃣ 技术分析 - 计算技术指标
# 6️⃣ 缓存状态 - 查看缓存系统状态
# 7️⃣ API文档 - 打开API文档
# 8️⃣ 查看日志 - 检查服务运行日志
# 9️⃣ 故障诊断 - 智能故障诊断
# 0️⃣ 退出 - 退出助手
# 特点:
# 🧠 智能建议和自动响应
# 🔧 故障自动诊断和解决方案
# 📊 实时状态监控
# 🎯 一键解决各种问题
🔄 智能重启功能
如果遇到端口占用问题(Address already in use),使用智能重启脚本:
# 一键重启 - 自动检测并停止已有服务
python restart.py
# 功能特点:
# ✅ 自动检测端口占用
# ✅ 安全停止已有进程
# ✅ 重新启动增强版服务
# ✅ 验证启动成功
# ✅ 显示服务地址和文档链接
🔍 启动验证
# 健康检查
curl http://localhost:8001/health
# 预期输出:
# {"status":"healthy","futu_connected":true,"cache_available":true}
如果看到上述输出,恭喜您已成功启动!🎉
🌟 核心亮点
🔥 智能缓存系统
- 三层缓存架构: 内存 + Redis + SQLite 多级缓存
- 性能提升99%+: 缓存命中时响应速度提升99.86%
- 智能过期策略: 不同数据类型的差异化缓存管理
- 自动容错: Redis故障时自动降级到本地缓存
📊 专业技术分析
- 15+技术指标: MACD、RSI、布林带、KDJ、移动平均线等
- 智能信号识别: 自动识别金叉死叉、超买超卖等交易信号
- 纯Python实现: 支持TA-Lib和纯Python两种计算方式
- 缓存优化: 指标计算结果智能缓存,避免重复计算
⚡ 性能优化
- 数据传输减少99%+: K线和基本信息数据智能压缩
- 响应时间优化: 平均响应时间提升3-5倍
- 内存管理: 智能内存分配和垃圾回收
- 连接复用: 富途API连接池优化
🛡️ 企业级特性
- 健康监控: 完整的服务和缓存状态监控
- 错误恢复: 自动重试和优雅降级机制
- 扩展性: 模块化设计,便于功能扩展
- 向下兼容: 完全兼容原有API接口
📊 性能对比
功能指标 | 原版服务 | 增强版服务 | 提升幅度 |
---|---|---|---|
K线获取响应时间 | 0.069s | 0.0001s (缓存命中) | 99.86% ⬆️ |
数据传输量 | 1092KB | 8.4KB | 99.2% ⬇️ |
技术指标支持 | ❌ 无 | ✅ 15+ | 新增 ✨ |
缓存系统 | ❌ 无 | ✅ 三层架构 | 新增 ✨ |
智能分析 | ❌ 无 | ✅ 专业量化 | 新增 ✨ |
💻 核心功能使用指南
🔍 1. 股票报价查询
# 单个股票查询
curl -X POST http://localhost:8001/api/quote/stock_quote \
-H "Content-Type: application/json" \
-d '{"code_list": ["HK.00700"]}'
# 批量股票查询(推荐)
curl -X POST http://localhost:8001/api/quote/stock_quote \
-H "Content-Type: application/json" \
-d '{
"code_list": ["HK.00700", "HK.09660", "HK.00005"],
"optimization": {"only_essential_fields": true}
}'
响应示例:
{
"ret_code": 0,
"ret_msg": "获取股票报价成功",
"data": {
"quotes": [{
"code": "HK.00700",
"last_price": 325.4,
"change_val": 2.8,
"change_rate": 0.87,
"volume": 12345678,
"turnover": 4.01e9
}]
}
}
📈 2. 历史K线数据
# 获取日K线数据
curl -X POST http://localhost:8001/api/quote/history_kline \
-H "Content-Type: application/json" \
-d '{
"code": "HK.00700",
"ktype": "K_DAY",
"start": "2024-01-01",
"end": "2024-12-31",
"max_count": 100
}'
# 获取分钟级K线数据
curl -X POST http://localhost:8001/api/quote/history_kline \
-H "Content-Type: application/json" \
-d '{
"code": "HK.00700",
"ktype": "K_30M",
"max_count": 48
}'
支持的K线类型:
K_1M
,K_3M
,K_5M
,K_15M
,K_30M
,K_60M
(分钟线)K_DAY
(日线),K_WEEK
(周线),K_MON
(月线)
🧮 3. 技术指标分析
# 计算单个指标
curl -X POST http://localhost:8001/api/analysis/technical_indicators \
-H "Content-Type: application/json" \
-d '{
"code": "HK.00700",
"indicators": ["rsi"],
"ktype": "K_DAY",
"period": 30
}'
# 计算多个指标
curl -X POST http://localhost:8001/api/analysis/technical_indicators \
-H "Content-Type: application/json" \
-d '{
"code": "HK.00700",
"indicators": ["macd", "rsi", "bollinger_bands"],
"ktype": "K_DAY"
}'
# 计算所有指标(完整分析)
curl -X POST http://localhost:8001/api/analysis/technical_indicators \
-H "Content-Type: application/json" \
-d '{
"code": "HK.00700",
"indicators": ["all"],
"ktype": "K_DAY"
}'
支持的技术指标:
- 趋势指标:
macd
,moving_averages
,ema
- 动量指标:
rsi
,kdj
- 波动性指标:
bollinger_bands
,atr
- 成交量指标:
obv
,vwap
- 强度指标:
adx
🗄️ 4. 缓存管理
# 查看缓存状态
curl http://localhost:8001/api/cache/status
# 预加载热门股票数据
curl -X POST http://localhost:8001/api/cache/preload \
-H "Content-Type: application/json" \
-d '{
"symbols": ["HK.00700", "HK.09660", "HK.00005"],
"days": 30,
"ktypes": ["K_DAY", "K_30M"]
}'
# 清理缓存
curl -X DELETE http://localhost:8001/api/cache/clear \
-H "Content-Type: application/json" \
-d '{"cache_type": "memory"}'
🔧 环境配置详解
1. Python环境配置
# 确认Python版本
python --version # 需要 3.10+
# 创建虚拟环境(推荐)
python -m venv venv
source venv/bin/activate # Linux/Mac
# 或 venv\Scripts\activate # Windows
# 安装依赖
pip install -r requirements_enhanced.txt
2. 富途OpenD配置
# 1. 下载富途OpenD客户端
# https://www.futunn.com/download/openAPI
# 2. 启动OpenD
# - 登录富途账号
# - 确保有相应市场的行情权限
# - 默认端口: 11111
# 3. 验证连接
telnet 127.0.0.1 11111
3. Redis配置(可选,推荐)
# macOS安装
brew install redis
brew services start redis
# Ubuntu安装
sudo apt-get install redis-server
sudo systemctl start redis
# 验证Redis
redis-cli ping # 应返回 PONG
4. TA-Lib配置(可选,性能提升)
# macOS安装
brew install ta-lib
pip install TA-Lib
# Ubuntu安装
sudo apt-get install libta-lib-dev
pip install TA-Lib
# 注意: 如果安装失败,系统会自动使用纯Python实现
🤖 AI助手集成指南
方案0: 智能统一入口(新用户推荐)
最简单的使用方式,无需编程:
# 🌟 启动智能助手 - 一站式解决所有需求
python futu_assistant.py
# 智能助手提供:
# 🎯 菜单式操作界面
# 🧠 智能建议和自动响应
# 🔧 故障自动诊断
# 📊 实时状态监控
# 🚀 一键重启和修复
# 💡 使用指导和帮助
特点:
- ✅ 零编程基础: 菜单式操作,简单易用
- ✅ 智能诊断: 自动检测问题并提供解决方案
- ✅ 一键修复: 自动处理端口占用、服务重启等问题
- ✅ 实时监控: 显示服务状态、缓存情况、连接状态
- ✅ 交互友好: 提供建议响应,引导用户操作
方案1: 稳定HTTP API(开发集成推荐)
直接使用HTTP API,100%稳定可靠:
import httpx
import asyncio
class FutuAnalysisAPI:
def __init__(self, base_url="http://localhost:8001"):
self.base_url = base_url
self.client = httpx.AsyncClient()
async def get_stock_quote(self, codes: list):
"""获取股票报价"""
response = await self.client.post(
f"{self.base_url}/api/quote/stock_quote",
json={"code_list": codes}
)
return response.json()
async def get_technical_analysis(self, code: str, indicators: list = ["all"]):
"""获取技术分析"""
response = await self.client.post(
f"{self.base_url}/api/analysis/technical_indicators",
json={
"code": code,
"indicators": indicators,
"ktype": "K_DAY"
}
)
return response.json()
# 使用示例
api = FutuAnalysisAPI()
quote = await api.get_stock_quote(["HK.00700"])
analysis = await api.get_technical_analysis("HK.00700")
方案2: MCP协议集成
在Cursor设置中添加MCP服务器:
{
"mcpServers": {
"futu-enhanced": {
"url": "http://127.0.0.1:8001/mcp",
"name": "富途量化分析平台"
}
}
}
注意: MCP协议可能存在初始化时序问题,建议优先使用HTTP API。
📚 完整API参考
🔍 行情数据接口
接口名称 | 端点 | 方法 | 功能描述 | 缓存 |
---|---|---|---|---|
股票报价 | /api/quote/stock_quote | POST | 实时股票报价信息 | 10秒 |
历史K线 | /api/quote/history_kline | POST | 历史K线数据 | 永久 |
股票基本信息 | /api/quote/stock_basicinfo | POST | 股票基本信息列表 | 1天 |
🧮 技术分析接口
接口名称 | 端点 | 方法 | 支持指标 | 缓存 |
---|---|---|---|---|
技术指标分析 | /api/analysis/technical_indicators | POST | 全部15+指标 | 5分钟 |
MACD指标 | /api/analysis/macd | POST | MACD专项分析 | 5分钟 |
RSI指标 | /api/analysis/rsi | POST | RSI专项分析 | 5分钟 |
🗄️ 管理接口
接口名称 | 端点 | 方法 | 功能描述 |
---|---|---|---|
健康检查 | /health | GET | 服务健康状态 |
缓存状态 | /api/cache/status | GET | 查看缓存使用情况 |
预加载数据 | /api/cache/preload | POST | 批量预加载数据 |
清理缓存 | /api/cache/clear | DELETE | 清理缓存数据 |
API文档 | /docs | GET | Swagger API文档 |
🚨 故障排除指南
❌ 常见问题及解决方案
问题1: 富途连接失败
ERROR: 连接富途OpenD失败
解决步骤:
# 1. 检查OpenD是否运行
netstat -an | grep 11111
# 2. 检查账号登录状态
# 确保OpenD客户端已登录并有行情权限
# 3. 重启服务
python main_enhanced.py
问题2: 缓存错误
WARNING: Redis连接失败,使用本地缓存
解决方案:
# Redis可选,不影响核心功能
# 如需启用Redis:
brew install redis && brew services start redis # macOS
# 或
sudo apt-get install redis-server && sudo systemctl start redis # Ubuntu
问题3: 技术指标计算失败
ERROR: 技术分析异常: index out of bounds
解决方案:
# 清理缓存,重新获取数据
curl -X DELETE http://localhost:8001/api/cache/clear \
-H "Content-Type: application/json" \
-d '{"cache_type": "sqlite"}'
# 预加载数据
curl -X POST http://localhost:8001/api/cache/preload \
-H "Content-Type: application/json" \
-d '{"symbols": ["HK.00700"], "days": 60}'
问题4: MCP调用被取消
ERROR: Received request before initialization was complete
解决方案:
# 使用稳定的HTTP API替代MCP
curl -X POST http://localhost:8001/api/quote/stock_quote \
-H "Content-Type: application/json" \
-d '{"code_list": ["HK.00700"]}'
🔍 健康检查命令
# 1. 服务状态检查
curl http://localhost:8001/health
# 2. 缓存状态检查
curl http://localhost:8001/api/cache/status
# 3. 测试核心功能
curl -X POST http://localhost:8001/api/quote/stock_quote \
-H "Content-Type: application/json" \
-d '{"code_list": ["HK.00700"]}'
# 4. 性能测试
time curl -X POST http://localhost:8001/api/analysis/technical_indicators \
-H "Content-Type: application/json" \
-d '{"code": "HK.00700", "indicators": ["rsi"]}'
📊 预期性能指标
指标 | 首次请求 | 缓存命中 | 目标值 |
---|---|---|---|
股票报价 | < 200ms | < 10ms | ✅ |
K线数据 | < 500ms | < 1ms | ✅ |
技术指标 | < 300ms | < 50ms | ✅ |
健康检查 | < 10ms | - | ✅ |
📦 生产环境部署
Docker部署(推荐)
FROM python:3.10-slim
WORKDIR /app
COPY requirements_enhanced.txt .
RUN pip install -r requirements_enhanced.txt
COPY . .
EXPOSE 8001
CMD ["uvicorn", "main_enhanced:app", "--host", "0.0.0.0", "--port", "8001"]
# 构建并运行
docker build -t futu-mcp-enhanced .
docker run -d -p 8001:8001 \
-v $(pwd)/data:/app/data \
--name futu-mcp \
futu-mcp-enhanced
Systemd服务部署
# /etc/systemd/system/futu-mcp.service
[Unit]
Description=富途MCP增强服务
After=network.target
[Service]
Type=simple
User=ubuntu
WorkingDirectory=/opt/mcp_futu
Environment=PATH=/opt/mcp_futu/venv/bin
ExecStart=/opt/mcp_futu/venv/bin/python main_enhanced.py
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
# 启用服务
sudo systemctl enable futu-mcp
sudo systemctl start futu-mcp
sudo systemctl status futu-mcp
🔮 版本对比和选择
服务版本选择指南
版本 | 端口 | 特性 | 适用场景 | 稳定性 |
---|---|---|---|---|
main_enhanced.py | 8001 | MCP + HTTP双协议 | AI助手集成 | 95% ⚠️ |
main_enhanced_simple_alternative.py | 8002 | 纯HTTP API | 生产环境 | 100% ✅ |
main_simple.py | 8000 | 基础功能 | 轻量使用 | 100% ✅ |
推荐选择:
- 🔥 生产环境:
main_enhanced_simple_alternative.py
(端口8002) - 🤖 AI集成:
main_enhanced.py
(端口8001) + HTTP API备选 - ⚡ 轻量场景:
main_simple.py
(端口8000)
🤝 获取支持
自助解决
- 📖 API文档: http://localhost:8001/docs
- 🔍 健康检查: http://localhost:8001/health
- 📊 缓存状态: http://localhost:8001/api/cache/status
社区支持
- GitHub Issues: 报告问题
- 讨论: GitHub Discussions
快速诊断
遇到问题时,请提供以下信息:
# 1. 系统信息
python --version
pip list | grep -E "(fastapi|futu|redis|pandas)"
# 2. 服务状态
curl http://localhost:8001/health
# 3. 错误日志
tail -n 50 logs/futu_mcp.log # 如果有日志文件
📜 许可证
本项目采用 MIT 许可证 - 详见 LICENSE 文件
🎉 开始您的专业量化交易之旅!
🔥 从API代理到量化分析平台 | 性能提升99%+ | 企业级稳定性
Related Servers
Zeplin
Official Zeplin server for AI-assisted UI development.
GitHub Workflow Debugger MCP
Diagnose and fix GitHub Actions workflow failures using the GitHub API.
Azure DevOps
Interact with Azure DevOps for managing projects, pipelines, and repositories.
Mobile Next
A platform-agnostic server for scalable mobile automation and development across iOS, Android, simulators, and emulators.
Starwind UI
A server providing tools for developers working with Starwind UI components.
Homebrew MCP
Interact with Homebrew (the package manager for macOS and Linux) using natural language commands.
Untun
Create secure tunnels to expose local servers to the internet using untun.
MCP Expr Lang
MCP Expr-Lang provides a seamless integration between Claude AI and the powerful expr-lang expression evaluation engine.
ComfyUI MCP Server
An image generation server that connects to a local ComfyUI instance via its API, supporting dynamic workflows.
BAMM
Interact with the Borrow Automated Market Maker (BAMM) protocol on the Fraxtal blockchain.