MetaTrader 5 plugin for bt_api, supporting multi-asset trading via MT5 Web API.
English | 中文
This package provides a MetaTrader 5 gateway adapter for the bt_api framework. It connects to MT5 terminals via the pymt5 WebSocket client and exposes a unified interface for trading and market data.
- MT5 WebSocket connection via
pymt5library - Real-time tick data via WebSocket push notifications
- Order placement — market, limit, stop orders with SL/TP support
- Position & balance tracking — open positions, account balance, equity
- Historical bars — M1/M5/M15/M30/H1/H4/D1/W1/MN1 timeframes
- Symbol subscription — batch subscribe to multiple symbols
- Order management — place, cancel, track open orders
- Python 3.9+
bt_api_base >= 0.15pymt5 >= 0.5.0(Windows/macOS only — MT5 terminal required)- A running MT5 terminal with WebAPI enabled
pip install bt_api_mt5Or install from source:
git clone https://github.com/cloudQuant/bt_api_mt5
cd bt_api_mt5
pip install -e .Note:
pymt5connects to a running MT5 terminal via WebSocket. MT5 only runs on Windows and macOS. Linux users can install the package but will not be able to establish connections without a remote MT5 terminal.
frombt_api_mt5importMt5GatewayAdapter# Initialize the adapteradapter=Mt5GatewayAdapter(
login=12345678, # MT5 account loginpassword="your_password",
ws_uri="ws://localhost:8080", # MT5 WebAPI URIsymbol_suffix="", # optional: symbol suffix mapping
)
# Connect to MT5adapter.connect()
# Subscribe to symbolsadapter.subscribe_symbols(["EURUSD", "GBPUSD"])
# Get account balancebalance=adapter.get_balance()
print(balance)
# Get open positionspositions=adapter.get_positions()
print(positions)
# Place a limit orderorder_result=adapter.place_order({
"symbol": "EURUSD",
"volume": 0.1,
"price": 1.0850,
"order_type": "limit",
"direction": "buy",
"sl": 1.0800,
"tp": 1.0900,
})
print(order_result)
# Cancel an ordercancel_result=adapter.cancel_order({"order_id": 12345678})
print(cancel_result)
# Get historical barsbars=adapter.get_bars("EURUSD", "H1", 100)
print(f"Got {len(bars)} bars")
# Disconnectadapter.disconnect()| Operation | Method | Status |
|---|---|---|
| Connect | connect() | ✅ |
| Disconnect | disconnect() | ✅ |
| Subscribe symbols | subscribe_symbols(symbols) | ✅ |
| Account balance | get_balance() | ✅ |
| Open positions | get_positions() | ✅ |
| Place order | place_order(payload) | ✅ |
| Cancel order | cancel_order(payload) | ✅ |
| Historical bars | get_bars(symbol, timeframe, count) | ✅ |
| Symbol info | get_symbol_info(symbol) | ✅ |
| Open orders | get_open_orders() | ✅ |
{
"symbol": "EURUSD", # Trading symbol"volume": 0.1, # Order volume (lots)"price": 1.0850, # Limit price (for limit/stop orders)"order_type": "limit", # "market", "limit", "stop""direction": "buy", # "buy" or "sell""sl": 1.0800, # Stop loss price (optional)"tp": 1.0900, # Take profit price (optional)"comment": "order comment"# Optional comment
}The adapter maintains a symbol map for mapping bt_api symbol names to MT5 symbol names. Configure it via the symbol_map kwarg:
adapter=Mt5GatewayAdapter(
login=12345678,
password="your_password",
ws_uri="ws://localhost:8080",
symbol_map={
"EURUSD": "EURUSD",
"XAUUSD": "GOLD",
},
symbol_suffix=""# optional suffix appended to all symbols
)bt_api_mt5/
├── src/bt_api_mt5/
│ ├── __init__.py # Package init, exports Mt5GatewayAdapter
│ ├── plugin.py # bt_api plugin registration
│ └── gateway/
│ ├── __init__.py
│ └── adapter.py # Mt5GatewayAdapter implementation
├── tests/
│ └── conftest.py # Pytest configuration
└── docs/
└── index.md # Documentation
| Resource | Link |
|---|---|
| English Docs | https://bt-api-mt5.readthedocs.io/ |
| Chinese Docs | https://bt-api-mt5.readthedocs.io/zh/latest/ |
| GitHub Repository | https://github.com/cloudQuant/bt_api_mt5 |
| Issue Tracker | https://github.com/cloudQuant/bt_api_mt5/issues |
MIT License - see LICENSE for details.
- Report bugs via GitHub Issues
- Email: yunjinqi@gmail.com
本包为 bt_api 框架提供 MetaTrader 5 网关适配器。通过 pymt5 WebSocket 客户端连接 MT5 终端,提供统一的交易和行情数据接口。
- MT5 WebSocket 连接 — 通过
pymt5库连接 MT5 终端 - 实时 tick 数据 — WebSocket 推送行情
- 下单 — 市价单、限价单、止损单,支持 SL/TP
- 持仓与余额查询 — 持仓、账户余额、权益
- 历史 K 线 — M1/M5/M15/M30/H1/H4/D1/W1/MN1 时间周期
- 品种订阅 — 批量订阅多个交易品种
- 订单管理 — 下单、撤单、查询挂单
- Python 3.9+
bt_api_base >= 0.15pymt5 >= 0.5.0(仅 Windows/macOS,需运行 MT5 终端)- 已启用 WebAPI 的 MT5 终端
pip install bt_api_mt5或从源码安装:
git clone https://github.com/cloudQuant/bt_api_mt5
cd bt_api_mt5
pip install -e .注意:
pymt5通过 WebSocket 连接运行中的 MT5 终端。MT5 仅在 Windows 和 macOS 上运行。Linux 用户可以安装包,但需要远程 MT5 终端才能建立连接。
frombt_api_mt5importMt5GatewayAdapter# 初始化适配器adapter=Mt5GatewayAdapter(
login=12345678, # MT5 账户登录名password="your_password",
ws_uri="ws://localhost:8080", # MT5 WebAPI 地址symbol_suffix="", # 可选:品种后缀映射
)
# 连接 MT5adapter.connect()
# 订阅品种adapter.subscribe_symbols(["EURUSD", "GBPUSD"])
# 查询账户余额balance=adapter.get_balance()
print(balance)
# 查询持仓positions=adapter.get_positions()
print(positions)
# 下限价单order_result=adapter.place_order({
"symbol": "EURUSD",
"volume": 0.1,
"price": 1.0850,
"order_type": "limit",
"direction": "buy",
"sl": 1.0800,
"tp": 1.0900,
})
print(order_result)
# 撤单cancel_result=adapter.cancel_order({"order_id": 12345678})
print(cancel_result)
# 查询历史 K 线bars=adapter.get_bars("EURUSD", "H1", 100)
print(f"获取了 {len(bars)} 根 K 线")
# 断开连接adapter.disconnect()| 操作 | 方法 | 状态 |
|---|---|---|
| 连接 | connect() | ✅ |
| 断开 | disconnect() | ✅ |
| 订阅品种 | subscribe_symbols(symbols) | ✅ |
| 账户余额 | get_balance() | ✅ |
| 持仓查询 | get_positions() | ✅ |
| 下单 | place_order(payload) | ✅ |
| 撤单 | cancel_order(payload) | ✅ |
| 历史 K 线 | get_bars(symbol, timeframe, count) | ✅ |
| 品种信息 | get_symbol_info(symbol) | ✅ |
| 挂单查询 | get_open_orders() | ✅ |
{
"symbol": "EURUSD", # 交易品种"volume": 0.1, # 委托数量(手)"price": 1.0850, # 委托价格(限价单/止损单)"order_type": "limit", # "market", "limit", "stop""direction": "buy", # "buy" 或 "sell""sl": 1.0800, # 止损价格(可选)"tp": 1.0900, # 止盈价格(可选)"comment": "订单备注"# 可选备注
}适配器维护品种映射表,通过 symbol_map 参数配置 bt_api 品种名到 MT5 品种名的映射:
adapter=Mt5GatewayAdapter(
login=12345678,
password="your_password",
ws_uri="ws://localhost:8080",
symbol_map={
"EURUSD": "EURUSD",
"XAUUSD": "GOLD",
},
symbol_suffix=""
)bt_api_mt5/
├── src/bt_api_mt5/
│ ├── __init__.py # 包初始化,导出 Mt5GatewayAdapter
│ ├── plugin.py # bt_api 插件注册
│ └── gateway/
│ ├── __init__.py
│ └── adapter.py # Mt5GatewayAdapter 实现
├── tests/
│ └── conftest.py # Pytest 配置
└── docs/
└── index.md # 文档
| 资源 | 链接 |
|---|---|
| 英文文档 | https://bt-api-mt5.readthedocs.io/ |
| 中文文档 | https://bt-api-mt5.readthedocs.io/zh/latest/ |
| GitHub 仓库 | https://github.com/cloudQuant/bt_api_mt5 |
| 问题反馈 | https://github.com/cloudQuant/bt_api_mt5/issues |
MIT 许可证 - 详见 LICENSE。
- 通过 GitHub Issues 反馈问题
- 邮箱: yunjinqi@gmail.com