improve training on signals, add save session button to store all progress

This commit is contained in:
Dobromir Popov
2025-07-02 10:59:13 +03:00
parent 488fbacf67
commit d15ebf54ca
3 changed files with 481 additions and 47 deletions

View File

@ -16,13 +16,13 @@ import time
import threading
import numpy as np
from datetime import datetime, timedelta
from typing import Dict, List, Optional, Tuple, Any
from dataclasses import dataclass
from typing import Dict, List, Optional, Tuple, Any, Union
from dataclasses import dataclass, field
from collections import deque
from .config import get_config
from .data_provider import DataProvider
from models import get_model_registry, ModelInterface, CNNModelInterface, RLAgentInterface
from models import get_model_registry, ModelInterface, CNNModelInterface, RLAgentInterface, ModelRegistry
# Import COB integration for real-time market microstructure data
try:
@ -45,7 +45,7 @@ class Prediction:
timeframe: str # Timeframe this prediction is for
timestamp: datetime
model_name: str # Name of the model that made this prediction
metadata: Dict[str, Any] = None # Additional model-specific data
metadata: Optional[Dict[str, Any]] = None # Additional model-specific data
@dataclass
class TradingDecision:
@ -62,10 +62,10 @@ class TradingOrchestrator:
"""
Enhanced Trading Orchestrator with full ML and COB integration
Coordinates CNN, DQN, and COB models for advanced trading decisions
Features real-time COB (Change of Bid) integration for market microstructure data
Features real-time COB (Change of Bid) data for market microstructure data
"""
def __init__(self, data_provider: DataProvider = None, enhanced_rl_training: bool = True, model_registry: Dict = None):
def __init__(self, data_provider: Optional[DataProvider] = None, enhanced_rl_training: bool = True, model_registry: Optional[ModelRegistry] = None):
"""Initialize the enhanced orchestrator with full ML capabilities"""
self.config = get_config()
self.data_provider = data_provider or DataProvider()
@ -79,18 +79,18 @@ class TradingOrchestrator:
self.symbols = self.config.get('symbols', ['ETH/USDT', 'BTC/USDT']) # Enhanced to support multiple symbols
# Dynamic weights (will be adapted based on performance)
self.model_weights = {} # {model_name: weight}
self.model_weights: Dict[str, float] = {} # {model_name: weight}
self._initialize_default_weights()
# State tracking
self.last_decision_time = {} # {symbol: datetime}
self.recent_decisions = {} # {symbol: List[TradingDecision]}
self.model_performance = {} # {model_name: {'correct': int, 'total': int, 'accuracy': float}}
self.last_decision_time: Dict[str, datetime] = {} # {symbol: datetime}
self.recent_decisions: Dict[str, List[TradingDecision]] = {} # {symbol: List[TradingDecision]}
self.model_performance: Dict[str, Dict[str, Any]] = {} # {model_name: {'correct': int, 'total': int, 'accuracy': float}}
# Model prediction tracking for dashboard visualization
self.recent_dqn_predictions = {} # {symbol: List[Dict]} - Recent DQN predictions
self.recent_cnn_predictions = {} # {symbol: List[Dict]} - Recent CNN predictions
self.prediction_accuracy_history = {} # {symbol: List[Dict]} - Prediction accuracy tracking
self.recent_dqn_predictions: Dict[str, deque] = {} # {symbol: List[Dict]} - Recent DQN predictions
self.recent_cnn_predictions: Dict[str, deque] = {} # {symbol: List[Dict]} - Recent CNN predictions
self.prediction_accuracy_history: Dict[str, deque] = {} # {symbol: List[Dict]} - Prediction accuracy tracking
# Initialize prediction tracking for each symbol
for symbol in self.symbols:
@ -99,39 +99,45 @@ class TradingOrchestrator:
self.prediction_accuracy_history[symbol] = deque(maxlen=200)
# Decision callbacks
self.decision_callbacks = []
self.decision_callbacks: List[Any] = []
# ENHANCED: Decision Fusion System - Built into orchestrator (no separate file needed!)
self.decision_fusion_enabled = True
self.decision_fusion_network = None
self.fusion_training_history = []
self.last_fusion_inputs = {}
self.fusion_checkpoint_frequency = 50 # Save every 50 decisions
self.fusion_decisions_count = 0
self.fusion_training_data = [] # Store training examples for decision model
self.decision_fusion_enabled: bool = True
self.decision_fusion_network: Any = None
self.fusion_training_history: List[Any] = []
self.last_fusion_inputs: Dict[str, Any] = {} # Fix: Explicitly initialize as dictionary
self.fusion_checkpoint_frequency: int = 50 # Save every 50 decisions
self.fusion_decisions_count: int = 0
self.fusion_training_data: List[Any] = [] # Store training examples for decision model
# COB Integration - Real-time market microstructure data
self.cob_integration = None
self.cob_integration: Optional[COBIntegration] = None # Fix: Use Optional for COBIntegration
self.latest_cob_data: Dict[str, Any] = {} # {symbol: COBSnapshot}
self.latest_cob_features: Dict[str, Any] = {} # {symbol: np.ndarray} - CNN features
self.latest_cob_state: Dict[str, Any] = {} # {symbol: np.ndarray} - DQN state features
self.cob_feature_history: Dict[str, List] = {symbol: [] for symbol in self.symbols} # Rolling history for models
self.cob_feature_history: Dict[str, List[Any]] = {symbol: [] for symbol in self.symbols} # Rolling history for models
# Enhanced ML Models
self.rl_agent = None # DQN Agent
self.cnn_model = None # CNN Model for pattern recognition
self.extrema_trainer = None # Extrema/pivot trainer
self.rl_agent: Any = None # DQN Agent
self.cnn_model: Any = None # CNN Model for pattern recognition
self.extrema_trainer: Any = None # Extrema/pivot trainer
self.primary_transformer: Any = None # Transformer model
self.primary_transformer_trainer: Any = None # Transformer model trainer
self.transformer_checkpoint_info: Dict[str, Any] = {} # Transformer checkpoint info
self.cob_rl_agent: Any = None # COB RL Agent
self.decision_model: Any = None # Decision Fusion model
self.latest_cnn_features: Dict[str, Any] = {} # CNN hidden features
self.latest_cnn_predictions: Dict[str, Any] = {} # CNN predictions
# Enhanced RL features
self.sensitivity_learning_queue = [] # For outcome-based learning
self.perfect_move_buffer = [] # Buffer for perfect move analysis
self.position_status = {} # Current positions
self.sensitivity_learning_queue: List[Any] = [] # For outcome-based learning
self.perfect_move_buffer: List[Any] = [] # Buffer for perfect move analysis
self.position_status: Dict[str, Any] = {} # Current positions
# Real-time processing
self.realtime_processing = False
self.realtime_tasks = []
self.realtime_processing: bool = False
self.realtime_tasks: List[Any] = []
logger.info("Enhanced TradingOrchestrator initialized with full ML capabilities")
logger.info(f"Enhanced RL training: {enhanced_rl_training}")
@ -310,9 +316,10 @@ class TradingOrchestrator:
self.cob_integration = COBIntegration(symbols=self.symbols)
# Register callbacks to receive real-time COB data
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)
if self.cob_integration:
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)
# Initialize 5-minute COB data matrix system
self.cob_matrix_duration = 300 # 5 minutes in seconds
@ -320,9 +327,9 @@ class TradingOrchestrator:
self.cob_matrix_size = self.cob_matrix_duration // self.cob_matrix_resolution # 300 samples
# COB data matrix storage - 5 minutes of 1-second snapshots
self.cob_data_matrix: Dict[str, deque] = {}
self.cob_feature_matrix: Dict[str, deque] = {}
self.cob_state_matrix: Dict[str, deque] = {}
self.cob_data_matrix: Dict[str, deque[Any]] = {}
self.cob_feature_matrix: Dict[str, deque[Any]] = {}
self.cob_state_matrix: Dict[str, deque[Any]] = {}
# Initialize matrix storage for each symbol
for symbol in self.symbols:
@ -336,16 +343,16 @@ class TradingOrchestrator:
self.cob_state_matrix[symbol] = deque(maxlen=self.cob_matrix_size)
# Initialize COB data storage (legacy support)
self.latest_cob_snapshots = {}
self.cob_feature_cache = {}
self.cob_state_cache = {}
self.latest_cob_snapshots: Dict[str, Any] = {}
self.cob_feature_cache: Dict[str, Any] = {}
self.cob_state_cache: Dict[str, Any] = {}
# COB matrix update tracking
self.last_cob_matrix_update = {}
self.last_cob_matrix_update: Dict[str, float] = {}
self.cob_matrix_update_interval = 1.0 # Update every 1 second
# COB matrix statistics
self.cob_matrix_stats = {
self.cob_matrix_stats: Dict[str, Any] = {
'total_updates': 0,
'matrix_fills': {symbol: 0 for symbol in self.symbols},
'feature_generations': 0,
@ -375,7 +382,8 @@ class TradingOrchestrator:
asyncio.set_event_loop(loop)
async def cob_main():
await self.cob_integration.start()
if self.cob_integration: # Additional check
await self.cob_integration.start()
# Keep running until stopped
while True:
await asyncio.sleep(1)