From 153ebe6ec2c08b7eedcccf1c3edd3044648b3e03 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Tue, 22 Jul 2025 21:18:31 +0300 Subject: [PATCH] stability --- core/orchestrator.py | 11 +++++++---- enhanced_realtime_training.py | 30 +++++++++++++++++++++++++++++- web/clean_dashboard.py | 4 +--- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/core/orchestrator.py b/core/orchestrator.py index fd3c0dc..ecbec58 100644 --- a/core/orchestrator.py +++ b/core/orchestrator.py @@ -230,7 +230,8 @@ class TradingOrchestrator: self.model_states['dqn']['checkpoint_loaded'] = True self.model_states['dqn']['checkpoint_filename'] = metadata.checkpoint_id checkpoint_loaded = True - logger.info(f"DQN checkpoint loaded: {metadata.checkpoint_id} (loss={metadata.loss:.4f})") + loss_str = f"{metadata.loss:.4f}" if metadata.loss is not None else "N/A" + logger.info(f"DQN checkpoint loaded: {metadata.checkpoint_id} (loss={loss_str})") except Exception as e: logger.warning(f"Error loading DQN checkpoint: {e}") @@ -269,7 +270,8 @@ class TradingOrchestrator: self.model_states['cnn']['checkpoint_loaded'] = True self.model_states['cnn']['checkpoint_filename'] = metadata.checkpoint_id checkpoint_loaded = True - logger.info(f"CNN checkpoint loaded: {metadata.checkpoint_id} (loss={metadata.loss:.4f})") + loss_str = f"{metadata.loss:.4f}" if metadata.loss is not None else "N/A" + logger.info(f"CNN checkpoint loaded: {metadata.checkpoint_id} (loss={loss_str})") except Exception as e: logger.warning(f"Error loading CNN checkpoint: {e}") @@ -356,7 +358,8 @@ class TradingOrchestrator: self.model_states['cob_rl']['checkpoint_loaded'] = True self.model_states['cob_rl']['checkpoint_filename'] = metadata.checkpoint_id checkpoint_loaded = True - logger.info(f"COB RL checkpoint loaded: {metadata.checkpoint_id} (loss={metadata.loss:.4f})") + loss_str = f"{metadata.loss:.4f}" if metadata.loss is not None else "N/A" + logger.info(f"COB RL checkpoint loaded: {metadata.checkpoint_id} (loss={loss_str})") except Exception as e: logger.warning(f"Error loading COB RL checkpoint: {e}") @@ -547,7 +550,7 @@ class TradingOrchestrator: if self.cob_integration: try: logger.info("Attempting to start COB integration...") - await self.cob_integration.start_streaming() + await self.cob_integration.start() logger.info("COB Integration streaming started successfully.") except Exception as e: logger.error(f"Failed to start COB integration streaming: {e}") diff --git a/enhanced_realtime_training.py b/enhanced_realtime_training.py index 1fd383c..a5f38dc 100644 --- a/enhanced_realtime_training.py +++ b/enhanced_realtime_training.py @@ -114,6 +114,34 @@ class EnhancedRealtimeTrainingSystem: logger.info("Enhanced Real-time Training System initialized") + def _get_dqn_state_features(self, symbol: str) -> Optional[np.ndarray]: + """Get DQN state features from orchestrator""" + try: + if not self.orchestrator: + return None + + # Get DQN state from orchestrator if available + if hasattr(self.orchestrator, 'build_comprehensive_rl_state'): + rl_state = self.orchestrator.build_comprehensive_rl_state(symbol) + if rl_state and 'state_vector' in rl_state: + return np.array(rl_state['state_vector'], dtype=np.float32) + + # Fallback: create basic state from available data + if len(self.real_time_data['ohlcv_1m']) > 0: + latest_bar = self.real_time_data['ohlcv_1m'][-1] + basic_state = [ + latest_bar.get('close', 0) / 10000.0, # Normalized price + latest_bar.get('volume', 0) / 1000000.0, # Normalized volume + 0.0, 0.0, 0.0 # Placeholder features + ] + return np.array(basic_state, dtype=np.float32) + + return None + + except Exception as e: + logger.debug(f"Error getting DQN state features for {symbol}: {e}") + return None + def start_training(self): """Start the enhanced real-time training system""" if self.is_training: @@ -1885,7 +1913,7 @@ class EnhancedRealtimeTrainingSystem: and self.orchestrator.rl_agent): # Use RL agent to make prediction - current_state = self._get_dqn_state(symbol) + current_state = self._get_dqn_state_features(symbol) if current_state is None: return action = self.orchestrator.rl_agent.act(current_state, explore=False) diff --git a/web/clean_dashboard.py b/web/clean_dashboard.py index 3ca7ef3..4cb2e03 100644 --- a/web/clean_dashboard.py +++ b/web/clean_dashboard.py @@ -5205,9 +5205,7 @@ class CleanTradingDashboard: if self.orchestrator and hasattr(self.orchestrator, 'add_decision_callback'): def connect_worker(): try: - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) - loop.run_until_complete(self.orchestrator.add_decision_callback(self._on_trading_decision)) + self.orchestrator.add_decision_callback(self._on_trading_decision) logger.info("Successfully connected to orchestrator for trading signals.") except Exception as e: logger.error(f"Orchestrator connection worker failed: {e}")