diff --git a/core/multi_exchange_cob_provider.py b/core/multi_exchange_cob_provider.py index d3dd2c7..f69aca0 100644 --- a/core/multi_exchange_cob_provider.py +++ b/core/multi_exchange_cob_provider.py @@ -750,7 +750,7 @@ class MultiExchangeCOBProvider: # Log consolidation performance every 100 iterations if len(self.processing_times['consolidation']) % 100 == 0: avg_time = sum(self.processing_times['consolidation']) / len(self.processing_times['consolidation']) - logger.info(f"Average consolidation time: {avg_time:.2f}ms") + logger.debug(f"Average consolidation time: {avg_time:.2f}ms") await asyncio.sleep(0.1) # 100ms consolidation frequency diff --git a/main.py b/main.py index 8efb1cc..2674716 100644 --- a/main.py +++ b/main.py @@ -122,34 +122,47 @@ def start_web_ui(): logger.info("=" * 50) logger.info("Starting Main Trading Dashboard UI...") logger.info("Trading Dashboard: http://127.0.0.1:8051") + logger.info("COB Integration: ENABLED (Real-time order book visualization)") logger.info("=" * 50) - # Import and create the main TradingDashboard (simplified approach) + # Import and create the main TradingDashboard with COB integration from web.dashboard import TradingDashboard from core.data_provider import DataProvider - from core.orchestrator import TradingOrchestrator + from core.enhanced_orchestrator import EnhancedTradingOrchestrator # Use enhanced version with COB from core.trading_executor import TradingExecutor # Initialize components for the dashboard config = get_config() data_provider = DataProvider() - # Create orchestrator for the dashboard (standard version for UI compatibility) - dashboard_orchestrator = TradingOrchestrator(data_provider=data_provider) + # Load model registry for enhanced features + try: + from models import get_model_registry + model_registry = {} # Use simple dict for now + except ImportError: + model_registry = {} - trading_executor = TradingExecutor() + # Create enhanced orchestrator for the dashboard (WITH COB integration) + dashboard_orchestrator = EnhancedTradingOrchestrator( + data_provider=data_provider, + symbols=config.get('symbols', ['ETH/USDT']), + enhanced_rl_training=True, # Enable RL training display + model_registry=model_registry + ) - # Create the main trading dashboard + trading_executor = TradingExecutor("config.yaml") + + # Create the main trading dashboard with enhanced features dashboard = TradingDashboard( data_provider=data_provider, orchestrator=dashboard_orchestrator, trading_executor=trading_executor ) - logger.info("Main TradingDashboard created successfully") - logger.info("Features: Live trading, RL training monitoring, Position management") + logger.info("Enhanced TradingDashboard created successfully") + logger.info("Features: Live trading, COB visualization, RL training monitoring, Position management") - # Run the dashboard server (simplified - no async loop) + # Run the dashboard server (COB integration will start automatically) dashboard.app.run(host='127.0.0.1', port=8051, debug=False, use_reloader=False) except Exception as e: diff --git a/web/dashboard.py b/web/dashboard.py index ca23dc5..012451c 100644 --- a/web/dashboard.py +++ b/web/dashboard.py @@ -92,6 +92,26 @@ except ImportError as e: class UIDataPacket: def __init__(self, *args, **kwargs): pass +# Import COB integration components if available +try: + from core.cob_integration import COBIntegration + from core.multi_exchange_cob_provider import MultiExchangeCOBProvider, COBSnapshot + COB_INTEGRATION_AVAILABLE = True + logger.info("COB integration components available") +except ImportError as e: + logger.warning(f"COB integration components not available: {e}") + COB_INTEGRATION_AVAILABLE = False + # Create fallback classes + class COBSnapshot: + def __init__(self, *args, **kwargs): + self.symbol = "N/A" + self.consolidated_bids = [] + self.consolidated_asks = [] + self.volume_weighted_mid = 0.0 + self.spread_bps = 0.0 + self.total_bid_liquidity = 0.0 + self.total_ask_liquidity = 0.0 + class AdaptiveThresholdLearner: """Learn optimal confidence thresholds based on real trade outcomes""" @@ -871,6 +891,19 @@ class TradingDashboard: ], className="card") ], className="mb-3"), + # COB (Consolidated Order Book) Visualization Section + html.Div([ + html.Div([ + html.Div([ + html.H6([ + html.I(className="fas fa-layer-group me-2"), + "Consolidated Order Book (COB) - Real-time Market Microstructure" + ], className="card-title mb-2"), + html.Div(id="cob-visualization-content", style={"height": "400px", "overflowY": "auto"}) + ], className="card-body p-2") + ], className="card") + ], className="mb-3"), + # Bottom row - Session performance and system status html.Div([ @@ -992,7 +1025,8 @@ class TradingDashboard: Output('system-status-details', 'children'), Output('current-leverage', 'children'), Output('leverage-risk', 'children'), - Output('cnn-monitoring-content', 'children') + Output('cnn-monitoring-content', 'children'), + Output('cob-visualization-content', 'children') ], [Input('interval-component', 'n_intervals')] ) @@ -1200,13 +1234,20 @@ class TradingDashboard: else: risk_level = "Extreme Risk" + # Generate COB visualization content + try: + cob_content = self._create_cob_visualization_content() + except Exception as e: + logger.warning(f"COB visualization error: {e}") + cob_content = [html.P("COB data loading...", className="text-muted")] + # BUILD FINAL RESULT result = ( price_text, pnl_text, pnl_class, fees_text, position_text, position_class, trade_count_text, portfolio_text, mexc_status, price_chart, training_metrics, decisions_list, session_perf, closed_trades_table, system_status['icon_class'], system_status['title'], system_status['details'], leverage_text, risk_level, - cnn_monitoring_content + cnn_monitoring_content, cob_content ) # Cache the result for emergencies @@ -5923,7 +5964,8 @@ class TradingDashboard: "fas fa-circle text-warning fa-2x", "Loading", [html.P("Loading...", className="text-muted")], f"{self.leverage_multiplier:.0f}x", "Loading", - [html.P("Loading...", className="text-muted")] + [html.P("Loading...", className="text-muted")], + [html.P("COB loading...", className="text-muted")] ) def _process_signal_optimized(self, signal): @@ -6097,6 +6139,138 @@ class TradingDashboard: ] except: return [html.P("CNN monitoring unavailable", className="text-muted")] + + def _create_cob_visualization_content(self) -> List: + """Create COB (Consolidated Order Book) visualization content""" + try: + content = [] + + # Check if we have enhanced orchestrator with COB integration + if not hasattr(self.orchestrator, 'latest_cob_features') or not hasattr(self.orchestrator, 'cob_integration'): + content.append(html.P("COB integration not available - using basic orchestrator", className="text-warning")) + return content + + # Get COB data for primary symbols + symbols = ['ETH/USDT', 'BTC/USDT'] + + for symbol in symbols: + # Get COB features and state + cob_features = getattr(self.orchestrator, 'latest_cob_features', {}).get(symbol) + cob_state = getattr(self.orchestrator, 'latest_cob_state', {}).get(symbol) + + # Get COB snapshot if integration is active + cob_snapshot = None + if hasattr(self.orchestrator, 'cob_integration') and self.orchestrator.cob_integration: + try: + cob_snapshot = self.orchestrator.cob_integration.get_cob_snapshot(symbol) + except: + pass + + # Create symbol section + content.append(html.H6(f"{symbol} - Consolidated Order Book", className="text-primary")) + + # COB Features Status + if cob_features is not None: + content.append(html.P([ + html.Strong("CNN Features: "), + f"Shape {cob_features.shape} - Ready for ML training" + ], className="text-success small")) + else: + content.append(html.P([ + html.Strong("CNN Features: "), + "Not available" + ], className="text-warning small")) + + # COB State Status + if cob_state is not None: + content.append(html.P([ + html.Strong("RL State: "), + f"Shape {cob_state.shape} - Ready for DQN training" + ], className="text-success small")) + else: + content.append(html.P([ + html.Strong("RL State: "), + "Not available" + ], className="text-warning small")) + + # COB Snapshot Details + if cob_snapshot: + content.append(html.Div([ + html.P([ + html.Strong("Mid Price: "), + f"${cob_snapshot.volume_weighted_mid:.2f}" + ], className="text-info small mb-1"), + html.P([ + html.Strong("Spread: "), + f"{cob_snapshot.spread_bps:.1f} bps" + ], className="text-info small mb-1"), + html.P([ + html.Strong("Bid Liquidity: "), + f"${cob_snapshot.total_bid_liquidity:,.0f}" + ], className="text-success small mb-1"), + html.P([ + html.Strong("Ask Liquidity: "), + f"${cob_snapshot.total_ask_liquidity:,.0f}" + ], className="text-success small mb-1"), + html.P([ + html.Strong("Active Exchanges: "), + ", ".join(cob_snapshot.exchanges_active) + ], className="text-secondary small mb-1"), + html.P([ + html.Strong("Order Book Levels: "), + f"{len(cob_snapshot.consolidated_bids)} bids, {len(cob_snapshot.consolidated_asks)} asks" + ], className="text-secondary small mb-1") + ], className="border-start border-primary ps-2 mb-2")) + else: + content.append(html.P("COB snapshot not available", className="text-muted small")) + + content.append(html.Hr()) + + # Training integration status + content.append(html.H6("COB → Training Pipeline Status", className="text-info")) + + # Check if COB data is being used in training + training_active = False + if hasattr(self.orchestrator, 'enhanced_rl_training') and self.orchestrator.enhanced_rl_training: + training_active = True + content.append(html.P([ + html.I(className="fas fa-check-circle text-success me-2"), + "COB data integrated into RL training pipeline" + ], className="small")) + content.append(html.P([ + html.I(className="fas fa-brain text-info me-2"), + "Real-time market microstructure → CNN features → RL states" + ], className="small")) + else: + content.append(html.P([ + html.I(className="fas fa-exclamation-triangle text-warning me-2"), + "COB training integration not active" + ], className="small")) + + # Performance metrics + if training_active: + try: + # Get COB integration performance + cob_update_count = 0 + last_update = "Never" + + for symbol in symbols: + if symbol in getattr(self.orchestrator, 'latest_cob_features', {}): + cob_update_count += 1 + + content.append(html.P([ + html.Strong("COB Updates: "), + f"{cob_update_count} symbols receiving data" + ], className="text-info small")) + + except Exception as e: + content.append(html.P(f"Error getting COB metrics: {e}", className="text-danger small")) + + return content + + except Exception as e: + logger.error(f"Error creating COB visualization: {e}") + return [html.P(f"COB visualization error: {str(e)}", className="text-danger")] def create_dashboard(data_provider: DataProvider = None, orchestrator: TradingOrchestrator = None, trading_executor: TradingExecutor = None) -> TradingDashboard: