|
@@ -206,13 +206,16 @@ class PositionTracker:
|
|
async def _handle_position_closed(self, symbol: str, position: Dict):
|
|
async def _handle_position_closed(self, symbol: str, position: Dict):
|
|
"""Handle position closed - save stats to database"""
|
|
"""Handle position closed - save stats to database"""
|
|
try:
|
|
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
|
|
# 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:
|
|
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
|
|
return
|
|
|
|
|
|
- current_price = float(market_data.get('markPx', 0))
|
|
|
|
|
|
+ current_price = float(market_data.get('ticker', {}).get('last', 0))
|
|
entry_price = position['entry_px']
|
|
entry_price = position['entry_px']
|
|
size = abs(position['size'])
|
|
size = abs(position['size'])
|
|
side = "Long" if position['size'] > 0 else "Short"
|
|
side = "Long" if position['size'] > 0 else "Short"
|
|
@@ -223,8 +226,8 @@ class PositionTracker:
|
|
else:
|
|
else:
|
|
pnl = (entry_price - current_price) * size
|
|
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
|
|
# Send notification
|
|
pnl_emoji = "🟢" if pnl >= 0 else "🔴"
|
|
pnl_emoji = "🟢" if pnl >= 0 else "🔴"
|