Kaynağa Gözat

Enhance Telegram bot balance and order handling - Added debug logging for raw balance and orders data to aid in troubleshooting. Updated balance calculations to ensure accurate P&L reporting in USDC and refined asset handling for non-USDC tokens. Improved performance summary display and adjusted command prompts for opening positions.

Carles Sentis 5 gün önce
ebeveyn
işleme
ec64d2f5c3
2 değiştirilmiş dosya ile 19 ekleme ve 27 silme
  1. 17 4
      src/telegram_bot.py
  2. 2 23
      src/trading_stats.py

+ 17 - 4
src/telegram_bot.py

@@ -533,6 +533,9 @@ Tap any button below for instant access to bot functions:
         if balance:
             balance_text = "💰 <b>Account Balance</b>\n\n"
             
+            # Debug: Show raw balance structure (can be removed after debugging)
+            logger.debug(f"Raw balance data: {balance}")
+            
             # CCXT balance structure includes 'free', 'used', and 'total'
             total_balance = balance.get('total', {})
             free_balance = balance.get('free', {})
@@ -557,10 +560,14 @@ Tap any button below for instant access to bot functions:
                         
                         balance_text += "\n"
                         
-                        # Calculate totals for USDC (main trading currency)
+                        # Calculate totals (convert all to USDC equivalent for summary)
                         if asset == 'USDC':
                             total_value += float(amount)
                             available_value += free_amount
+                        else:
+                            # For non-USDC assets, add to totals (assuming 1:1 for now, could be enhanced with price conversion)
+                            total_value += float(amount)
+                            available_value += free_amount
                 
                 # Summary section
                 balance_text += f"💼 <b>Portfolio Summary:</b>\n"
@@ -573,10 +580,12 @@ Tap any button below for instant access to bot functions:
                 # Add P&L summary
                 basic_stats = self.stats.get_basic_stats()
                 if basic_stats['initial_balance'] > 0:
-                    pnl = total_value - basic_stats['initial_balance']
+                    # Use USDC balance for P&L calculation
+                    usdc_total = float(total_balance.get('USDC', 0))
+                    pnl = usdc_total - basic_stats['initial_balance']
                     pnl_percent = (pnl / basic_stats['initial_balance']) * 100
                     
-                    balance_text += f"\n📊 <b>Performance:</b>\n"
+                    balance_text += f"\n📊 <b>Performance (USDC):</b>\n"
                     balance_text += f"   💵 P&L: ${pnl:,.2f} ({pnl_percent:+.2f}%)\n"
                     balance_text += f"   📈 Initial: ${basic_stats['initial_balance']:,.2f}"
             else:
@@ -639,7 +648,7 @@ Tap any button below for instant access to bot functions:
             else:
                 positions_text += "📭 <b>No open positions currently</b>\n\n"
                 positions_text += "🚀 Ready to start trading!\n"
-                positions_text += "Use /buy or /sell commands to open positions."
+                positions_text += "Use /long or /short commands to open positions."
         else:
             # Actual API error
             positions_text = "❌ <b>Could not fetch positions data</b>\n\n"
@@ -661,6 +670,10 @@ Tap any button below for instant access to bot functions:
         
         orders = self.client.get_open_orders()
         
+        # Debug: Log what we got from orders
+        logger.debug(f"Raw orders data: {orders}")
+        logger.debug(f"Orders type: {type(orders)}, Length: {len(orders) if orders else 'None'}")
+        
         if orders is not None:  # Successfully fetched (could be empty list)
             if token_filter:
                 orders_text = f"📋 <b>Open Orders - {token_filter}</b>\n\n"

+ 2 - 23
src/trading_stats.py

@@ -635,30 +635,9 @@ Please try again in a moment. If the issue persists, contact support.
         for token, trades in token_trades.items():
             completed_trades = [t for t in trades if t.get('pnl', 0) != 0]
             
+            # Only include tokens that have actual completed trades with P&L
             if not completed_trades:
-                # Only opening trades, no P&L yet
-                total_trades = len(trades)
-                buy_trades = len([t for t in trades if t['side'] == 'buy'])
-                sell_trades = len([t for t in trades if t['side'] == 'sell'])
-                
-                token_performance[token] = {
-                    'total_pnl': 0.0,
-                    'pnl_percentage': 0.0,
-                    'total_trades': total_trades,
-                    'completed_trades': 0,
-                    'buy_trades': buy_trades,
-                    'sell_trades': sell_trades,
-                    'win_rate': 0.0,
-                    'avg_win': 0.0,
-                    'avg_loss': 0.0,
-                    'largest_win': 0.0,
-                    'largest_loss': 0.0,
-                    'total_volume': sum(t['value'] for t in trades),
-                    'profit_factor': 0.0,
-                    'total_wins': 0,
-                    'total_losses': 0
-                }
-                continue
+                continue  # Skip tokens with no completed trades
             
             # Calculate metrics for completed trades
             total_pnl = sum(t['pnl'] for t in completed_trades)