cleanup, cob ladder still broken

This commit is contained in:
Dobromir Popov
2025-07-07 01:07:48 +03:00
parent 97d9bc97ee
commit 9101448e78
75 changed files with 546 additions and 12123 deletions

View File

@ -386,7 +386,7 @@ class TradingOrchestrator:
# Import COB integration directly (same as working dashboard)
from core.cob_integration import COBIntegration
# Initialize COB integration with our symbols
# Initialize COB integration with our symbols (but don't start it yet)
self.cob_integration = COBIntegration(symbols=self.symbols)
# Register callbacks to receive real-time COB data
@ -440,13 +440,21 @@ class TradingOrchestrator:
except Exception as e:
logger.error(f"Error initializing COB integration: {e}")
self.cob_integration = None
self.cob_integration = None # Ensure it's None if init fails
logger.info("COB integration will be disabled - models will use basic price data")
async def start_cob_integration(self):
"""Start COB integration with matrix data collection"""
try:
if self.cob_integration:
if not self.cob_integration:
logger.info("COB integration not initialized yet, creating instance.")
from core.cob_integration import COBIntegration
self.cob_integration = COBIntegration(symbols=self.symbols)
# Re-register callbacks if COBIntegration was just created
self.cob_integration.add_cnn_callback(self._on_cob_cnn_features)
self.cob_integration.add_dqn_callback(self._on_cob_dqn_features)
self.cob_integration.add_dashboard_callback(self._on_cob_dashboard_data)
logger.info("Starting COB integration with 5-minute matrix collection...")
# Start COB integration in background thread
@ -480,12 +488,11 @@ class TradingOrchestrator:
self._start_cob_matrix_worker()
logger.info("COB Integration started - 5-minute data matrix streaming active")
else:
logger.warning("COB integration is None - cannot start")
except Exception as e:
logger.error(f"Error starting COB integration: {e}")
self.cob_integration = None
logger.info("COB integration will be disabled - models will use basic price data")
def _start_cob_matrix_worker(self):
"""Start background worker for COB matrix updates"""
@ -760,7 +767,18 @@ class TradingOrchestrator:
def get_cob_snapshot(self, symbol: str) -> Optional[COBSnapshot]:
"""Get latest COB snapshot for a symbol"""
return self.latest_cob_data.get(symbol)
try:
# First try to get from COB integration (live data)
if self.cob_integration:
snapshot = self.cob_integration.get_cob_snapshot(symbol)
if snapshot:
return snapshot
# Fallback to cached data if COB integration not available
return self.latest_cob_data.get(symbol)
except Exception as e:
logger.warning(f"Error getting COB snapshot for {symbol}: {e}")
return None
def get_cob_feature_matrix(self, symbol: str, sequence_length: int = 60) -> Optional[np.ndarray]:
"""
@ -1325,12 +1343,25 @@ class TradingOrchestrator:
if state is None:
return None
# Get RL agent's action and confidence - use the actual underlying model
# Get RL agent's action, confidence, and q_values from the underlying model
if hasattr(model.model, 'act_with_confidence'):
action_idx, confidence = model.model.act_with_confidence(state)
# Call act_with_confidence and handle different return formats
result = model.model.act_with_confidence(state)
if len(result) == 3:
# EnhancedCNN format: (action, confidence, q_values)
action_idx, confidence, raw_q_values = result
elif len(result) == 2:
# DQN format: (action, confidence)
action_idx, confidence = result
raw_q_values = None
else:
logger.error(f"Unexpected return format from act_with_confidence: {len(result)} values")
return None
elif hasattr(model.model, 'act'):
action_idx = model.model.act(state, explore=False)
confidence = 0.7 # Default confidence for basic act method
raw_q_values = None # No raw q_values from simple act
else:
logger.error(f"RL model {model.name} has no act method")
return None
@ -1338,11 +1369,19 @@ class TradingOrchestrator:
action_names = ['SELL', 'HOLD', 'BUY']
action = action_names[action_idx]
# Convert raw_q_values to list if they are a tensor
q_values_for_capture = None
if raw_q_values is not None and hasattr(raw_q_values, 'tolist'):
q_values_for_capture = raw_q_values.tolist()
elif raw_q_values is not None and isinstance(raw_q_values, list):
q_values_for_capture = raw_q_values
# Create prediction object
prediction = Prediction(
action=action,
confidence=float(confidence),
probabilities={action: float(confidence), 'HOLD': 1.0 - float(confidence)},
# Use actual q_values if available, otherwise default probabilities
probabilities={action_names[i]: float(q_values_for_capture[i]) if q_values_for_capture else (1.0 / len(action_names)) for i in range(len(action_names))},
timeframe='mixed', # RL uses mixed timeframes
timestamp=datetime.now(),
model_name=model.name,
@ -1352,17 +1391,9 @@ class TradingOrchestrator:
# Capture DQN prediction for dashboard visualization
current_price = self._get_current_price(symbol)
if current_price:
# Get Q-values if available
q_values = [0.33, 0.33, 0.34] # Default
if hasattr(model, 'get_q_values'):
try:
q_values = model.get_q_values(state)
if hasattr(q_values, 'tolist'):
q_values = q_values.tolist()
except:
pass
self.capture_dqn_prediction(symbol, action_idx, float(confidence), current_price, q_values)
# Only pass q_values if they exist, otherwise pass empty list
q_values_to_pass = q_values_for_capture if q_values_for_capture is not None else []
self.capture_dqn_prediction(symbol, action_idx, float(confidence), current_price, q_values_to_pass)
return prediction
@ -2434,11 +2465,11 @@ class TradingOrchestrator:
self.decision_fusion_network = DecisionFusionNet()
logger.info("Decision fusion network initialized")
except Exception as e:
logger.warning(f"Decision fusion initialization failed: {e}")
self.decision_fusion_enabled = False
def _initialize_enhanced_training_system(self):
"""Initialize the enhanced real-time training system"""
try:
@ -2599,7 +2630,7 @@ class TradingOrchestrator:
if self.enhanced_training_system:
self.enhanced_training_system.dashboard = dashboard
logger.info("Dashboard reference set for enhanced training system")
except Exception as e:
logger.error(f"Error setting training dashboard: {e}")