Răsfoiți Sursa

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 săptămână în urmă
părinte
comite
1e459c8602
3 a modificat fișierele cu 18 adăugiri și 18 ștergeri
  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"
 
                     # 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)
 
                     # 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
                     
                     total_position_value += individual_position_value
                     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
 
                     # Format position details
@@ -103,10 +98,15 @@ class PositionsCommands(InfoCommandsBase):
                     size_str = formatter.format_amount(abs_current_amount, base_asset)
 
                     # 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')
                     if leverage is not None:
                         try:

+ 1 - 1
src/stats/trade_lifecycle_manager.py

@@ -377,7 +377,7 @@ class TradeLifecycleManager:
                 WHERE trade_lifecycle_id = ?
             """
             
-            self.db.execute_query(query, params)
+            self.db._execute_query(query, params)
             return True
         except Exception as 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
 
 # Bot version
-BOT_VERSION = "2.4.192"
+BOT_VERSION = "2.4.193"
 
 # Add src directory to Python path
 sys.path.insert(0, str(Path(__file__).parent / "src"))