下单预览(事前风险模拟)
模拟"如果此时开一个仓位"对 uniMMR 的影响。前端可据此在用户提交订单前
提示"最大可安全杠杆"或"这单会让你进入 margin-call 区"。
该接口是纯计算,不下单、不改变任何状态。
HTTP 请求
POST /api/v1/unified/risk/simulate (HMAC SHA256)
请求体
| 名称 | 类型 | 是否必需 | 描述 |
|---|---|---|---|
| symbol | STRING | YES | 如 "BTCUSDT" |
| side | STRING | NO | "long" / "short",当前版本仅作信息,不影响计算 |
| size_usd | DECIMAL | YES | 假设开仓的名义价值(美元,正数) |
| leverage | INT | YES | 杠杆倍数(≥ 1,≤ 该 symbol 允许的最大值) |
响应示例
{
"current_uni_mmr": "3.50",
"simulated_uni_mmr": "2.80",
"new_initial_margin": "2500.00",
"new_maint_margin": "250.000000",
"available_after": "42500.00",
"can_open": true,
"reason": null
}
字段说明
| 字段 | 含义 |
|---|---|
current_uni_mmr | 模拟前的 uniMMR |
simulated_uni_mmr | 假设按当前 mark 成交后的 uniMMR |
new_initial_margin | size_usd / leverage |
new_maint_margin | 新仓位按梯度表贡献的 MM |
available_after | total_equity − (现有初始保证金 + new_initial_margin) |
can_open | 所有校验通过则为 true |
reason | can_open == false 时的英文简短原因 |
校验规则
以下任一条件导致 can_open == false:
account_status == reduce_only→"account in reduce_only mode"account_status == liquidating→"account is liquidating"available_after < 0→"insufficient available balance after opening"simulated_uni_mmr < 1.10→"simulated uniMMR would fall below 1.10"
错误码
| HTTP | Code | 含义 |
|---|---|---|
| 400 | INVALID_LEVERAGE | leverage < 1 |
| 400 | INVALID_SIZE | size_usd <= 0 |
代码示例
curl
API_KEY="your_api_key"
API_SECRET="your_api_secret"
TIMESTAMP=$(date +%s%3N)
BODY='{"symbol":"BTCUSDT","side":"long","size_usd":"5000","leverage":10}'
PAYLOAD="timestamp=${TIMESTAMP}${BODY}"
SIGNATURE=$(echo -n "${PAYLOAD}" | openssl dgst -sha256 -hmac "${API_SECRET}" | awk '{print $2}')
curl -s -X POST \
-H "X-MBX-APIKEY: ${API_KEY}" \
-H "Content-Type: application/json" \
--data "${BODY}" \
"https://api.ztdx.io/api/v1/unified/risk/simulate?timestamp=${TIMESTAMP}&signature=${SIGNATURE}"
Python
import time, hmac, hashlib, json, requests
API_KEY = "your_api_key"
API_SECRET = "your_api_secret"
ts = int(time.time() * 1000)
body = json.dumps({"symbol": "BTCUSDT", "size_usd": "5000", "leverage": 10},
separators=(",", ":"))
payload = f"timestamp={ts}{body}"
sig = hmac.new(API_SECRET.encode(), payload.encode(), hashlib.sha256).hexdigest()
r = requests.post(
f"https://api.ztdx.io/api/v1/unified/risk/simulate?timestamp={ts}&signature={sig}",
headers={"X-MBX-APIKEY": API_KEY, "Content-Type": "application/json"},
data=body,
)
print(r.json())