280 lines
12 KiB
Python
280 lines
12 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Streamlined Trading System - Web Dashboard + Training
|
|
|
|
Integrated system with both training loop and web dashboard:
|
|
- Training Pipeline: Data -> COB -> Indicators -> CNN -> RL -> Orchestrator -> Execution
|
|
- Web Dashboard: Real-time monitoring and control interface
|
|
- 2-Action System: BUY/SELL with intelligent position management
|
|
- Always invested approach with smart risk/reward setup detection
|
|
|
|
Usage:
|
|
python main.py [--symbol ETH/USDT] [--port 8050]
|
|
"""
|
|
|
|
import os
|
|
# Fix OpenMP library conflicts before importing other modules
|
|
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'
|
|
os.environ['OMP_NUM_THREADS'] = '4'
|
|
|
|
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__)
|
|
|
|
async def run_web_dashboard():
|
|
"""Run the streamlined web dashboard with 2-action system and always-invested approach"""
|
|
try:
|
|
logger.info("Starting Streamlined Trading Dashboard...")
|
|
logger.info("2-Action System: BUY/SELL with intelligent position management")
|
|
logger.info("Always Invested Approach: Smart risk/reward setup detection")
|
|
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 models import get_model_registry
|
|
model_registry = {} # Use simple dict for now
|
|
logger.info("[MODELS] Model registry initialized for training")
|
|
except ImportError:
|
|
model_registry = {}
|
|
logger.warning("Model registry not available, using empty registry")
|
|
|
|
# Create streamlined orchestrator with 2-action system and always-invested approach
|
|
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")
|
|
logger.info("Always Invested: Learning to spot high risk/reward setups")
|
|
|
|
# Start COB integration for real-time market microstructure
|
|
try:
|
|
# Create and start COB integration task
|
|
cob_task = asyncio.create_task(orchestrator.start_cob_integration())
|
|
logger.info("COB Integration startup task created")
|
|
except Exception as e:
|
|
logger.warning(f"COB Integration startup failed (will retry): {e}")
|
|
|
|
# Create trading executor for live execution
|
|
trading_executor = TradingExecutor()
|
|
|
|
# Start the training and monitoring loop
|
|
logger.info(f"Starting Enhanced Training Pipeline")
|
|
logger.info("Live Data Processing: ENABLED")
|
|
logger.info("COB Integration: ENABLED (Real-time market microstructure)")
|
|
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("Always Invested: Different thresholds for entry/exit")
|
|
logger.info("Pipeline: Data -> COB -> Indicators -> CNN -> RL -> Orchestrator -> Execution")
|
|
logger.info("Starting training loop...")
|
|
|
|
# Start the training loop
|
|
await start_training_loop(orchestrator, trading_executor)
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error in streamlined dashboard: {e}")
|
|
logger.error("Training stopped")
|
|
import traceback
|
|
logger.error(traceback.format_exc())
|
|
|
|
def start_web_ui():
|
|
"""Start the main TradingDashboard UI in a separate thread"""
|
|
try:
|
|
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 with COB integration
|
|
from web.dashboard import TradingDashboard
|
|
from core.data_provider import DataProvider
|
|
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()
|
|
|
|
# Load model registry for enhanced features
|
|
try:
|
|
from models import get_model_registry
|
|
model_registry = {} # Use simple dict for now
|
|
except ImportError:
|
|
model_registry = {}
|
|
|
|
# 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
|
|
)
|
|
|
|
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("Enhanced TradingDashboard created successfully")
|
|
logger.info("Features: Live trading, COB visualization, RL training monitoring, Position management")
|
|
|
|
# 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:
|
|
logger.error(f"Error starting main trading dashboard UI: {e}")
|
|
import traceback
|
|
logger.error(traceback.format_exc())
|
|
|
|
async def start_training_loop(orchestrator, trading_executor):
|
|
"""Start the main training and monitoring loop"""
|
|
logger.info("=" * 70)
|
|
logger.info("STARTING ENHANCED TRAINING LOOP WITH COB INTEGRATION")
|
|
logger.info("=" * 70)
|
|
|
|
try:
|
|
# Start real-time processing
|
|
await orchestrator.start_realtime_processing()
|
|
|
|
# Main training loop
|
|
iteration = 0
|
|
while True:
|
|
iteration += 1
|
|
|
|
logger.info(f"Training iteration {iteration}")
|
|
|
|
# Make coordinated decisions (this triggers CNN and RL training)
|
|
decisions = await orchestrator.make_coordinated_decisions()
|
|
|
|
# Log decisions and performance
|
|
for symbol, decision in decisions.items():
|
|
if decision:
|
|
logger.info(f"{symbol}: {decision.action} (confidence: {decision.confidence:.3f})")
|
|
|
|
# Execute if confidence is high enough
|
|
if decision.confidence > 0.7:
|
|
logger.info(f"Executing {symbol}: {decision.action}")
|
|
# trading_executor.execute_action(decision)
|
|
|
|
# Log performance metrics every 10 iterations
|
|
if iteration % 10 == 0:
|
|
metrics = orchestrator.get_performance_metrics()
|
|
logger.info(f"Performance metrics: {metrics}")
|
|
|
|
# Log COB integration status
|
|
for symbol in orchestrator.symbols:
|
|
cob_features = orchestrator.latest_cob_features.get(symbol)
|
|
cob_state = orchestrator.latest_cob_state.get(symbol)
|
|
if cob_features is not None:
|
|
logger.info(f"{symbol} COB: CNN features {cob_features.shape}, DQN state {cob_state.shape if cob_state is not None else 'None'}")
|
|
|
|
# Sleep between iterations
|
|
await asyncio.sleep(5) # 5 second intervals
|
|
|
|
except KeyboardInterrupt:
|
|
logger.info("Training interrupted by user")
|
|
except Exception as e:
|
|
logger.error(f"Error in training loop: {e}")
|
|
import traceback
|
|
logger.error(traceback.format_exc())
|
|
finally:
|
|
await orchestrator.stop_realtime_processing()
|
|
await orchestrator.stop_cob_integration()
|
|
logger.info("Training loop stopped")
|
|
|
|
async def main():
|
|
"""Main entry point with both training loop and web dashboard"""
|
|
parser = argparse.ArgumentParser(description='Streamlined Trading System - Training + Web Dashboard')
|
|
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 - TRAINING + MAIN DASHBOARD")
|
|
logger.info(f"Primary Symbol: {args.symbol}")
|
|
logger.info(f"Training Port: {args.port}")
|
|
logger.info(f"Main Trading Dashboard: http://127.0.0.1:8051")
|
|
logger.info("2-Action System: BUY/SELL with intelligent position management")
|
|
logger.info("Always Invested: Learning to spot high risk/reward setups")
|
|
logger.info("Flow: Data -> COB -> Indicators -> CNN -> RL -> Orchestrator -> Execution")
|
|
logger.info("Main Dashboard: Live trading, RL monitoring, Position management")
|
|
logger.info("=" * 70)
|
|
|
|
# Start main trading dashboard UI in a separate thread
|
|
web_thread = Thread(target=start_web_ui, daemon=True)
|
|
web_thread.start()
|
|
logger.info("Main trading dashboard UI thread started")
|
|
|
|
# Give web UI time to start
|
|
await asyncio.sleep(2)
|
|
|
|
# Run the training loop (this will run indefinitely)
|
|
await 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())) |