This commit is contained in:
Dobromir Popov
2025-10-22 23:42:21 +03:00
parent f456b2747e
commit dbab0283c9
2 changed files with 108 additions and 46 deletions

View File

@@ -9,6 +9,7 @@ import sys
import logging import logging
import torch.nn.functional as F import torch.nn.functional as F
import time import time
from typing import Tuple, List, Dict, Any, Optional
# Add parent directory to path # Add parent directory to path
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

View File

@@ -305,6 +305,7 @@ class TradingOrchestrator:
self.data_provider = data_provider or DataProvider() self.data_provider = data_provider or DataProvider()
self.universal_adapter = UniversalDataAdapter(self.data_provider) self.universal_adapter = UniversalDataAdapter(self.data_provider)
self.model_manager = None # Will be initialized later if needed self.model_manager = None # Will be initialized later if needed
self.model_registry = model_registry # Model registry for dynamic model management
self.enhanced_rl_training = enhanced_rl_training self.enhanced_rl_training = enhanced_rl_training
# Set primary trading symbol # Set primary trading symbol
@@ -462,6 +463,45 @@ class TradingOrchestrator:
self.inference_logger = None # Will be initialized later if needed self.inference_logger = None # Will be initialized later if needed
self.db_manager = None # Will be initialized later if needed self.db_manager = None # Will be initialized later if needed
# CRITICAL: Initialize model_states dictionary to track model performance
self.model_states: Dict[str, Dict[str, Any]] = {
"dqn": {
"initial_loss": None,
"current_loss": None,
"best_loss": None,
"checkpoint_loaded": False,
"checkpoint_filename": None
},
"cnn": {
"initial_loss": None,
"current_loss": None,
"best_loss": None,
"checkpoint_loaded": False,
"checkpoint_filename": None
},
"extrema_trainer": {
"initial_loss": None,
"current_loss": None,
"best_loss": None,
"checkpoint_loaded": False,
"checkpoint_filename": None
},
"decision_fusion": {
"initial_loss": None,
"current_loss": None,
"best_loss": None,
"checkpoint_loaded": False,
"checkpoint_filename": None
},
"transformer": {
"initial_loss": None,
"current_loss": None,
"best_loss": None,
"checkpoint_loaded": False,
"checkpoint_filename": None
}
}
# ENHANCED: Real-time Training System Integration # ENHANCED: Real-time Training System Integration
self.enhanced_training_system = ( self.enhanced_training_system = (
None # Will be set to EnhancedRealtimeTrainingSystem if available None # Will be set to EnhancedRealtimeTrainingSystem if available
@@ -578,9 +618,16 @@ class TradingOrchestrator:
# Wait 24 hours before next cleanup # Wait 24 hours before next cleanup
await asyncio.sleep(24 * 60 * 60) # 24 hours in seconds await asyncio.sleep(24 * 60 * 60) # 24 hours in seconds
# Try to get or create event loop
try:
loop = asyncio.get_running_loop()
# Create and start the cleanup task # Create and start the cleanup task
self._db_cleanup_task = asyncio.create_task(cleanup_task()) self._db_cleanup_task = loop.create_task(cleanup_task())
logger.info("Database cleanup scheduler started - will run every 24 hours") logger.info("Database cleanup scheduler started - will run every 24 hours")
except RuntimeError:
# No running event loop - schedule for later
logger.info("No event loop available yet - database cleanup will be scheduled when loop starts")
self._db_cleanup_task = None
except Exception as e: except Exception as e:
logger.error(f"Failed to schedule database cleanup: {e}") logger.error(f"Failed to schedule database cleanup: {e}")
@@ -639,7 +686,7 @@ class TradingOrchestrator:
""" """
try: try:
if self.cob_integration is None: if self.cob_integration is None:
logger.warning("COB integration not initialized - cannot start sync") logger.info("COB integration not initialized - skipping sync")
return return
# Create async task to start COB integration # Create async task to start COB integration
@@ -651,9 +698,16 @@ class TradingOrchestrator:
except Exception as e: except Exception as e:
logger.error(f"Failed to start COB integration sync: {e}") logger.error(f"Failed to start COB integration sync: {e}")
# Try to get or create event loop
try:
loop = asyncio.get_running_loop()
# Create the task (will be executed when event loop is running) # Create the task (will be executed when event loop is running)
self._cob_sync_task = asyncio.create_task(start_cob_task()) self._cob_sync_task = loop.create_task(start_cob_task())
logger.info("COB integration sync task created - will start when event loop is available") logger.info("COB integration sync task created - will start when event loop is available")
except RuntimeError:
# No running event loop - schedule for later
logger.info("No event loop available yet - COB integration will be started when loop starts")
self._cob_sync_task = None
except Exception as e: except Exception as e:
logger.error(f"Failed to initialize COB integration sync: {e}") logger.error(f"Failed to initialize COB integration sync: {e}")
@@ -941,7 +995,8 @@ class TradingOrchestrator:
self.cob_rl_agent = None self.cob_rl_agent = None
# CRITICAL: Register models with the model registry # CRITICAL: Register models with the model registry (if available)
if self.model_registry is not None:
logger.info("Registering models with model registry...") logger.info("Registering models with model registry...")
logger.info( logger.info(
f"Model registry before registration: {len(self.model_registry.models)} models" f"Model registry before registration: {len(self.model_registry.models)} models"
@@ -982,9 +1037,13 @@ class TradingOrchestrator:
logger.error("Failed to register Extrema Trainer with registry") logger.error("Failed to register Extrema Trainer with registry")
except Exception as e: except Exception as e:
logger.error(f"Failed to register Extrema Trainer: {e}") logger.error(f"Failed to register Extrema Trainer: {e}")
else:
logger.info("Model registry not available - skipping model registration")
except Exception as e: except Exception as e:
logger.error(f"Error initializing ML models: {e}") logger.error(f"Error initializing ML models: {e}")
import traceback
logger.error(f"Traceback: {traceback.format_exc()}")
def get_model_training_stats(self) -> Dict[str, Dict[str, Any]]: def get_model_training_stats(self) -> Dict[str, Dict[str, Any]]:
"""Get current model training statistics for dashboard display""" """Get current model training statistics for dashboard display"""
@@ -1362,8 +1421,8 @@ class TradingOrchestrator:
def register_model_dynamically(self, model_name: str, model_interface): def register_model_dynamically(self, model_name: str, model_interface):
"""Register a new model dynamically and set up its toggle state""" """Register a new model dynamically and set up its toggle state"""
try: try:
# Register with model registry # Register with model registry (if available)
if self.model_registry.register_model(model_interface): if self.model_registry is not None and self.model_registry.register_model(model_interface):
# Initialize toggle state for the new model # Initialize toggle state for the new model
if model_name not in self.model_toggle_states: if model_name not in self.model_toggle_states:
self.model_toggle_states[model_name] = { self.model_toggle_states[model_name] = {
@@ -1373,6 +1432,8 @@ class TradingOrchestrator:
logger.info(f"Registered new model dynamically: {model_name}") logger.info(f"Registered new model dynamically: {model_name}")
self._save_ui_state() self._save_ui_state()
return True return True
elif self.model_registry is None:
logger.warning(f"Cannot register model {model_name} - model registry not available")
return False return False
except Exception as e: except Exception as e:
logger.error(f"Error registering model {model_name} dynamically: {e}") logger.error(f"Error registering model {model_name} dynamically: {e}")