Browse Source

Refactor positions command to streamline unrealized PnL and margin calculations. Updated retrieval methods to provide default values, enhancing data consistency. Improved clarity in position display by explicitly defining position direction and emoji representation. Additionally, modified TradeLifecycleManager to use a private method for executing database queries, ensuring better encapsulation of database interactions.

Carles Sentis 1 week ago
parent
commit
1e459c8602
3 changed files with 18 additions and 18 deletions
  1. 16 16
      src/commands/info/positions.py
  2. 1 1
      src/stats/trade_lifecycle_manager.py
  3. 1 1
      trading_bot.py

+ 16 - 16
src/commands/info/positions.py

@@ -73,27 +73,22 @@ class PositionsCommands(InfoCommandsBase):
                             duration_str = "Error"
                             duration_str = "Error"
 
 
                     # Calculate unrealized PnL
                     # Calculate unrealized PnL
-                    unrealized_pnl = position_trade.get('unrealized_pnl')
-                    if unrealized_pnl is None:
-                        if position_side == 'long':
-                            unrealized_pnl = (mark_price - entry_price) * abs_current_amount
-                        else:  # Short position
-                            unrealized_pnl = (entry_price - mark_price) * abs_current_amount
-                    unrealized_pnl = unrealized_pnl or 0.0
-
-                    # ROE Percentage from database
+                    unrealized_pnl = position_trade.get('unrealized_pnl', 0.0)
+                    
+                    # Get ROE from database
                     roe_percentage = position_trade.get('roe_percentage', 0.0)
                     roe_percentage = position_trade.get('roe_percentage', 0.0)
 
 
                     # Add to totals
                     # Add to totals
-                    individual_position_value = position_trade.get('position_value')
-                    if individual_position_value is None:
+                    individual_position_value = position_trade.get('position_value', 0.0)
+                    if individual_position_value is None or individual_position_value == 0:
                         individual_position_value = abs_current_amount * mark_price
                         individual_position_value = abs_current_amount * mark_price
                     
                     
                     total_position_value += individual_position_value
                     total_position_value += individual_position_value
                     total_unrealized += unrealized_pnl
                     total_unrealized += unrealized_pnl
                     
                     
-                    margin_used = position_trade.get('margin_used')
-                    if margin_used is not None:
+                    # Add margin to total
+                    margin_used = position_trade.get('margin_used', 0.0)
+                    if margin_used > 0:
                         total_margin_used += margin_used
                         total_margin_used += margin_used
 
 
                     # Format position details
                     # Format position details
@@ -103,10 +98,15 @@ class PositionsCommands(InfoCommandsBase):
                     size_str = formatter.format_amount(abs_current_amount, base_asset)
                     size_str = formatter.format_amount(abs_current_amount, base_asset)
 
 
                     # Position header
                     # Position header
-                    pos_emoji = "🟢" if position_side == 'long' else "🔴"
-                    direction_text = position_side.upper()
+                    pos_emoji = ""
+                    direction_text = ""
+                    if position_side == 'long':
+                        pos_emoji = "🟢"
+                        direction_text = "LONG"
+                    else:  # Short position
+                        pos_emoji = "🔴"
+                        direction_text = "SHORT"
                     
                     
-                    # Add leverage if available
                     leverage = position_trade.get('leverage')
                     leverage = position_trade.get('leverage')
                     if leverage is not None:
                     if leverage is not None:
                         try:
                         try:

+ 1 - 1
src/stats/trade_lifecycle_manager.py

@@ -377,7 +377,7 @@ class TradeLifecycleManager:
                 WHERE trade_lifecycle_id = ?
                 WHERE trade_lifecycle_id = ?
             """
             """
             
             
-            self.db.execute_query(query, params)
+            self.db._execute_query(query, params)
             return True
             return True
         except Exception as e:
         except Exception as e:
             logger.error(f"Error updating trade market data: {e}")
             logger.error(f"Error updating trade market data: {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.4.192"
+BOT_VERSION = "2.4.193"
 
 
 # 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"))