From 64678bd8d3b35472084982a32b4d0830610cebe5 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Tue, 8 Jul 2025 02:03:32 +0300 Subject: [PATCH] more live trades fix --- core/orchestrator.py | 2 +- core/trading_executor.py | 22 +++++++--- web/clean_dashboard.py | 93 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 104 insertions(+), 13 deletions(-) diff --git a/core/orchestrator.py b/core/orchestrator.py index bbdef3a..fd3c0dc 100644 --- a/core/orchestrator.py +++ b/core/orchestrator.py @@ -1031,7 +1031,7 @@ class TradingOrchestrator: action_probs = [0.1, 0.1, 0.8] # Default distribution action_probs[action_idx] = confidence else: - # Fallback to generic predict method + # Fallback to generic predict method action_probs, confidence = model.predict(enhanced_features) except Exception as e: logger.warning(f"CNN prediction failed: {e}") diff --git a/core/trading_executor.py b/core/trading_executor.py index 32f0fd2..13c55c3 100644 --- a/core/trading_executor.py +++ b/core/trading_executor.py @@ -230,15 +230,25 @@ class TradingExecutor: required_capital = self._calculate_position_size(confidence, current_price) # Get available balance for the quote asset - available_balance = self.exchange.get_balance(quote_asset) - - # If USDC balance is insufficient, check USDT as fallback (for MEXC compatibility) - if available_balance < required_capital and quote_asset == 'USDC': + # For MEXC, prioritize USDT over USDC since most accounts have USDT + if quote_asset == 'USDC': + # Check USDT first (most common balance) usdt_balance = self.exchange.get_balance('USDT') + usdc_balance = self.exchange.get_balance('USDC') + if usdt_balance >= required_capital: available_balance = usdt_balance - quote_asset = 'USDT' # Use USDT instead - logger.info(f"BALANCE CHECK: Using USDT fallback balance for {symbol}") + quote_asset = 'USDT' # Use USDT for trading + logger.info(f"BALANCE CHECK: Using USDT balance for {symbol} (preferred)") + elif usdc_balance >= required_capital: + available_balance = usdc_balance + logger.info(f"BALANCE CHECK: Using USDC balance for {symbol}") + else: + # Use the larger balance for reporting + available_balance = max(usdt_balance, usdc_balance) + quote_asset = 'USDT' if usdt_balance > usdc_balance else 'USDC' + else: + available_balance = self.exchange.get_balance(quote_asset) logger.info(f"BALANCE CHECK: Symbol: {symbol}, Action: {action}, Required: ${required_capital:.2f} {quote_asset}, Available: ${available_balance:.2f} {quote_asset}") diff --git a/web/clean_dashboard.py b/web/clean_dashboard.py index 669df70..f5a042b 100644 --- a/web/clean_dashboard.py +++ b/web/clean_dashboard.py @@ -318,6 +318,59 @@ class CleanTradingDashboard: except Exception as e: logger.warning(f"Error getting balance: {e}") return 100.0 # Default balance + + def _get_live_balance(self) -> float: + """Get real-time balance from exchange when in live trading mode""" + try: + if self.trading_executor: + # Check if we're in live trading mode + is_live = (hasattr(self.trading_executor, 'trading_enabled') and + self.trading_executor.trading_enabled and + hasattr(self.trading_executor, 'simulation_mode') and + not self.trading_executor.simulation_mode) + + if is_live and hasattr(self.trading_executor, 'exchange_interface'): + # Get real balance from exchange (throttled to avoid API spam) + import time + current_time = time.time() + + # Cache balance for 10 seconds to avoid excessive API calls + if not hasattr(self, '_last_balance_check') or current_time - self._last_balance_check > 10: + exchange = self.trading_executor.exchange_interface + if hasattr(exchange, 'get_balance'): + live_balance = exchange.get_balance('USDC') + if live_balance is not None and live_balance > 0: + self._cached_live_balance = live_balance + self._last_balance_check = current_time + logger.info(f"LIVE BALANCE: Retrieved ${live_balance:.2f} USDC from MEXC") + return live_balance + else: + logger.warning(f"LIVE BALANCE: Retrieved ${live_balance:.2f} USDC - checking USDT as fallback") + # Also try USDT as fallback since user might have USDT + usdt_balance = exchange.get_balance('USDT') + if usdt_balance is not None and usdt_balance > 0: + self._cached_live_balance = usdt_balance + self._last_balance_check = current_time + logger.info(f"LIVE BALANCE: Using USDT balance ${usdt_balance:.2f}") + return usdt_balance + else: + logger.warning("LIVE BALANCE: Exchange interface does not have get_balance method") + else: + # Return cached balance if within 10 second window + if hasattr(self, '_cached_live_balance'): + return self._cached_live_balance + else: + logger.debug("LIVE BALANCE: Not in live trading mode, using simulation balance") + + # Fallback to initial balance for simulation mode + return self._get_initial_balance() + + except Exception as e: + logger.error(f"Error getting live balance: {e}") + # Return cached balance if available, otherwise fallback + if hasattr(self, '_cached_live_balance'): + return self._cached_live_balance + return self._get_initial_balance() def _setup_layout(self): """Setup the dashboard layout using layout manager""" @@ -411,17 +464,45 @@ class CleanTradingDashboard: trade_count = len(self.closed_trades) trade_str = f"{trade_count} Trades" - # Portfolio value - initial_balance = self._get_initial_balance() - portfolio_value = initial_balance + total_session_pnl # Use total P&L including unrealized - portfolio_str = f"${portfolio_value:.2f}" + # Portfolio value - use live balance for live trading + current_balance = self._get_live_balance() + portfolio_value = current_balance + total_session_pnl # Use total P&L including unrealized - # MEXC status + # Show live balance indicator for live trading + balance_indicator = "" + if self.trading_executor: + is_live = (hasattr(self.trading_executor, 'trading_enabled') and + self.trading_executor.trading_enabled and + hasattr(self.trading_executor, 'simulation_mode') and + not self.trading_executor.simulation_mode) + if is_live: + balance_indicator = " (LIVE)" + + portfolio_str = f"${portfolio_value:.2f}{balance_indicator}" + + # MEXC status with balance info mexc_status = "SIM" if self.trading_executor: if hasattr(self.trading_executor, 'trading_enabled') and self.trading_executor.trading_enabled: if hasattr(self.trading_executor, 'simulation_mode') and not self.trading_executor.simulation_mode: - mexc_status = "LIVE" + # Show live balance in MEXC status - detect currency + try: + exchange = self.trading_executor.exchange_interface + usdc_balance = exchange.get_balance('USDC') if hasattr(exchange, 'get_balance') else 0 + usdt_balance = exchange.get_balance('USDT') if hasattr(exchange, 'get_balance') else 0 + + if usdc_balance > 0: + mexc_status = f"LIVE - ${usdc_balance:.2f} USDC" + elif usdt_balance > 0: + mexc_status = f"LIVE - ${usdt_balance:.2f} USDT" + else: + mexc_status = f"LIVE - ${current_balance:.2f}" + except: + mexc_status = f"LIVE - ${current_balance:.2f}" + else: + mexc_status = "SIM" + else: + mexc_status = "DISABLED" return price_str, session_pnl_str, position_str, trade_str, portfolio_str, mexc_status