fix: consistent safe type helpers in auto_trader, log emergency exit errors

- Replace all raw pos["key"].(type) assertions with posFloat64/posString/posInt64 helpers
  across auto_trader_decision, auto_trader_grid, auto_trader_grid_orders,
  auto_trader_grid_regime, auto_trader_loop, auto_trader_orders, auto_trader_risk
- Add posInt64 helper for int64 extraction (createdTime etc)
- Fix emergencyExit: log CloseLong/CloseShort errors instead of silently dropping
- Fix emergencyExit: log GetPositions error on failure
- Upgrade closeAllPositions log level from Infof to Warnf for close failures
- Zero raw type assertions remaining in auto_trader_* files
This commit is contained in:
shinchan-zhai
2026-03-23 12:29:21 +08:00
parent ed9230d38b
commit 3c39d1efbe
8 changed files with 78 additions and 51 deletions

View File

@@ -26,13 +26,12 @@ func (at *AutoTrader) checkTotalPositionLimit(symbol string, additionalValue flo
positions, err := at.trader.GetPositions()
if err == nil {
for _, pos := range positions {
if sym, ok := pos["symbol"].(string); ok && sym == symbol {
if size, ok := pos["positionAmt"].(float64); ok {
if price, ok := pos["markPrice"].(float64); ok {
currentPositionValue = math.Abs(size) * price
} else if entryPrice, ok := pos["entryPrice"].(float64); ok {
currentPositionValue = math.Abs(size) * entryPrice
}
if posString(pos, "symbol") == symbol {
size := posFloat64(pos, "positionAmt")
if price := posFloat64(pos, "markPrice"); price > 0 {
currentPositionValue = math.Abs(size) * price
} else if entryPrice := posFloat64(pos, "entryPrice"); entryPrice > 0 {
currentPositionValue = math.Abs(size) * entryPrice
}
}
}
@@ -267,10 +266,9 @@ func (at *AutoTrader) syncGridState() {
logger.Warnf("[Grid] Failed to get positions for state sync: %v", err)
} else {
for _, pos := range positions {
if sym, ok := pos["symbol"].(string); ok && sym == gridConfig.Symbol {
if size, ok := pos["positionAmt"].(float64); ok {
currentPositionSize = size
}
if posString(pos, "symbol") == gridConfig.Symbol {
currentPositionSize = posFloat64(pos, "positionAmt")
break
}
}
}
@@ -333,12 +331,12 @@ func (at *AutoTrader) closeAllPositions() error {
}
for _, pos := range positions {
symbol, _ := pos["symbol"].(string)
symbol := posString(pos, "symbol")
if symbol != gridConfig.Symbol {
continue
}
size, _ := pos["positionAmt"].(float64)
size := posFloat64(pos, "positionAmt")
if size == 0 {
continue
}
@@ -349,7 +347,7 @@ func (at *AutoTrader) closeAllPositions() error {
_, err = at.trader.CloseShort(symbol, -size)
}
if err != nil {
logger.Infof("Failed to close position: %v", err)
logger.Warnf("[Grid] Failed to close position (size=%.6f): %v", size, err)
}
}