mt5-httpapi
REST API on top of MetaTrader 5 running inside a Windows VM. Talk to it with plain HTTP/JSON — no MT5 libraries, no Windows, no bullshit. Just curl and go.
For installation and setup, see references/setup.md.
Setup
The API should already be running. Set the base URL and token:
CODEBLOCK0
Each terminal has its own port (configured in terminals.json). If running multiple terminals, set MT5_API_URL to the port for the terminal you want to talk to.
Verify: curl -H "Authorization: Bearer $MT5_API_TOKEN" $MT5_API_URL/ping — should return {"status": "ok"}. If not, the API isn't up yet (may still be initializing — it retries in the background).
Auth is optional — if no token is configured on the server, all requests go through without a token. If a token is configured, all endpoints require Authorization: Bearer <token> and return 401 without it.
How It Works
GET for reading, POST for creating, PUT for modifying, DELETE for closing/canceling. All bodies are JSON.
Every error response:
CODEBLOCK1
Pre-Trade Checks (DO NOT SKIP)
Before placing any trade:
- 1.
GET /account → trade_allowed must be INLINECODE8 - INLINECODE9 →
trade_mode must be 4 (full trading) - INLINECODE12 → check
trade_contract_size — 1 lot of EURUSD = 100,000 EUR, not 1 EUR - INLINECODE14 →
connected must be INLINECODE16
API Reference
Health
CODEBLOCK2
Terminal
CODEBLOCK3
Key fields on /terminal: connected, trade_allowed, build, company.
Account
CODEBLOCK4
CODEBLOCK5
Symbols
CODEBLOCK6
Timeframes: M1 M2 M3 M4 M5 M6 M10 M12 M15 M20 M30 H1 H2 H3 H4 H6 H8 H12 D1 W1 INLINECODE42
Key symbol fields: bid, ask, digits, point, trade_contract_size, trade_tick_value, trade_tick_size, volume_min, volume_max, volume_step, spread, swap_long, swap_short, trade_stops_level, trade_mode.
Orders
CODEBLOCK7
Order types: BUY, SELL, BUY_LIMIT, SELL_LIMIT, BUY_STOP, SELL_STOP, BUY_STOP_LIMIT, INLINECODE65
Fill policies: FOK, IOC (default), INLINECODE68
Expiration: GTC (default), DAY, SPECIFIED, INLINECODE72
Required fields: symbol, type, volume. price auto-fills for market orders.
Trade result:
CODEBLOCK8
INLINECODE77 10009 = success. Anything else = something went wrong.
Positions
CODEBLOCK9
Key position fields: ticket, type (0=buy, 1=sell), volume, price_open, price_current, sl, tp, profit, swap.
History
CODEBLOCK10
INLINECODE87 and to are required, unix epoch seconds.
Deal fields: type (0=buy, 1=sell), entry (0=opening, 1=closing), profit (0 for entries, realized P&L for exits).
Position Sizing
CODEBLOCK11
Round down to nearest volume_step, clamp to [volume_min, volume_max]. Sanity check: volume * trade_contract_size * price should make sense relative to account balance.
Tips
- - Always check
retcode — 10009 = good, anything else = bad - Use
GET /error to debug failed trades - INLINECODE97 on orders = max slippage in points (default 20, raise for volatile markets)
- INLINECODE98 matters — try
FOK, IOC, RETURN if orders get rejected - Candle
time is the open time, not close time - INLINECODE103 = minimum SL/TP distance from current price in points
- Markets have hours — check
trade_mode before placing orders
mt5-httpapi
基于运行在Windows虚拟机中的MetaTrader 5的REST API。通过纯HTTP/JSON与其通信——无需MT5库、无需Windows、无需繁琐操作。只需curl即可使用。
安装和设置请参见references/setup.md。
设置
API应已在运行中。设置基础URL和令牌:
bash
export MT5APIURL=http://localhost:6542
export MT5APITOKEN=yourtokenhere
每个终端拥有独立的端口(在terminals.json中配置)。如果运行多个终端,请将MT5APIURL设置为您要通信的终端端口。
验证: curl -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/ping — 应返回{status: ok}。否则,API尚未启动(可能仍在初始化——它会在后台重试)。
认证是可选的——如果服务器未配置令牌,所有请求无需令牌即可通过。如果配置了令牌,所有端点都需要Authorization: Bearer ,否则返回401。
工作原理
GET用于读取,POST用于创建,PUT用于修改,DELETE用于关闭/取消。所有请求体均为JSON格式。
每个错误响应:
json
{error: 问题描述}
交易前检查(请勿跳过)
在下单前:
- 1. GET /account → tradeallowed必须为true
- GET /symbols/SYMBOL → trademode必须为4(完全交易)
- GET /symbols/SYMBOL → 检查tradecontractsize — 1手EURUSD = 100,000 EUR,而非1 EUR
- GET /terminal → connected必须为true
API参考
健康检查
bash
curl -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/ping
{status: ok}
curl -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/error
{code: 1, message: Success}
终端
bash
curl -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/terminal
curl -X POST -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/terminal/init
curl -X POST -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/terminal/shutdown
curl -X POST -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/terminal/restart
/terminal的关键字段:connected、trade_allowed、build、company。
账户
bash
curl -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/account
json
{
login: 12345678,
balance: 10000.0,
equity: 10000.0,
margin: 0.0,
margin_free: 10000.0,
margin_level: 0.0,
leverage: 500,
currency: USD,
trade_allowed: true,
marginsocall: 70.0,
marginsoso: 20.0
}
交易品种
bash
curl -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/symbols
curl -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/symbols?group=USD
curl -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/symbols/EURUSD
curl -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/symbols/EURUSD/tick
curl -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/symbols/EURUSD/rates?timeframe=H4&count=100
curl -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/symbols/EURUSD/ticks?count=100
时间周期:M1 M2 M3 M4 M5 M6 M10 M12 M15 M20 M30 H1 H2 H3 H4 H6 H8 H12 D1 W1 MN1
关键品种字段:bid、ask、digits、point、tradecontractsize、tradetickvalue、tradeticksize、volumemin、volumemax、volumestep、spread、swaplong、swapshort、tradestopslevel、trademode。
挂单
bash
下市价单
curl -X POST -H Authorization: Bearer $MT5
APITOKEN $MT5
APIURL/orders \
-H Content-Type: application/json \
-d {symbol: EURUSD, type: BUY, volume: 0.1, sl: 1.08, tp: 1.10}
查看挂单列表
curl -H Authorization: Bearer $MT5
APITOKEN $MT5
APIURL/orders
curl -H Authorization: Bearer $MT5
APITOKEN $MT5
APIURL/orders?symbol=EURUSD
curl -H Authorization: Bearer $MT5
APITOKEN $MT5
APIURL/orders/42094812
修改挂单
curl -X PUT -H Authorization: Bearer $MT5
APITOKEN $MT5
APIURL/orders/42094812 \
-H Content-Type: application/json \
-d {price: 1.09, sl: 1.07, tp: 1.11}
取消挂单
curl -X DELETE -H Authorization: Bearer $MT5
APITOKEN $MT5
APIURL/orders/42094812
订单类型:BUY、SELL、BUYLIMIT、SELLLIMIT、BUYSTOP、SELLSTOP、BUYSTOPLIMIT、SELLSTOPLIMIT
成交策略:FOK、IOC(默认)、RETURN
过期时间:GTC(默认)、DAY、SPECIFIED、SPECIFIED_DAY
必填字段:symbol、type、volume。市价单的price会自动填充。
交易结果:
json
{
retcode: 10009,
deal: 40536203,
order: 42094820,
volume: 0.1,
price: 1.0950,
comment: Request executed
}
retcode 10009 = 成功。其他值 = 出现问题。
持仓
bash
curl -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/positions
curl -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/positions?symbol=EURUSD
curl -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/positions/42094820
更新止损/止盈
curl -X PUT -H Authorization: Bearer $MT5
APITOKEN $MT5
APIURL/positions/42094820 \
-H Content-Type: application/json \
-d {sl: 1.085, tp: 1.105}
平全仓
curl -X DELETE -H Authorization: Bearer $MT5
APITOKEN $MT5
APIURL/positions/42094820
部分平仓
curl -X DELETE -H Authorization: Bearer $MT5
APITOKEN $MT5
APIURL/positions/42094820 \
-H Content-Type: application/json \
-d {volume: 0.05}
关键持仓字段:ticket、type(0=买入,1=卖出)、volume、priceopen、pricecurrent、sl、tp、profit、swap。
历史记录
bash
curl -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/history/orders?from=$(date -d 1 day ago +%s)&to=$(date +%s)
curl -H Authorization: Bearer $MT5APITOKEN $MT5APIURL/history/deals?from=$(date -d 1 day ago +%s