#!/usr/bin/env python3 """ Real-time Trading Dashboard & Live Training Main entry point for: - Live market data streaming - Real-time model training - Web dashboard visualization - Live trading execution Usage: python main_dashboard.py [--port 8051] [--no-training] Examples: # Full system with training python main_dashboard.py --port 8051 # Dashboard only (no training) python main_dashboard.py --port 8051 --no-training """ import os import sys import logging import argparse from typing import Optional # Add project root to path sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) # Import core components try: from core.config import setup_logging from core.data_provider import DataProvider from core.orchestrator import TradingOrchestrator from core.trading_executor import TradingExecutor from web.clean_dashboard import create_clean_dashboard except ImportError as e: print(f"Error importing core modules: {e}") sys.exit(1) logger = logging.getLogger(__name__) def create_safe_orchestrator() -> Optional[TradingOrchestrator]: """Create orchestrator with safe CNN model handling""" try: # Create orchestrator with basic configuration (uses correct constructor parameters) orchestrator = TradingOrchestrator( enhanced_rl_training=True # Enable RL training for model improvement ) logger.info("Trading orchestrator created successfully") return orchestrator except Exception as e: logger.error(f"Error creating orchestrator: {e}") logger.info("Continuing without orchestrator - dashboard will run in view-only mode") return None def create_safe_trading_executor() -> Optional[TradingExecutor]: """Create trading executor with safe configuration""" try: # TradingExecutor only accepts config_path parameter trading_executor = TradingExecutor(config_path="config.yaml") logger.info("Trading executor created successfully") return trading_executor except Exception as e: logger.error(f"Error creating trading executor: {e}") logger.info("Continuing without trading executor - dashboard will be view-only") return None def main(): """Main entry point for realtime dashboard""" parser = argparse.ArgumentParser(description='Real-time Trading Dashboard') parser.add_argument('--port', type=int, default=8051, help='Dashboard port (default: 8051)') parser.add_argument('--host', type=str, default='127.0.0.1', help='Dashboard host (default: 127.0.0.1)') parser.add_argument('--debug', action='store_true', help='Enable debug mode') parser.add_argument('--no-training', action='store_true', help='Disable ML training for stability') args = parser.parse_args() # Setup logging try: setup_logging() logger.info("=" * 80) logger.info("REAL-TIME TRADING DASHBOARD & LIVE TRAINING") logger.info("=" * 80) logger.info(f"Dashboard: http://{args.host}:{args.port}") logger.info(f"Training: {'DISABLED' if args.no_training else 'ENABLED'}") logger.info("Features: Real-time Charts, Trading Interface, Model Monitoring") logger.info("=" * 80) except Exception as e: print(f"Error setting up logging: {e}") # Continue without logging setup # Set environment variables for optimization os.environ['ENABLE_REALTIME_CHARTS'] = '1' if not args.no_training: os.environ['ENABLE_NN_MODELS'] = '1' try: # Model Selection at Startup logger.info("Performing intelligent model selection...") try: from utils.model_selector import select_and_load_best_models selected_models, loaded_models = select_and_load_best_models() logger.info(f"Selected {len(selected_models)} model types, loaded {len(loaded_models)} models") except Exception as e: logger.warning(f"Model selection failed, using defaults: {e}") selected_models, loaded_models = {}, {} # Create data provider logger.info("Initializing data provider...") data_provider = DataProvider(symbols=['ETH/USDT', 'BTC/USDT']) # Create orchestrator (with safe CNN handling) logger.info("Initializing trading orchestrator...") orchestrator = create_safe_orchestrator() # Create trading executor logger.info("Initializing trading executor...") trading_executor = create_safe_trading_executor() # Create and run dashboard logger.info("Creating dashboard...") dashboard = create_clean_dashboard( data_provider=data_provider, orchestrator=orchestrator, trading_executor=trading_executor ) # Start the dashboard server logger.info(f"Starting dashboard server on http://{args.host}:{args.port}") dashboard.run_server( host=args.host, port=args.port, debug=args.debug ) except KeyboardInterrupt: logger.info("Dashboard stopped by user") except Exception as e: logger.error(f"Error running dashboard: {e}") # Try to provide helpful error message if "model.fit" in str(e) or "CNN" in str(e): logger.error("CNN model training error detected. Try running with --no-training flag") logger.error("Command: python main_dashboard.py --no-training") sys.exit(1) finally: logger.info("Dashboard shutdown complete") if __name__ == '__main__': main()