From 1d224e5b8ce3134cf62ccf2c726d8543edd280a4 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Tue, 22 Jul 2025 16:28:16 +0300 Subject: [PATCH] references --- core/orchestrator.py | 135 --------------------------------------- core/trading_executor.py | 4 +- test_mexc_order_fix.py | 2 +- 3 files changed, 3 insertions(+), 138 deletions(-) diff --git a/core/orchestrator.py b/core/orchestrator.py index 2f9cecb..91399a6 100644 --- a/core/orchestrator.py +++ b/core/orchestrator.py @@ -2246,144 +2246,9 @@ class TradingOrchestrator: try: model_obj = None current_loss = None - model_type = model_name # Get model object and calculate current performance if model_name == 'dqn' and self.rl_agent: - model_obj = self.rl_agent - # Use current loss from model state or estimate from performance - current_loss = self.model_states['dqn'].get('current_loss') - if current_loss is None: - # Estimate loss from performance score (inverse relationship) - current_loss = max(0.001, 1.0 - performance_score) - - # Update model state tracking - self.model_states['dqn']['current_loss'] = current_loss - - # If this is the first loss value, set it as initial and best - if self.model_states['dqn']['initial_loss'] is None: - self.model_states['dqn']['initial_loss'] = current_loss - if self.model_states['dqn']['best_loss'] is None or current_loss < self.model_states['dqn']['best_loss']: - self.model_states['dqn']['best_loss'] = current_loss - - elif model_name == 'cnn' and self.cnn_model: - model_obj = self.cnn_model - # Use current loss from model state or estimate from performance - current_loss = self.model_states['cnn'].get('current_loss') - if current_loss is None: - # Estimate loss from performance score (inverse relationship) - current_loss = max(0.001, 1.0 - performance_score) - - # Update model state tracking - self.model_states['cnn']['current_loss'] = current_loss - - # If this is the first loss value, set it as initial and best - if self.model_states['cnn']['initial_loss'] is None: - self.model_states['cnn']['initial_loss'] = current_loss - if self.model_states['cnn']['best_loss'] is None or current_loss < self.model_states['cnn']['best_loss']: - self.model_states['cnn']['best_loss'] = current_loss - - elif model_name == 'cob_rl' and self.cob_rl_agent: - model_obj = self.cob_rl_agent - # Use current loss from model state or estimate from performance - current_loss = self.model_states['cob_rl'].get('current_loss') - if current_loss is None: - # Estimate loss from performance score (inverse relationship) - current_loss = max(0.001, 1.0 - performance_score) - - # Update model state tracking - self.model_states['cob_rl']['current_loss'] = current_loss - - # If this is the first loss value, set it as initial and best - if self.model_states['cob_rl']['initial_loss'] is None: - self.model_states['cob_rl']['initial_loss'] = current_loss - if self.model_states['cob_rl']['best_loss'] is None or current_loss < self.model_states['cob_rl']['best_loss']: - self.model_states['cob_rl']['best_loss'] = current_loss - - elif model_name == 'extrema' and hasattr(self, 'extrema_trainer') and self.extrema_trainer: - model_obj = self.extrema_trainer - # Use current loss from model state or estimate from performance - current_loss = self.model_states['extrema'].get('current_loss') - if current_loss is None: - # Estimate loss from performance score (inverse relationship) - current_loss = max(0.001, 1.0 - performance_score) - - # Update model state tracking - self.model_states['extrema']['current_loss'] = current_loss - - # If this is the first loss value, set it as initial and best - if self.model_states['extrema']['initial_loss'] is None: - self.model_states['extrema']['initial_loss'] = current_loss - if self.model_states['extrema']['best_loss'] is None or current_loss < self.model_states['extrema']['best_loss']: - self.model_states['extrema']['best_loss'] = current_loss - - # Skip if we couldn't get a model object - if model_obj is None: - continue - - # Prepare performance metrics for checkpoint - performance_metrics = { - 'loss': current_loss, - 'accuracy': performance_score, # Use confidence as a proxy for accuracy - } - - # Prepare training metadata - training_metadata = { - 'training_iteration': self.training_iterations, - 'timestamp': datetime.now().isoformat() - } - - # Save checkpoint using checkpoint manager - from utils.checkpoint_manager import save_checkpoint - checkpoint_metadata = save_checkpoint( - model=model_obj, - model_name=model_name, - model_type=model_type, - performance_metrics=performance_metrics, - training_metadata=training_metadata - ) - - if checkpoint_metadata: - logger.info(f"Saved checkpoint for {model_name}: {checkpoint_metadata.checkpoint_id} (loss={current_loss:.4f})") - - # Also save periodically based on training iterations - if self.training_iterations % 100 == 0: - # Force save every 100 training iterations regardless of performance - checkpoint_metadata = save_checkpoint( - model=model_obj, - model_name=model_name, - model_type=model_type, - performance_metrics=performance_metrics, - training_metadata=training_metadata, - force_save=True - ) - if checkpoint_metadata: - logger.info(f"Periodic checkpoint saved for {model_name}: {checkpoint_metadata.checkpoint_id}") - - except Exception as e: - logger.error(f"Error saving checkpoint for {model_name}: {e}") - - except Exception as e: - logger.error(f"Error in _save_training_checkpoints: {e}") - - def _initialize_checkpoint_manager(self): - """Initialize the checkpoint manager for model persistence""" - try: - from utils.checkpoint_manager import get_checkpoint_manager - self.checkpoint_manager = get_checkpoint_manager() - - # Initialize model states dictionary to track performance - self.model_states = { - 'dqn': {'initial_loss': None, 'current_loss': None, 'best_loss': float('inf'), 'checkpoint_loaded': False}, - 'cnn': {'initial_loss': None, 'current_loss': None, 'best_loss': float('inf'), 'checkpoint_loaded': False}, - 'cob_rl': {'initial_loss': None, 'current_loss': None, 'best_loss': float('inf'), 'checkpoint_loaded': False}, - 'extrema': {'initial_loss': None, 'current_loss': None, 'best_loss': float('inf'), 'checkpoint_loaded': False} - } - - logger.info("Checkpoint manager initialized for model persistence") - except Exception as e: - logger.error(f"Error initializing checkpoint manager: {e}") - self.checkpoint_manager = None model_obj = self.rl_agent # Use negative performance score as loss (higher confidence = lower loss) current_loss = 1.0 - performance_score diff --git a/core/trading_executor.py b/core/trading_executor.py index 9917df1..6b234da 100644 --- a/core/trading_executor.py +++ b/core/trading_executor.py @@ -22,8 +22,8 @@ import sys # Add NN directory to path for exchange interfaces sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'NN')) -from NN.exchanges.exchange_factory import ExchangeFactory -from NN.exchanges.exchange_interface import ExchangeInterface +from core.exchanges.exchange_factory import ExchangeFactory +from core.exchanges.exchange_interface import ExchangeInterface from .config import get_config from .config_sync import ConfigSynchronizer diff --git a/test_mexc_order_fix.py b/test_mexc_order_fix.py index 8b99cee..6c3f1b6 100644 --- a/test_mexc_order_fix.py +++ b/test_mexc_order_fix.py @@ -29,7 +29,7 @@ def test_mexc_order_fix(): # Import after path setup try: - from NN.exchanges.mexc_interface import MEXCInterface + from core.exchanges.mexc_interface import MEXCInterface except ImportError as e: print(f"❌ Import error: {e}") return False