mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-15 00:36:56 +08:00
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:
@@ -18,6 +18,30 @@ func posString(pos map[string]interface{}, key string) string {
|
||||
return v
|
||||
}
|
||||
|
||||
// posInt64 extracts an int64 from a position map, returning 0 on failure.
|
||||
func posInt64(pos map[string]interface{}, key string) int64 {
|
||||
value, ok := pos[key]
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
switch v := value.(type) {
|
||||
case int64:
|
||||
return v
|
||||
case int:
|
||||
return int64(v)
|
||||
case float64:
|
||||
return int64(v)
|
||||
case string:
|
||||
parsed, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return parsed
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// SafeFloat64 Safely extract float64 value from map
|
||||
func SafeFloat64(data map[string]interface{}, key string) (float64, error) {
|
||||
value, ok := data[key]
|
||||
|
||||
Reference in New Issue
Block a user