more live trades fix

This commit is contained in:
Dobromir Popov
2025-07-08 02:03:32 +03:00
parent 4ab7bc1846
commit 64678bd8d3
3 changed files with 104 additions and 13 deletions

View File

@ -1031,7 +1031,7 @@ class TradingOrchestrator:
action_probs = [0.1, 0.1, 0.8] # Default distribution action_probs = [0.1, 0.1, 0.8] # Default distribution
action_probs[action_idx] = confidence action_probs[action_idx] = confidence
else: else:
# Fallback to generic predict method # Fallback to generic predict method
action_probs, confidence = model.predict(enhanced_features) action_probs, confidence = model.predict(enhanced_features)
except Exception as e: except Exception as e:
logger.warning(f"CNN prediction failed: {e}") logger.warning(f"CNN prediction failed: {e}")

View File

@ -230,15 +230,25 @@ class TradingExecutor:
required_capital = self._calculate_position_size(confidence, current_price) required_capital = self._calculate_position_size(confidence, current_price)
# Get available balance for the quote asset # Get available balance for the quote asset
available_balance = self.exchange.get_balance(quote_asset) # For MEXC, prioritize USDT over USDC since most accounts have USDT
if quote_asset == 'USDC':
# If USDC balance is insufficient, check USDT as fallback (for MEXC compatibility) # Check USDT first (most common balance)
if available_balance < required_capital and quote_asset == 'USDC':
usdt_balance = self.exchange.get_balance('USDT') usdt_balance = self.exchange.get_balance('USDT')
usdc_balance = self.exchange.get_balance('USDC')
if usdt_balance >= required_capital: if usdt_balance >= required_capital:
available_balance = usdt_balance available_balance = usdt_balance
quote_asset = 'USDT' # Use USDT instead quote_asset = 'USDT' # Use USDT for trading
logger.info(f"BALANCE CHECK: Using USDT fallback balance for {symbol}") 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}") logger.info(f"BALANCE CHECK: Symbol: {symbol}, Action: {action}, Required: ${required_capital:.2f} {quote_asset}, Available: ${available_balance:.2f} {quote_asset}")

View File

@ -319,6 +319,59 @@ class CleanTradingDashboard:
logger.warning(f"Error getting balance: {e}") logger.warning(f"Error getting balance: {e}")
return 100.0 # Default balance 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): def _setup_layout(self):
"""Setup the dashboard layout using layout manager""" """Setup the dashboard layout using layout manager"""
self.app.layout = self.layout_manager.create_main_layout() self.app.layout = self.layout_manager.create_main_layout()
@ -411,17 +464,45 @@ class CleanTradingDashboard:
trade_count = len(self.closed_trades) trade_count = len(self.closed_trades)
trade_str = f"{trade_count} Trades" trade_str = f"{trade_count} Trades"
# Portfolio value # Portfolio value - use live balance for live trading
initial_balance = self._get_initial_balance() current_balance = self._get_live_balance()
portfolio_value = initial_balance + total_session_pnl # Use total P&L including unrealized portfolio_value = current_balance + total_session_pnl # Use total P&L including unrealized
portfolio_str = f"${portfolio_value:.2f}"
# 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" mexc_status = "SIM"
if self.trading_executor: if self.trading_executor:
if hasattr(self.trading_executor, 'trading_enabled') and self.trading_executor.trading_enabled: 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: 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 return price_str, session_pnl_str, position_str, trade_str, portfolio_str, mexc_status