Ver Fonte

Enhance error handling in positions command for numeric value conversions. Updated logic to safely convert entry price, current amount, mark price, unrealized P&L, ROE percentage, individual position value, and margin used, ensuring robust data retrieval and logging warnings for conversion failures. This improves data consistency and reliability in financial calculations.

Carles Sentis há 1 semana atrás
pai
commit
9c134af542
2 ficheiros alterados com 49 adições e 8 exclusões
  1. 48 7
      src/commands/info/positions.py
  2. 1 1
      trading_bot.py

+ 48 - 7
src/commands/info/positions.py

@@ -45,8 +45,22 @@ class PositionsCommands(InfoCommandsBase):
                     symbol = position_trade['symbol']
                     base_asset = symbol.split('/')[0] if '/' in symbol else symbol
                     position_side = position_trade.get('position_side', 'unknown')
-                    entry_price = float(position_trade.get('entry_price', 0.0))
-                    current_amount = float(position_trade.get('current_position_size', 0.0))
+                    
+                    # Safely convert numeric values with proper null checks
+                    entry_price = 0.0
+                    if position_trade.get('entry_price') is not None:
+                        try:
+                            entry_price = float(position_trade['entry_price'])
+                        except (ValueError, TypeError):
+                            logger.warning(f"Could not convert entry_price for {symbol}")
+                    
+                    current_amount = 0.0
+                    if position_trade.get('current_position_size') is not None:
+                        try:
+                            current_amount = float(position_trade['current_position_size'])
+                        except (ValueError, TypeError):
+                            logger.warning(f"Could not convert current_position_size for {symbol}")
+                    
                     abs_current_amount = abs(current_amount)
                     trade_type = position_trade.get('trade_type', 'manual')
                     
@@ -78,16 +92,37 @@ class PositionsCommands(InfoCommandsBase):
                             duration_str = "Error"
                     
                     # Get price data with defaults
-                    mark_price = float(position_trade.get('mark_price', entry_price))
+                    mark_price = entry_price  # Default to entry price
+                    if position_trade.get('mark_price') is not None:
+                        try:
+                            mark_price = float(position_trade['mark_price'])
+                        except (ValueError, TypeError):
+                            logger.warning(f"Could not convert mark_price for {symbol}")
                     
                     # Calculate unrealized PnL
-                    unrealized_pnl = float(position_trade.get('unrealized_pnl', 0.0))
+                    unrealized_pnl = 0.0
+                    if position_trade.get('unrealized_pnl') is not None:
+                        try:
+                            unrealized_pnl = float(position_trade['unrealized_pnl'])
+                        except (ValueError, TypeError):
+                            logger.warning(f"Could not convert unrealized_pnl for {symbol}")
                     
                     # Get ROE from database
-                    roe_percentage = float(position_trade.get('roe_percentage', 0.0))
+                    roe_percentage = 0.0
+                    if position_trade.get('roe_percentage') is not None:
+                        try:
+                            roe_percentage = float(position_trade['roe_percentage'])
+                        except (ValueError, TypeError):
+                            logger.warning(f"Could not convert roe_percentage for {symbol}")
 
                     # Add to totals
-                    individual_position_value = float(position_trade.get('position_value', 0.0))
+                    individual_position_value = 0.0
+                    if position_trade.get('position_value') is not None:
+                        try:
+                            individual_position_value = float(position_trade['position_value'])
+                        except (ValueError, TypeError):
+                            logger.warning(f"Could not convert position_value for {symbol}")
+                    
                     if individual_position_value <= 0:
                         individual_position_value = abs_current_amount * mark_price
                     
@@ -95,7 +130,13 @@ class PositionsCommands(InfoCommandsBase):
                     total_unrealized += unrealized_pnl
                     
                     # Add margin to total
-                    margin_used = float(position_trade.get('margin_used', 0.0))
+                    margin_used = 0.0
+                    if position_trade.get('margin_used') is not None:
+                        try:
+                            margin_used = float(position_trade['margin_used'])
+                        except (ValueError, TypeError):
+                            logger.warning(f"Could not convert margin_used for {symbol}")
+                    
                     if margin_used > 0:
                         total_margin_used += margin_used
                     

+ 1 - 1
trading_bot.py

@@ -14,7 +14,7 @@ from datetime import datetime
 from pathlib import Path
 
 # Bot version
-BOT_VERSION = "2.4.195"
+BOT_VERSION = "2.4.196"
 
 # Add src directory to Python path
 sys.path.insert(0, str(Path(__file__).parent / "src"))