Quellcode durchsuchen

Refactor positions command to improve mark price validation and enhance trading statistics retrieval. Updated the logic to ensure mark price is greater than zero before displaying it, and modified the last activity timestamp retrieval to include both opened and closed trades, ensuring accurate and timezone-aware data handling.

Carles Sentis vor 1 Woche
Ursprung
Commit
0d1c0aac12
3 geänderte Dateien mit 35 neuen und 7 gelöschten Zeilen
  1. 1 1
      src/commands/info/positions.py
  2. 33 5
      src/stats/trading_stats.py
  3. 1 1
      trading_bot.py

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

@@ -129,7 +129,7 @@ class PositionsCommands(InfoCommandsBase):
                     positions_text += f"   💰 Entry: {entry_price_str}\n"
                     positions_text += f"   ⏳ Duration: {duration_str}\n"
                     positions_text += f"   🏦 Value: ${individual_position_value:,.2f}\n"
-                    if mark_price != 0 and abs(mark_price - entry_price) > 1e-9:
+                    if mark_price > 0 and abs(mark_price - entry_price) > 1e-9:
                         positions_text += f"   📈 Mark: {mark_price_str}\n"
                     pnl_emoji = "🟢" if unrealized_pnl >= 0 else "🔴"
                     positions_text += f"   {pnl_emoji} P&L: ${unrealized_pnl:,.2f}\n"

+ 33 - 5
src/stats/trading_stats.py

@@ -409,12 +409,40 @@ class TradingStats:
         start_date_obj = datetime.fromisoformat(start_date_iso) if start_date_iso else datetime.now(timezone.utc)
         days_active = (datetime.now(timezone.utc) - start_date_obj).days + 1
         
-        # 'last_trade' timestamp could be the last update to token_stats or an open trade
-        last_activity_ts = token_stats_summary['overall_last_cycle_closed'] if token_stats_summary else None
-        last_open_trade_ts_row = self.db_manager._fetchone_query("SELECT MAX(updated_at) as last_update FROM trades WHERE status = 'position_opened'")
+        # Get last activity timestamp
+        last_activity_ts = None
+        last_activity_query = """
+            SELECT MAX(updated_at) as last_update
+            FROM trades
+            WHERE status IN ('position_opened', 'position_closed')
+        """
+        last_activity_row = self.db_manager._fetchone_query(last_activity_query)
+        if last_activity_row and last_activity_row['last_update']:
+            last_activity_ts = last_activity_row['last_update']
+            # Ensure timezone-aware
+            if isinstance(last_activity_ts, str):
+                last_activity_ts = datetime.fromisoformat(last_activity_ts)
+            if last_activity_ts.tzinfo is None:
+                last_activity_ts = last_activity_ts.replace(tzinfo=timezone.utc)
+
+        # Get last open trade timestamp
+        last_open_trade_query = """
+            SELECT MAX(updated_at) as last_update
+            FROM trades
+            WHERE status = 'position_opened'
+        """
+        last_open_trade_ts_row = self.db_manager._fetchone_query(last_open_trade_query)
         if last_open_trade_ts_row and last_open_trade_ts_row['last_update']:
-            if not last_activity_ts or datetime.fromisoformat(last_open_trade_ts_row['last_update']) > datetime.fromisoformat(last_activity_ts):
-                last_activity_ts = last_open_trade_ts_row['last_update']
+            last_open_trade_ts = last_open_trade_ts_row['last_update']
+            # Ensure timezone-aware
+            if isinstance(last_open_trade_ts, str):
+                last_open_trade_ts = datetime.fromisoformat(last_open_trade_ts)
+            if last_open_trade_ts.tzinfo is None:
+                last_open_trade_ts = last_open_trade_ts.replace(tzinfo=timezone.utc)
+
+            # Now both datetimes are timezone-aware, we can compare them
+            if not last_activity_ts or last_open_trade_ts > last_activity_ts:
+                last_activity_ts = last_open_trade_ts
 
         return {
             'total_trades': total_trades_redefined,

+ 1 - 1
trading_bot.py

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