跳到主要内容

切换保证金模式

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_modeSTRINGYES"isolated""unified"

响应示例

{
"success": true,
"margin_mode": "unified",
"uni_mmr": null,
"available_balance": "0"
}

错误码

HTTPCode含义
400INVALID_MARGIN_MODEmargin_mode 不是 "isolated" / "unified"
400HAS_OPEN_ORDERS存在未结束的订单,需先撤销
400HAS_OPEN_POSITIONS切回 isolated 时仍有开仓
400UNI_MMR_TOO_LOW切换到 unifieduniMMR < 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())