MetaTrader 5 MCP服务器
一种模型上下文协议(MCP)服务器,为MetaTrader5(MT5)平台提供交易接口。
特性
- 账户管理:获取账户信息、余额、权益、保证金
- 职位管理:查看、修改和关闭空缺职位
- 订单操作:下单/挂单,取消订单
- 市场数据:获取交易品种信息、当前价格(报价)
- 价格/蜡烛:通过start_pos/count或日期范围获取OHLCV
- 指标:计算SMA、EMA、RSI、MACD、布林带、ATR、随机指标
- Bundle Indicators:在一次调用中计算多个指标
- 信号工具:EMA交叉机制标志和参赛作品的汇流评分
安装
使用uv安装依赖项:
uv sync配置
环境变量现在直接在MCP客户端设置中配置(否 .env 需要文件)。
克劳德桌面/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-下达市价单或挂单
- 支持:买入、卖出、买入_限制、卖出_限制、买入_停止、卖出_停止 - 可选SL/TP
mt5_close_position-凭票平仓mt5_cancel_order-取消待处理订单mt5_modify_position-修改未平仓头寸的SL/TP
市场数据
mt5_get_symbol_info-获取合同规范mt5_get_tick-获取当前买入价/卖出价
价格/蜡烛
mt5_get_rates_from_pos-从某个位置(0=当前柱状图)开始获取OHLCVstart_pos和countmt5_get_rates_range-在ISO日期范围内获取OHLCVdate_from→date_to
指标
所有指标工具都支持直接数据(关闭/蜡烛)或通过自动获取 symbol + timeframe 加上 date_from/date_to 或 start_pos/count.Set 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-使用相同的提取蜡烛在一次调用中计算多个指标
- Args: - indicators:来自\[“sma”、“ema”、”rsi“、”macd“、”bbands“、”atr“、”randomic“\]的名称数组 - 可选的 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 (高于/低于/等于), 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 (自动|长|短),可选 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上运行)
- MetaTrader 5终端已安装并正在运行
- 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许可证
麻省理工学院
备注
- 使用此MCP时,MT5终端必须正在运行
- 确保您的帐户已启用交易权限
- 在使用真钱之前,先在模拟账户上进行测试
