Skip to main content
Glama
edkdev

MetaTrader5 MCP Server

by edkdev

MetaTrader5 MCP 服务器

一个提供 MetaTrader5 (MT5) 平台交易接口的模型上下文协议 (MCP) 服务器。

功能

  • 账户管理:获取账户信息、余额、净值、保证金

  • 持仓管理:查看、修改和关闭开仓头寸

  • 订单操作:下达市价/挂单、取消订单

  • 市场数据:获取品种信息、当前价格(报价)

  • 行情/蜡烛图:通过 start_pos/count 或日期范围获取 OHLCV 数据

  • 指标:计算 SMA、EMA、RSI、MACD、布林带、ATR、随机指标

  • 批量指标:单次调用计算多个指标

  • 信号工具:EMA 交叉趋势标志和入场一致性评分

Related MCP server: MetaTrader 5 MCP Server

安装

使用 uv 安装依赖:

uv sync

配置

环境变量现在直接在 MCP 客户端设置中配置(无需 .env 文件)。

Claude Desktop / Kiro

添加到您的 mcp.json 配置文件中:

{
  "mcpServers": {
    "mt5-trading": {
      "command": "uvx",
      "args": ["mt5-mcp"],
      "env": {
        "MT5_LOGIN": "your_account_number",
        "MT5_PASSWORD": "your_password",
        "MT5_SERVER": "your_broker_server",
        "MT5_PATH": "C:\\Program Files\\MetaTrader 5\\terminal64.exe",
        "MT5_TIMEOUT": "60000",
        "DEFAULT_DEVIATION": "20",
        "DEFAULT_MAGIC": "234000"
      }
    }
  }
}

必需的环境变量:

  • MT5_LOGIN - 您的 MT5 账户号码

  • MT5_PASSWORD - 您的 MT5 账户密码

  • MT5_SERVER - 您的经纪商服务器名称

  • MT5_PATH - MT5 终端可执行文件路径

可选的环境变量:

  • MT5_TIMEOUT - 连接超时时间(毫秒,默认:60000)

  • DEFAULT_DEVIATION - 默认价格偏差(点数,默认:20)

  • DEFAULT_MAGIC - 订单默认魔法数字(默认:234000)

替代方案:本地开发

对于本地开发,您也可以直接运行:

# Set environment variables and run
MT5_LOGIN=your_account MT5_PASSWORD=your_pass MT5_SERVER=your_server MT5_PATH="C:\Program Files\MetaTrader 5\terminal64.exe" uv run python mt5_mcp/server.py

或使用 uv 目录的旧方法:

{
  "mcpServers": {
    "mt5-trading": {
      "command": "uv",
      "args": [
        "--directory",
        "D:\\path\\mt5-mcp",
        "run",
        "python",
        "mt5_mcp/server.py"
      ],
      "env": {
        "MT5_LOGIN": "your_account_number",
        "MT5_PASSWORD": "your_password",
        "MT5_SERVER": "your_broker_server",
        "MT5_PATH": "C:\\Program Files\\MetaTrader 5\\terminal64.exe"
      }
    }
  }
}

可用工具

账户信息

  • mt5_get_account_info - 获取账户余额、净值、保证金等

  • mt5_get_positions - 获取开仓头寸(全部或按品种筛选)

  • mt5_get_orders - 获取挂单

订单操作

  • mt5_place_order - 下达市价或挂单

    • 支持:buy、sell、buy_limit、sell_limit、buy_stop、sell_stop

    • 可选的止损/止盈

  • mt5_close_position - 按订单号平仓

  • mt5_cancel_order - 取消挂单

  • mt5_modify_position - 修改开仓头寸的止损/止盈

市场数据

  • mt5_get_symbol_info - 获取合约规格

  • mt5_get_tick - 获取当前买/卖价

行情 / 蜡烛图

  • mt5_get_rates_from_pos - 从指定位置开始获取 OHLCV 数据(0=当前柱),需提供 start_pos 和 count

  • mt5_get_rates_range - 在 ISO 日期范围 date_from → date_to 内获取 OHLCV 数据

指标

所有指标工具支持直接数据(收盘价/蜡烛图)或通过 symbol + timeframe 自动获取,并配合 date_from/date_to 或 start_pos/count。设置 return_last_only=true 仅返回最新值。

  • mt5_calc_sma - 简单移动平均线

    • 参数:period(默认 20)

  • mt5_calc_ema - 指数移动平均线

    • 参数:period(默认 20)

  • mt5_calc_rsi - 相对强弱指数

    • 参数:period(默认 14)

  • mt5_calc_macd - MACD 线、信号线、柱状图

    • 参数:fast(12)、slow(26)、signal(9)

  • mt5_calc_bbands - 布林带(上轨/中轨/下轨)

    • 参数:period(20)、stddev(2.0)

  • mt5_calc_atr - 平均真实波幅(需要蜡烛图)

    • 参数:period(14)

  • mt5_calc_stochastic - 随机振荡指标 %K/%D(需要蜡烛图)

    • 参数:k_period(14)、d_period(3)、smooth_k(1)

