mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-18 18:04:32 +08:00
refactor(trader): update SetMarginMode to avoid legacy endpoint and improve logging
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user