Browse Source

Update bot version to 2.6.287 and enhance error handling in position calculations

- Incremented bot version to 2.6.287.
- Improved handling of None values for markPrice in position calculations to prevent errors.
- Added error handling for portfolio total calculations to ensure robustness against ValueError and TypeError exceptions.
- Updated balance snapshot recording to include unrealized PnL for better tracking.
Carles Sentis 18 hours ago
parent
commit
0b251de1a1
3 changed files with 20 additions and 12 deletions
  1. 15 9
      src/commands/info/positions.py
  2. 4 2
      src/commands/management_commands.py
  3. 1 1
      trading_bot.py

+ 15 - 9
src/commands/info/positions.py

@@ -215,15 +215,21 @@ class PositionsCommands(InfoCommandsBase):
             total_unrealized_pnl = 0.0
             total_unrealized_pnl = 0.0
             total_roe = 0.0
             total_roe = 0.0
             for pos in open_positions:
             for pos in open_positions:
-                size = float(pos.get('size', 0))
-                entry_price = float(pos.get('entryPrice', 0))
-                mark_price = float(pos.get('markPrice', 0))
-                roe = float(pos.get('roe_percentage', 0))
-                if size != 0 and entry_price != 0:
-                    position_value = abs(size * entry_price)
-                    total_unrealized_pnl += size * (mark_price - entry_price)
-                    total_roe += roe * position_value
-                    total_position_value += position_value
+                try:
+                    size = float(pos.get('size', 0)) if pos.get('size') is not None else 0.0
+                    entry_price = float(pos.get('entryPrice', 0)) if pos.get('entryPrice') is not None else 0.0
+                    # Handle None markPrice safely
+                    mark_price_raw = pos.get('markPrice')
+                    mark_price = float(mark_price_raw) if mark_price_raw is not None else entry_price
+                    roe = float(pos.get('roe_percentage', 0)) if pos.get('roe_percentage') is not None else 0.0
+                    if size != 0 and entry_price != 0:
+                        position_value = abs(size * entry_price)
+                        total_unrealized_pnl += size * (mark_price - entry_price)
+                        total_roe += roe * position_value
+                        total_position_value += position_value
+                except (ValueError, TypeError) as e:
+                    logger.warning(f"Error calculating portfolio totals for position: {e}")
+                    continue
             # Weighted average ROE
             # Weighted average ROE
             avg_roe = (total_roe / total_position_value) if total_position_value > 0 else 0.0
             avg_roe = (total_roe / total_position_value) if total_position_value > 0 else 0.0
             roe_emoji = "🟢" if avg_roe >= 0 else "🔴"
             roe_emoji = "🟢" if avg_roe >= 0 else "🔴"

+ 4 - 2
src/commands/management_commands.py

@@ -675,7 +675,9 @@ Will trigger when {token} price moves {alarm['direction']} {target_price_str}
                                         
                                         
                                         # Update with current market data
                                         # Update with current market data
                                         unrealized_pnl = float(position.get('unrealizedPnl', 0))
                                         unrealized_pnl = float(position.get('unrealizedPnl', 0))
-                                        mark_price = float(position.get('markPrice', entry_price))
+                                        # Handle None markPrice safely
+                                        mark_price_raw = position.get('markPrice')
+                                        mark_price = float(mark_price_raw) if mark_price_raw is not None else entry_price
                                         
                                         
                                         stats.update_trade_market_data(
                                         stats.update_trade_market_data(
                                             trade_lifecycle_id=lifecycle_id,
                                             trade_lifecycle_id=lifecycle_id,
@@ -738,7 +740,7 @@ Will trigger when {token} price moves {alarm['direction']} {target_price_str}
                 if balance_data and balance_data.get('total'):
                 if balance_data and balance_data.get('total'):
                     current_balance = float(balance_data['total'].get('USDC', 0))
                     current_balance = float(balance_data['total'].get('USDC', 0))
                     if stats:
                     if stats:
-                        await stats.record_balance_snapshot(current_balance, notes="Post-sync balance")
+                        await stats.record_balance_snapshot(current_balance, unrealized_pnl=0.0, notes="Post-sync balance")
             except Exception as e:
             except Exception as e:
                 logger.warning(f"Could not update balance after sync: {e}")
                 logger.warning(f"Could not update balance after sync: {e}")
 
 

+ 1 - 1
trading_bot.py

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