
Alpaca MCP Server

2025.03.21
0
Python金融交易股票交易账户管理金融服务
Alpaca MCP Server 是一个基于 Model Context Protocol (MCP) 的服务,旨在让大型语言模型(如 Claude)能够通过自然语言与 Alpaca 交易 API 进行交互。该服务器支持股票交易、持仓查询、市场数据获取以及账户管理等功能,为用户提供了一个便捷的金融交易工具。
View on GitHub
Overview
基本能力
产品定位
Alpaca MCP Server 是一个金融交易工具,通过自然语言处理技术,使用户能够通过简单的对话形式进行股票交易和账户管理。
核心功能
- Alpaca 文档访问:提供 Alpaca SDK 文档,帮助用户构建交易策略。
- 市场数据:获取实时股票报价和历史价格数据。
- 账户信息:查询账户余额、购买力和状态。
- 持仓管理:查看当前持仓及其表现。
- 订单管理:通过自然语言下达市价单和限价单,并支持订单的列表、跟踪和取消。
适用场景
- 个人投资者:通过自然语言进行股票交易和账户管理。
- 量化交易:结合语言模型进行策略开发和执行。
- 教育用途:学习股票交易和金融市场的基本操作。
工具列表
get_account_info()
:获取账户余额和状态。get_positions()
:列出所有当前持仓。get_stock_quote(symbol)
:获取股票的实时报价。get_stock_bars(symbol, days)
:获取股票的历史价格数据。get_orders(status, limit)
:列出指定状态的订单。place_market_order(symbol, side, quantity)
:下达市价单。place_limit_order(symbol, side, quantity, limit_price)
:下达限价单。cancel_all_orders()
:取消所有未完成订单。close_all_positions(cancel_orders)
:平仓所有持仓。
常见问题解答
- 默认交易模式:服务器默认使用 Alpaca 的模拟交易模式,如需真实交易,需修改
TradingClient
初始化参数。 - 安全性:服务器具有访问 Alpaca 账户的权限,建议在批准任何交易前仔细检查 Claude 的建议。
使用教程
使用依赖
- Python 3.10+
- Alpaca API 密钥
- Claude for Desktop 或其他 MCP 客户端
安装教程
- 克隆仓库:
bash git clone https://github.com/YOUR_USERNAME/alpaca-mcp.git cd alpaca-mcp
- 安装依赖包:
bash pip install mcp alpaca-py python-dotenv
- 创建
.env
文件并填写 Alpaca API 凭证:API_KEY_ID=your_alpaca_api_key API_SECRET_KEY=your_alpaca_secret_key
调试方式
- 启动服务器:
bash python alpaca_mcp_server.py
- 配置 Claude for Desktop:
- 打开 Claude for Desktop
- 进入设置 > 开发者 > 编辑配置
- 添加服务器配置到
claude_desktop_config.json
并保存 -
重启 Claude for Desktop
-
示例查询:
- "What's my current account balance and buying power?"
- "Show me my current positions"
- "Get the latest quote for AAPL"
- "Show me the price history for TSLA over the last 10 days"
- "Buy 5 shares of MSFT at market price"
- "Sell 10 shares of AMZN with a limit price of $130"
- "Cancel all my open orders"