Merge pull request #433 from zhouyongyou/fix/dual-side-position-mode

fix(binance): initialize dual-side position mode to prevent code=-4061 errors
This commit is contained in:
Icyoung
2025-11-05 16:09:37 +08:00
committed by GitHub

View File

@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"log" "log"
"strconv" "strconv"
"strings"
"sync" "sync"
"time" "time"
@@ -32,10 +33,40 @@ type FuturesTrader struct {
// NewFuturesTrader 创建合约交易器 // NewFuturesTrader 创建合约交易器
func NewFuturesTrader(apiKey, secretKey string) *FuturesTrader { func NewFuturesTrader(apiKey, secretKey string) *FuturesTrader {
client := futures.NewClient(apiKey, secretKey) client := futures.NewClient(apiKey, secretKey)
return &FuturesTrader{ trader := &FuturesTrader{
client: client, client: client,
cacheDuration: 15 * time.Second, // 15秒缓存 cacheDuration: 15 * time.Second, // 15秒缓存
} }
// 设置双向持仓模式Hedge Mode
// 这是必需的,因为代码中使用了 PositionSide (LONG/SHORT)
if err := trader.setDualSidePosition(); err != nil {
log.Printf("⚠️ 设置双向持仓模式失败: %v (如果已是双向模式则忽略此警告)", err)
}
return trader
}
// setDualSidePosition 设置双向持仓模式(初始化时调用)
func (t *FuturesTrader) setDualSidePosition() error {
// 尝试设置双向持仓模式
err := t.client.NewChangePositionModeService().
DualSide(true). // true = 双向持仓Hedge Mode
Do(context.Background())
if err != nil {
// 如果错误信息包含"No need to change",说明已经是双向持仓模式
if strings.Contains(err.Error(), "No need to change position side") {
log.Printf(" ✓ 账户已是双向持仓模式Hedge Mode")
return nil
}
// 其他错误则返回(但在调用方不会中断初始化)
return err
}
log.Printf(" ✓ 账户已切换为双向持仓模式Hedge Mode")
log.Printf(" 双向持仓模式允许同时持有多单和空单")
return nil
} }
// GetBalance 获取账户余额(带缓存) // GetBalance 获取账户余额(带缓存)