批量指标

  • mt5_calc_bundle - 使用同一组获取的蜡烛图数据,单次调用计算多个指标

    • 参数:

      • indicators:名称数组,可选值 ["sma","ema","rsi","macd","bbands","atr","stochastic"]

      • 可选的 params 对象,用于覆盖单个指标参数(例如 { "sma": {"period": 50}, "bbands": {"stddev": 2.5} })

      • 支持 symbol/timeframe 配合范围/数量,或直接使用 candles/closes

      • return_last_only:若为 true,仅返回最新值

信号工具

  • mt5_signal_crossover - EMA 交叉和趋势标志

    • 参数:fast(默认 50)、slow(默认 200)、lookback_bars(默认 200)

    • 返回:state(above/below/equal)、crossed_up、crossed_down、age_bars、slope_fast、slope_slow、spread、spread_pct、price_above_both、price_below_both、price

  • mt5_signal_confluence - 入场一致性评分

    • 指标参数:ema_fast(50)、ema_slow(200)、ema_near(20)、rsi_period(14)、macd_fast(12)、macd_slow(26)、macd_signal(9)、bb_period(20)、bb_stddev(2.0)、atr_period(14)

    • 阈值:near_k_atr(0.5)、atr_expansion_ratio(1.0)、score_threshold(3)

    • 控制:direction(auto|long|short)、可选的 weights(趋势/动量/波动性/位置/触发)

    • 返回多空双方的组件标志和评分,以及建议方向

使用示例

# In Claude Desktop, you can now ask:

"What's my MT5 account balance?"
"Show me my open positions on EURUSD"
"Place a buy order for 0.1 lot EURUSD at market"
"Close position with ticket 12345"
"Get current price for XAUUSD"

# Indicators (auto-fetch candles)
"Compute RSI(14) on EURUSD H1 using the last 300 bars (latest value only)"
"Compute MACD and Bollinger Bands on EURUSD H4 for September"

# Bundle
"Compute RSI+MACD+BBands (latest values) on EURUSD H1 using last 300 bars"

# Signals
"Give me EMA(50/200) crossover regime info on EURUSD H1"
"Compute confluence score on EURUSD H1 (defaults) and tell me if entry is ready"

系统要求

  • Windows 操作系统(MT5 仅支持 Windows)

  • 已安装并运行的 MetaTrader5 终端

  • Python >=3.11

  • 有效的 MT5 交易账户

架构

mt5-mcp/
├── mt5_mcp/
│   ├── __init__.py          # Package init
│   ├── server.py            # MCP server with tool definitions
│   ├── mt5_service.py       # MT5 SDK wrapper
│   └── indicators.py        # Pure-Python technical indicators
├── pyproject.toml           # Project dependencies (uv)
├── .env.example             # Environment template
└── README.md

开发

以开发模式运行:

uv run python mt5_mcp/server.py

许可证

MIT

注意事项

  • 软件按现状提供,软件作者不对交易造成的任何损失负责。外汇交易具有高度波动性和风险,请极度谨慎操作。

  • 使用此 MCP 时,MT5 终端必须处于运行状态

  • 确保您的账户已启用交易权限

  • 在实盘使用前,请先在模拟账户上测试

Related MCP Connectors

Related MCP Servers

  • A
    license
    Not graded
    quality
    D
    maintenance
    Enables access to MetaTrader5 market data and trading functionality, including real-time quotes, historical OHLCV data, tick data, symbol information, and technical indicators for forex and other trading instruments.
    22
    MIT
  • A
    license
    Not graded
    quality
    A
    maintenance
    Enables AI assistants to connect to MetaTrader 5 for trading, market data access, and account management through the Model Context Protocol.
    106 PyPI
    236
    MIT
  • F
    license
    Not graded
    quality
    D
    maintenance
    Enables AI applications to interact with MetaTrader 5 terminals via WebSocket MCP protocol for trading operations and account management.
    4
    -
  • A
    license
    Not graded
    quality
    D
    maintenance
    Enables interaction with MetaTrader 5 for market data, technical analysis, Fibonacci calculations, and trading via MCP clients such as Claude.
    MIT