fix sim and wip fix live
This commit is contained in:
@ -205,6 +205,9 @@ class CleanTradingDashboard:
|
||||
# Start signal generation loop to ensure continuous trading signals
|
||||
self._start_signal_generation_loop()
|
||||
|
||||
# Start live balance sync for trading
|
||||
self._start_live_balance_sync()
|
||||
|
||||
# Start training sessions if models are showing FRESH status
|
||||
threading.Thread(target=self._delayed_training_check, daemon=True).start()
|
||||
|
||||
@ -329,14 +332,14 @@ class CleanTradingDashboard:
|
||||
hasattr(self.trading_executor, 'simulation_mode') and
|
||||
not self.trading_executor.simulation_mode)
|
||||
|
||||
if is_live and hasattr(self.trading_executor, 'exchange_interface'):
|
||||
if is_live and hasattr(self.trading_executor, 'exchange'):
|
||||
# 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
|
||||
# Cache balance for 5 seconds for more frequent updates in live trading
|
||||
if not hasattr(self, '_last_balance_check') or current_time - self._last_balance_check > 5:
|
||||
exchange = self.trading_executor.exchange
|
||||
if hasattr(exchange, 'get_balance'):
|
||||
live_balance = exchange.get_balance('USDC')
|
||||
if live_balance is not None and live_balance > 0:
|
||||
@ -354,13 +357,20 @@ class CleanTradingDashboard:
|
||||
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")
|
||||
logger.warning("LIVE BALANCE: Exchange 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
|
||||
elif hasattr(self.trading_executor, 'simulation_mode') and self.trading_executor.simulation_mode:
|
||||
# In simulation mode, show dynamic balance based on P&L
|
||||
initial_balance = self._get_initial_balance()
|
||||
realized_pnl = sum(trade.get('pnl', 0) for trade in self.closed_trades)
|
||||
simulation_balance = initial_balance + realized_pnl
|
||||
logger.debug(f"SIMULATION BALANCE: ${simulation_balance:.2f} (Initial: ${initial_balance:.2f} + P&L: ${realized_pnl:.2f})")
|
||||
return simulation_balance
|
||||
else:
|
||||
logger.debug("LIVE BALANCE: Not in live trading mode, using simulation balance")
|
||||
logger.debug("LIVE BALANCE: Not in live trading mode, using initial balance")
|
||||
|
||||
# Fallback to initial balance for simulation mode
|
||||
return self._get_initial_balance()
|
||||
@ -484,10 +494,13 @@ class CleanTradingDashboard:
|
||||
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:
|
||||
if hasattr(self.trading_executor, 'simulation_mode') and self.trading_executor.simulation_mode:
|
||||
# Show simulation mode status with simulated balance
|
||||
mexc_status = f"SIM - ${current_balance:.2f}"
|
||||
elif hasattr(self.trading_executor, 'simulation_mode') and not self.trading_executor.simulation_mode:
|
||||
# Show live balance in MEXC status - detect currency
|
||||
try:
|
||||
exchange = self.trading_executor.exchange_interface
|
||||
exchange = self.trading_executor.exchange
|
||||
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
|
||||
|
||||
@ -2957,6 +2970,39 @@ class CleanTradingDashboard:
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error starting signal generation loop: {e}")
|
||||
|
||||
def _start_live_balance_sync(self):
|
||||
"""Start continuous live balance synchronization for trading"""
|
||||
def balance_sync_worker():
|
||||
while True:
|
||||
try:
|
||||
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 and hasattr(self.trading_executor, 'exchange'):
|
||||
# Force balance refresh every 15 seconds in live mode
|
||||
if hasattr(self, '_last_balance_check'):
|
||||
del self._last_balance_check # Force refresh
|
||||
|
||||
balance = self._get_live_balance()
|
||||
if balance > 0:
|
||||
logger.debug(f"BALANCE SYNC: Live balance: ${balance:.2f}")
|
||||
else:
|
||||
logger.warning("BALANCE SYNC: Could not retrieve live balance")
|
||||
|
||||
# Sync balance every 15 seconds for live trading
|
||||
time.sleep(15)
|
||||
except Exception as e:
|
||||
logger.debug(f"Error in balance sync loop: {e}")
|
||||
time.sleep(30) # Wait longer on error
|
||||
|
||||
# Start balance sync thread only if we have trading enabled
|
||||
if self.trading_executor:
|
||||
threading.Thread(target=balance_sync_worker, daemon=True).start()
|
||||
logger.info("BALANCE SYNC: Background balance synchronization started")
|
||||
|
||||
def _generate_dqn_signal(self, symbol: str, current_price: float) -> Optional[Dict]:
|
||||
"""Generate trading signal using DQN agent - NOT AVAILABLE IN BASIC ORCHESTRATOR"""
|
||||
@ -4600,28 +4646,35 @@ class CleanTradingDashboard:
|
||||
imbalance = cob_snapshot['stats']['imbalance']
|
||||
abs_imbalance = abs(imbalance)
|
||||
|
||||
# Dynamic threshold based on imbalance strength
|
||||
# Dynamic threshold based on imbalance strength with realistic confidence
|
||||
if abs_imbalance > 0.8: # Very strong imbalance (>80%)
|
||||
threshold = 0.05 # 5% threshold for very strong signals
|
||||
confidence_multiplier = 3.0
|
||||
base_confidence = 0.85 # High but not perfect confidence
|
||||
confidence_boost = (abs_imbalance - 0.8) * 0.75 # Scale remaining 15%
|
||||
elif abs_imbalance > 0.5: # Strong imbalance (>50%)
|
||||
threshold = 0.1 # 10% threshold for strong signals
|
||||
confidence_multiplier = 2.5
|
||||
base_confidence = 0.70 # Good confidence
|
||||
confidence_boost = (abs_imbalance - 0.5) * 0.50 # Scale up to 85%
|
||||
elif abs_imbalance > 0.3: # Moderate imbalance (>30%)
|
||||
threshold = 0.15 # 15% threshold for moderate signals
|
||||
confidence_multiplier = 2.0
|
||||
base_confidence = 0.55 # Moderate confidence
|
||||
confidence_boost = (abs_imbalance - 0.3) * 0.75 # Scale up to 70%
|
||||
else: # Weak imbalance
|
||||
threshold = 0.2 # 20% threshold for weak signals
|
||||
confidence_multiplier = 1.5
|
||||
base_confidence = 0.35 # Low confidence
|
||||
confidence_boost = abs_imbalance * 0.67 # Scale up to 55%
|
||||
|
||||
# Generate signal if imbalance exceeds threshold
|
||||
if abs_imbalance > threshold:
|
||||
# Calculate more realistic confidence (never exactly 1.0)
|
||||
final_confidence = min(0.95, base_confidence + confidence_boost)
|
||||
|
||||
signal = {
|
||||
'timestamp': datetime.now(),
|
||||
'type': 'cob_liquidity_imbalance',
|
||||
'action': 'BUY' if imbalance > 0 else 'SELL',
|
||||
'symbol': symbol,
|
||||
'confidence': min(1.0, abs_imbalance * confidence_multiplier),
|
||||
'confidence': final_confidence,
|
||||
'strength': abs_imbalance,
|
||||
'threshold_used': threshold,
|
||||
'signal_strength': 'very_strong' if abs_imbalance > 0.8 else 'strong' if abs_imbalance > 0.5 else 'moderate' if abs_imbalance > 0.3 else 'weak',
|
||||
|
Reference in New Issue
Block a user