gogo2/main_clean.py
2025-05-30 19:35:11 +03:00

248 lines
10 KiB
Python

#!/usr/bin/env python3
"""
Clean Trading System - Streamlined Entry Point
Simplified entry point with only essential modes:
- test: Test data provider and core components
- web: Live trading dashboard with integrated training pipeline
Streamlined Flow: Data -> Indicators/Pivots -> CNN -> RL -> Orchestrator -> Execution
Usage:
python main_clean.py --mode [test|web] --symbol ETH/USDT
"""
import asyncio
import argparse
import logging
import sys
from pathlib import Path
from threading import Thread
import time
# Add project root to path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
from core.config import get_config, setup_logging, Config
from core.data_provider import DataProvider
logger = logging.getLogger(__name__)
def run_data_test():
"""Test the enhanced data provider and core components"""
try:
config = get_config()
logger.info("Testing Enhanced Data Provider and Core Components...")
# Test data provider with multiple timeframes
data_provider = DataProvider(
symbols=['ETH/USDT'],
timeframes=['1s', '1m', '1h', '4h']
)
# Test historical data
logger.info("Testing historical data fetching...")
df = data_provider.get_historical_data('ETH/USDT', '1h', limit=100)
if df is not None:
logger.info(f"[SUCCESS] Historical data: {len(df)} candles loaded")
logger.info(f" Columns: {len(df.columns)} total")
logger.info(f" Date range: {df['timestamp'].min()} to {df['timestamp'].max()}")
# Show indicator breakdown
basic_cols = ['timestamp', 'open', 'high', 'low', 'close', 'volume']
indicators = [col for col in df.columns if col not in basic_cols]
logger.info(f" Technical indicators: {len(indicators)}")
else:
logger.error("[FAILED] Failed to load historical data")
# Test multi-timeframe feature matrix
logger.info("Testing multi-timeframe feature matrix...")
feature_matrix = data_provider.get_feature_matrix('ETH/USDT', ['1h', '4h'], window_size=20)
if feature_matrix is not None:
logger.info(f"[SUCCESS] Feature matrix shape: {feature_matrix.shape}")
logger.info(f" Timeframes: {feature_matrix.shape[0]}")
logger.info(f" Window size: {feature_matrix.shape[1]}")
logger.info(f" Features: {feature_matrix.shape[2]}")
else:
logger.error("[FAILED] Failed to create feature matrix")
# Test CNN model availability
try:
from NN.models.cnn_model import CNNModel
cnn = CNNModel(n_actions=2) # 2-action system
logger.info("[SUCCESS] CNN model initialized with 2 actions (BUY/SELL)")
except Exception as e:
logger.warning(f"[WARNING] CNN model not available: {e}")
# Test RL agent availability
try:
from NN.models.dqn_agent import DQNAgent
agent = DQNAgent(state_shape=(50,), n_actions=2) # 2-action system
logger.info("[SUCCESS] RL Agent initialized with 2 actions (BUY/SELL)")
except Exception as e:
logger.warning(f"[WARNING] RL Agent not available: {e}")
# Test orchestrator
try:
from core.enhanced_orchestrator import EnhancedTradingOrchestrator
orchestrator = EnhancedTradingOrchestrator(data_provider)
logger.info("[SUCCESS] Enhanced Trading Orchestrator initialized")
except Exception as e:
logger.warning(f"[WARNING] Enhanced Orchestrator not available: {e}")
# Test health check
health = data_provider.health_check()
logger.info(f"[SUCCESS] Data provider health check completed")
logger.info("[SUCCESS] Core system test completed successfully!")
logger.info("2-Action System: BUY/SELL only (no HOLD)")
logger.info("Streamlined Flow: Data -> Indicators -> CNN -> RL -> Orchestrator -> Execution")
except Exception as e:
logger.error(f"Error in system test: {e}")
import traceback
logger.error(traceback.format_exc())
raise
def run_web_dashboard():
"""Run the streamlined web dashboard with integrated training pipeline"""
try:
logger.info("Starting Streamlined Trading Dashboard...")
logger.info("2-Action System: BUY/SELL with intelligent position management")
logger.info("Integrated Training Pipeline: Live data -> Models -> Trading")
# Get configuration
config = get_config()
# Initialize core components for streamlined pipeline
from core.data_provider import DataProvider
from core.enhanced_orchestrator import EnhancedTradingOrchestrator
from core.trading_executor import TradingExecutor
# Create data provider
data_provider = DataProvider()
# Verify data connection
logger.info("[DATA] Verifying live data connection...")
symbol = config.get('symbols', ['ETH/USDT'])[0]
test_df = data_provider.get_historical_data(symbol, '1m', limit=10)
if test_df is not None and len(test_df) > 0:
logger.info("[SUCCESS] Data connection verified")
logger.info(f"[SUCCESS] Fetched {len(test_df)} candles for validation")
else:
logger.error("[ERROR] Data connection failed - no live data available")
return
# Load model registry for integrated pipeline
try:
from core.model_registry import get_model_registry
model_registry = get_model_registry()
logger.info("[MODELS] Model registry loaded for integrated training")
except ImportError:
model_registry = {}
logger.warning("Model registry not available, using empty registry")
# Create streamlined orchestrator with 2-action system
orchestrator = EnhancedTradingOrchestrator(
data_provider=data_provider,
symbols=config.get('symbols', ['ETH/USDT']),
enhanced_rl_training=True,
model_registry=model_registry
)
logger.info("Enhanced Trading Orchestrator with 2-Action System initialized")
# Create trading executor for live execution
trading_executor = TradingExecutor()
# Import and create streamlined dashboard
from web.dashboard import TradingDashboard
dashboard = TradingDashboard(
data_provider=data_provider,
orchestrator=orchestrator,
trading_executor=trading_executor
)
# Start the integrated dashboard
port = config.get('web', {}).get('port', 8050)
host = config.get('web', {}).get('host', '127.0.0.1')
logger.info(f"Starting Streamlined Dashboard at http://{host}:{port}")
logger.info("Live Data Processing: ENABLED")
logger.info("Integrated CNN Training: ENABLED")
logger.info("Integrated RL Training: ENABLED")
logger.info("Real-time Indicators & Pivots: ENABLED")
logger.info("Live Trading Execution: ENABLED")
logger.info("2-Action System: BUY/SELL with position intelligence")
logger.info("Pipeline: Data -> Indicators -> CNN -> RL -> Orchestrator -> Execution")
dashboard.run(host=host, port=port, debug=False)
except Exception as e:
logger.error(f"Error in streamlined dashboard: {e}")
logger.error("Dashboard stopped - trying minimal fallback")
try:
# Minimal fallback dashboard
from web.dashboard import TradingDashboard
from core.data_provider import DataProvider
data_provider = DataProvider()
dashboard = TradingDashboard(data_provider)
logger.info("Using minimal fallback dashboard")
dashboard.run(host='127.0.0.1', port=8050, debug=False)
except Exception as fallback_error:
logger.error(f"Fallback dashboard failed: {fallback_error}")
logger.error(f"Fatal error: {e}")
import traceback
logger.error(traceback.format_exc())
async def main():
"""Main entry point with streamlined mode selection"""
parser = argparse.ArgumentParser(description='Streamlined Trading System - Integrated Pipeline')
parser.add_argument('--mode',
choices=['test', 'web'],
default='web',
help='Operation mode: test (system check) or web (live trading)')
parser.add_argument('--symbol', type=str, default='ETH/USDT',
help='Primary trading symbol (default: ETH/USDT)')
parser.add_argument('--port', type=int, default=8050,
help='Web dashboard port (default: 8050)')
parser.add_argument('--debug', action='store_true',
help='Enable debug mode')
args = parser.parse_args()
# Setup logging
setup_logging()
try:
logger.info("=" * 70)
logger.info("STREAMLINED TRADING SYSTEM - INTEGRATED PIPELINE")
logger.info(f"Mode: {args.mode.upper()}")
logger.info(f"Primary Symbol: {args.symbol}")
if args.mode == 'web':
logger.info("Integrated Flow: Data -> Indicators -> CNN -> RL -> Execution")
logger.info("2-Action System: BUY/SELL with intelligent position management")
logger.info("=" * 70)
# Route to appropriate mode
if args.mode == 'test':
run_data_test()
elif args.mode == 'web':
run_web_dashboard()
logger.info("[SUCCESS] Operation completed successfully!")
except KeyboardInterrupt:
logger.info("System shutdown requested by user")
except Exception as e:
logger.error(f"Fatal error: {e}")
import traceback
logger.error(traceback.format_exc())
return 1
return 0
if __name__ == "__main__":
sys.exit(asyncio.run(main()))