more live trades fix
This commit is contained in:
@ -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
|
||||
|
||||
|
Reference in New Issue
Block a user