Fix/binance server time (#453)

* Fix Binance futures server time sync
* Fix Binance server time sync; clean up logging and restore decision sorting
---------
Co-authored-by: tinkle-community <tinklefund@gmail.com>
This commit is contained in:
zcan
2025-11-05 17:09:47 +08:00
committed by GitHub
parent f29e4b18d7
commit 7ab2dbcc8d
2 changed files with 20 additions and 2 deletions

View File

@@ -33,6 +33,8 @@ type FuturesTrader struct {
// NewFuturesTrader 创建合约交易器
func NewFuturesTrader(apiKey, secretKey string) *FuturesTrader {
client := futures.NewClient(apiKey, secretKey)
// 同步时间,避免 Timestamp ahead 错误
syncBinanceServerTime(client)
trader := &FuturesTrader{
client: client,
cacheDuration: 15 * time.Second, // 15秒缓存
@@ -69,6 +71,20 @@ func (t *FuturesTrader) setDualSidePosition() error {
return nil
}
// syncBinanceServerTime 同步币安服务器时间,确保请求时间戳合法
func syncBinanceServerTime(client *futures.Client) {
serverTime, err := client.NewServerTimeService().Do(context.Background())
if err != nil {
log.Printf("⚠️ 同步币安服务器时间失败: %v", err)
return
}
now := time.Now().UnixMilli()
offset := now - serverTime
client.TimeOffset = offset
log.Printf("⏱ 已同步币安服务器时间,偏移 %dms", offset)
}
// GetBalance 获取账户余额(带缓存)
func (t *FuturesTrader) GetBalance() (map[string]interface{}, error) {
// 先检查缓存是否有效