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

@@ -45,7 +45,7 @@ func (at *AutoTrader) executeOpenLongWithRecord(decision *kernel.Decision, actio
// Check if there's already a position in the same symbol and direction
for _, pos := range positions {
if pos["symbol"] == decision.Symbol && pos["side"] == "long" {
if posString(pos, "symbol") == decision.Symbol && posString(pos, "side") == "long" {
return fmt.Errorf("❌ %s already has long position, close it first", decision.Symbol)
}
}
@@ -162,7 +162,7 @@ func (at *AutoTrader) executeOpenShortWithRecord(decision *kernel.Decision, acti
// Check if there's already a position in the same symbol and direction
for _, pos := range positions {
if pos["symbol"] == decision.Symbol && pos["side"] == "short" {
if posString(pos, "symbol") == decision.Symbol && posString(pos, "side") == "short" {
return fmt.Errorf("❌ %s already has short position, close it first", decision.Symbol)
}
}
@@ -294,11 +294,9 @@ func (at *AutoTrader) executeCloseLongWithRecord(decision *kernel.Decision, acti
positions, err := at.trader.GetPositions()
if err == nil {
for _, pos := range positions {
if pos["symbol"] == decision.Symbol && pos["side"] == "long" {
if ep, ok := pos["entryPrice"].(float64); ok {
entryPrice = ep
}
if amt, ok := pos["positionAmt"].(float64); ok && amt > 0 {
if posString(pos, "symbol") == decision.Symbol && posString(pos, "side") == "long" {
entryPrice = posFloat64(pos, "entryPrice")
if amt := posFloat64(pos, "positionAmt"); amt > 0 {
quantity = amt
}
break
@@ -358,11 +356,9 @@ func (at *AutoTrader) executeCloseShortWithRecord(decision *kernel.Decision, act
positions, err := at.trader.GetPositions()
if err == nil {
for _, pos := range positions {
if pos["symbol"] == decision.Symbol && pos["side"] == "short" {
if ep, ok := pos["entryPrice"].(float64); ok {
entryPrice = ep
}
if amt, ok := pos["positionAmt"].(float64); ok {
if posString(pos, "symbol") == decision.Symbol && posString(pos, "side") == "short" {
entryPrice = posFloat64(pos, "entryPrice")
if amt := posFloat64(pos, "positionAmt"); amt != 0 {
quantity = -amt // positionAmt is negative for short
}
break