交易市场数据服务器

Alpaca MCP Server 是一个为 Alpaca 经纪 API 提供交易和市场数据工具的服务,支持账户管理、订单处理、市场数据获取和期权交易等功能。

  • by NonmaskableInt
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 NonmaskableInt/alpaca_mcp

使用说明

羊驼MCP服务器

MCP(模型上下文协议)服务器,为 羊驼 经纪公司API。

特性

账户和投资组合

  • get_count_info -账户余额、购买力、股权、保证金信息
  • get_positions -损益表中的当前股票头寸
  • get_portfolio_历史 -历史投资组合业绩

订单

基本订单类型

  • 获取订单 -带有过滤功能的订单历史记录
  • 获取订单 -按ID获取特定订单
  • 地点_市场_订单 -以最优惠的价格执行(立即)
  • 地点限制订单 -以指定价格或更高价格执行(或根本不执行)
  • 地点_停止_订单 -当止损价格达到时触发市价单(担保完成)
  • place_stop_limit_order -当止损价格达到时触发限价订单(保证最高价格)
  • 地点_栏杆_停止_订购 -价格变动后的动态止损
  • 取消订单 -取消待处理订单

保护性秩序策略

使用这些来自动保护既有利润目标又有止损的头寸:

  • 地点_价格_订单 -新头寸:入市+止盈+止损(3个订单)

    • 在进入一个位置并需要自动保护时使用
    • 示例:以175美元买入,以185美元获利,以170美元止损
  • 地点_订单 -对于现有头寸:止盈+止损(2个订单)

    • 当您已经拥有股份并希望添加保护时使用
    • 示例:已经拥有股票,以185美元(利润)或170美元(亏损)退出,以先到者为准

职位管理

  • 闭合位置 -关闭全部或部分头寸
  • close_all_位置 -关闭所有打开的位置(紧急出口)

市场数据

  • get_latest_quotes -实时买卖报价
  • get_stock_bars -历史OHLCV数据

期权交易

  • get_option_contracts -搜索选项链
  • get_option_contract -获取具体的合同详细信息
  • 获取选项位置 -当前期权头寸
  • 地点_选项_订单 -单腿期权订单
  • place_multi_leg_option_order -散布、跨越等。
  • 锻炼选择体位 -行使期权合约

安装

需要Python 3.10+和 紫外线.

# Clone the repository
git clone <repository-url>
cd alpaca_mcp

# Install dependencies
uv sync

# Install with dev dependencies (for testing)
uv sync --extra dev

配置

设置以下环境变量:

export ALPACA_API_KEY="your-api-key"
export ALPACA_SECRET_KEY="your-secret-key"
export ALPACA_PAPER="true"  # Use paper trading (default: true)

从获取API密钥 羊驼仪表板.

用法

运行服务器

# Using the launcher (cross-platform)
python launch.py

# Or directly with uv
uv run alpaca-mcp-server

# With SSE transport
uv run alpaca-mcp-server --sse

# With streamable HTTP transport
uv run alpaca-mcp-server --streamable

MCP客户端配置

添加到MCP客户端配置中:

{
  "mcpServers": {
    "alpaca": {
      "command": "python",
      "args": ["/path/to/alpaca_mcp/launch.py"],
      "env": {
        "ALPACA_API_KEY": "your-api-key",
        "ALPACA_SECRET_KEY": "your-secret-key",
        "ALPACA_PAPER": "true"
      }
    }
  }
}

例子

下支架订单

购买10股AAPL股票,自动获利180美元,止损165美元:

{
  "tool": "place_bracket_order",
  "arguments": {
    "symbol": "AAPL",
    "qty": 10,
    "side": "buy",
    "take_profit_limit_price": 180.00,
    "stop_loss_stop_price": 165.00,
    "entry_type": "limit",
    "entry_limit_price": 170.00
  }
}

设置期权价差

购买垂直看涨价差:

{
  "tool": "place_multi_leg_option_order",
  "arguments": {
    "legs": [
      {
        "symbol": "AAPL240119C00175000",
        "ratio_qty": 1,
        "side": "buy",
        "position_intent": "buy_to_open"
      },
      {
        "symbol": "AAPL240119C00180000",
        "ratio_qty": 1,
        "side": "sell",
        "position_intent": "sell_to_open"
      }
    ],
    "order_type": "limit",
    "limit_price": 2.50
  }
}

获取历史数据

{
  "tool": "get_stock_bars",
  "arguments": {
    "symbols": "AAPL,MSFT",
    "timeframe": "1Day",
    "start": "2024-01-01",
    "end": "2024-01-31",
    "limit": 100
  }
}

测试

# Run all tests
uv run pytest

# Run with verbose output
uv run pytest -v

# Run with coverage report
uv run pytest --cov=server --cov-report=term-missing

# Run specific test file
uv run pytest tests/test_orders.py

# Run specific test
uv run pytest tests/test_orders.py::TestPlaceBracketOrder -v

项目结构

alpaca_mcp/
├── server.py           # Main MCP server implementation
├── launch.py           # Cross-platform launcher
├── pyproject.toml      # Project configuration
├── shared/
│   ├── __init__.py
│   └── types.py        # Pydantic models for requests/responses
└── tests/
    ├── conftest.py     # Test fixtures
    ├── test_account.py
    ├── test_orders.py
    ├── test_market_data.py
    └── test_options.py

许可证

麻省理工学院