切换保证金模式
在 isolated(逐仓)与 unified(统一保证金)之间切换当前用户的账户模式。
HTTP 请求
POST /api/v1/account/margin-mode (HMAC SHA256)
前置条件
| 目标模式 | 必须满足 |
|---|---|
unified | 无 pending / open / partially_filled 订单;如有持仓则切换后 uniMMR ≥ 1.10,否则自动回滚并返回 400 UNI_MMR_TOO_LOW |
isolated | 无 pending / open / partially_filled 订单,且无任何开仓 |
请求体
| 名称 | 类型 | 是否必需 | 描述 |
|---|---|---|---|
| margin_mode | STRING | YES | "isolated" 或 "unified" |
响应示例
{
"success": true,
"margin_mode": "unified",
"uni_mmr": null,
"available_balance": "0"
}
错误码
| HTTP | Code | 含义 |
|---|---|---|
| 400 | INVALID_MARGIN_MODE | margin_mode 不是 "isolated" / "unified" |
| 400 | HAS_OPEN_ORDERS | 存在未结束的订单,需先撤销 |
| 400 | HAS_OPEN_POSITIONS | 切回 isolated 时仍有开仓 |
| 400 | UNI_MMR_TOO_LOW | 切换到 unified 后 uniMMR < 1.10,切换已回滚 |
代码示例
curl
API_KEY="your_api_key"
API_SECRET="your_api_secret"
TIMESTAMP=$(date +%s%3N)
BODY='{"margin_mode":"unified"}'
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/account/margin-mode?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({"margin_mode": "unified"}, 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/account/margin-mode?timestamp={ts}&signature={sig}",
headers={"X-MBX-APIKEY": API_KEY, "Content-Type": "application/json"},
data=body,
)
print(r.json())