refactor(trader): update SetMarginMode to avoid legacy endpoint and improve logging

This commit is contained in:
Dean
2026-04-14 23:34:35 +08:00
parent a3d8831b36
commit c2fc80e269
2 changed files with 33 additions and 22 deletions

View File

@@ -82,31 +82,14 @@ func (t *OKXTrader) GetBalance() (map[string]interface{}, error) {
// SetMarginMode sets margin mode
func (t *OKXTrader) SetMarginMode(symbol string, isCrossMargin bool) error {
instId := t.convertSymbol(symbol)
t.isCrossMargin = isCrossMargin
mgnMode := t.marginMode()
body := map[string]interface{}{
"instId": instId,
"mgnMode": mgnMode,
}
_, err := t.doRequest("POST", "/api/v5/account/set-isolated-mode", body)
if err != nil {
// Ignore error if already in target mode
if strings.Contains(err.Error(), "already") {
logger.Infof(" ✓ %s margin mode is already %s", symbol, mgnMode)
return nil
}
// Cannot change when there are positions
if strings.Contains(err.Error(), "position") {
logger.Infof(" ⚠️ %s has positions, cannot change margin mode", symbol)
return nil
}
return err
}
logger.Infof(" ✓ %s margin mode set to %s", symbol, mgnMode)
// OKX V5 unified account applies cross/isolated per order via tdMode,
// while leverage uses mgnMode on /account/set-leverage.
// Persist the configured mode locally so subsequent leverage/order calls use it,
// instead of calling the legacy isolated-mode endpoint that returns 51000 errors.
logger.Infof(" ✓ %s margin mode configured as %s (applied via tdMode/mgnMode on subsequent requests)", symbol, mgnMode)
return nil
}