Browse Source

Enhance position closed handling in PositionTracker with full symbol format

- Updated market data retrieval to use full symbol format (e.g., "BTC/USDC:USDC") for improved accuracy in PnL calculations.
- Adjusted logging to reflect the full symbol format when fetching market data.
- Ensured database saving of position stats utilizes the full symbol format for consistency.
Carles Sentis 1 ngày trước cách đây
mục cha
commit
d37be6ad27
2 tập tin đã thay đổi với 9 bổ sung6 xóa
  1. 8 5
      src/monitoring/position_tracker.py
  2. 1 1
      trading_bot.py

+ 8 - 5
src/monitoring/position_tracker.py

@@ -206,13 +206,16 @@ class PositionTracker:
     async def _handle_position_closed(self, symbol: str, position: Dict):
         """Handle position closed - save stats to database"""
         try:
+            # Construct full symbol format for market data (symbol here is just token name like "BTC")
+            full_symbol = f"{symbol}/USDC:USDC"
+            
             # Get current market price for PnL calculation
-            market_data = self.hl_client.get_market_data(symbol)
+            market_data = self.hl_client.get_market_data(full_symbol)
             if not market_data:
-                logger.error(f"Could not get market data for {symbol}")
+                logger.error(f"Could not get market data for {full_symbol}")
                 return
                 
-            current_price = float(market_data.get('markPx', 0))
+            current_price = float(market_data.get('ticker', {}).get('last', 0))
             entry_price = position['entry_px']
             size = abs(position['size'])
             side = "Long" if position['size'] > 0 else "Short"
@@ -223,8 +226,8 @@ class PositionTracker:
             else:
                 pnl = (entry_price - current_price) * size
                 
-            # Save to database
-            await self._save_position_stats(symbol, side, size, entry_price, current_price, pnl)
+            # Save to database with full symbol format
+            await self._save_position_stats(full_symbol, side, size, entry_price, current_price, pnl)
             
             # Send notification
             pnl_emoji = "🟢" if pnl >= 0 else "🔴"

+ 1 - 1
trading_bot.py

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