Skip to main content

Switch Margin Mode

Switch the authenticated user between isolated (per-position margin) and unified (portfolio margin) modes.

HTTP Request

POST /api/v1/account/margin-mode (HMAC SHA256)

Preconditions

TargetRequired state
unifiedNo pending / open / partially-filled orders; new uniMMR (if positions exist) must be ≥ 1.10 — otherwise the switch is rolled back and the call returns 400 UNI_MMR_TOO_LOW
isolatedNo pending / open / partially-filled orders AND no open positions

Request Body

NameTypeRequiredDescription
margin_modeSTRINGYES"isolated" or "unified"

Response Example

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

Error Codes

HTTPCodeMeaning
400INVALID_MARGIN_MODEmargin_mode is not "isolated" / "unified"
400HAS_OPEN_ORDERSUser has non-terminal orders — cancel them first
400HAS_OPEN_POSITIONSSwitching to isolated while positions are still open
400UNI_MMR_TOO_LOWuniMMR would be below 1.10 after switching to unified — switch rolled back

Code Examples

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())