@@ -489,12 +489,27 @@ func (t *AsterTrader) GetBalance() (map[string]interface{}, error) {
489489 totalMarginUsed := 0.0
490490 realUnrealizedPnl := 0.0
491491 for _ , pos := range positions {
492- markPrice := pos ["markPrice" ].(float64 )
493- quantity := pos ["positionAmt" ].(float64 )
492+ // 安全地提取浮点数值,避免 panic
493+ markPrice , err := SafeFloat64 (pos , "markPrice" )
494+ if err != nil {
495+ log .Printf ("⚠️ 无法解析 markPrice: %v" , err )
496+ continue
497+ }
498+
499+ quantity , err := SafeFloat64 (pos , "positionAmt" )
500+ if err != nil {
501+ log .Printf ("⚠️ 无法解析 positionAmt: %v" , err )
502+ continue
503+ }
494504 if quantity < 0 {
495505 quantity = - quantity
496506 }
497- unrealizedPnl := pos ["unRealizedProfit" ].(float64 )
507+
508+ unrealizedPnl , err := SafeFloat64 (pos , "unRealizedProfit" )
509+ if err != nil {
510+ log .Printf ("⚠️ 无法解析 unRealizedProfit: %v" , err )
511+ continue
512+ }
498513 realUnrealizedPnl += unrealizedPnl
499514
500515 leverage := 10
@@ -544,11 +559,21 @@ func (t *AsterTrader) GetPositions() ([]map[string]interface{}, error) {
544559 continue // 跳过空仓位
545560 }
546561
547- entryPrice , _ := strconv .ParseFloat (pos ["entryPrice" ].(string ), 64 )
548- markPrice , _ := strconv .ParseFloat (pos ["markPrice" ].(string ), 64 )
549- unRealizedProfit , _ := strconv .ParseFloat (pos ["unRealizedProfit" ].(string ), 64 )
550- leverageVal , _ := strconv .ParseFloat (pos ["leverage" ].(string ), 64 )
551- liquidationPrice , _ := strconv .ParseFloat (pos ["liquidationPrice" ].(string ), 64 )
562+ // 安全地提取并解析价格数据
563+ entryPriceStr , _ := SafeString (pos , "entryPrice" )
564+ entryPrice , _ := strconv .ParseFloat (entryPriceStr , 64 )
565+
566+ markPriceStr , _ := SafeString (pos , "markPrice" )
567+ markPrice , _ := strconv .ParseFloat (markPriceStr , 64 )
568+
569+ unRealizedProfitStr , _ := SafeString (pos , "unRealizedProfit" )
570+ unRealizedProfit , _ := strconv .ParseFloat (unRealizedProfitStr , 64 )
571+
572+ leverageStr , _ := SafeString (pos , "leverage" )
573+ leverageVal , _ := strconv .ParseFloat (leverageStr , 64 )
574+
575+ liquidationPriceStr , _ := SafeString (pos , "liquidationPrice" )
576+ liquidationPrice , _ := strconv .ParseFloat (liquidationPriceStr , 64 )
552577
553578 // 判断方向(与Binance一致)
554579 side := "long"
@@ -718,7 +743,12 @@ func (t *AsterTrader) CloseLong(symbol string, quantity float64) (map[string]int
718743
719744 for _ , pos := range positions {
720745 if pos ["symbol" ] == symbol && pos ["side" ] == "long" {
721- quantity = pos ["positionAmt" ].(float64 )
746+ qty , err := SafeFloat64 (pos , "positionAmt" )
747+ if err != nil {
748+ log .Printf ("⚠️ 无法解析 positionAmt: %v" , err )
749+ continue
750+ }
751+ quantity = qty
722752 break
723753 }
724754 }
@@ -801,7 +831,12 @@ func (t *AsterTrader) CloseShort(symbol string, quantity float64) (map[string]in
801831 for _ , pos := range positions {
802832 if pos ["symbol" ] == symbol && pos ["side" ] == "short" {
803833 // Aster的GetPositions已经将空仓数量转换为正数,直接使用
804- quantity = pos ["positionAmt" ].(float64 )
834+ qty , err := SafeFloat64 (pos , "positionAmt" )
835+ if err != nil {
836+ log .Printf ("⚠️ 无法解析 positionAmt: %v" , err )
837+ continue
838+ }
839+ quantity = qty
805840 break
806841 }
807842 }
0 commit comments