cob integration (wip)
This commit is contained in:
187
main.py
187
main.py
@@ -1,9 +1,10 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Streamlined Trading System - Web Dashboard Only
|
||||
Streamlined Trading System - Web Dashboard + Training
|
||||
|
||||
Simplified entry point with only the web dashboard mode:
|
||||
- Streamlined Flow: Data -> Indicators/Pivots -> CNN -> RL -> Orchestrator -> Execution
|
||||
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
|
||||
|
||||
@@ -11,6 +12,11 @@ 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
|
||||
@@ -28,7 +34,7 @@ from core.data_provider import DataProvider
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def run_web_dashboard():
|
||||
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...")
|
||||
@@ -60,9 +66,9 @@ def run_web_dashboard():
|
||||
|
||||
# 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")
|
||||
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")
|
||||
@@ -77,56 +83,139 @@ def run_web_dashboard():
|
||||
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()
|
||||
|
||||
# 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}")
|
||||
# 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 -> Indicators -> CNN -> RL -> Orchestrator -> Execution")
|
||||
logger.info(f"Dashboard optimized: 300ms updates for sub-1s responsiveness")
|
||||
logger.info("Pipeline: Data -> COB -> Indicators -> CNN -> RL -> Orchestrator -> Execution")
|
||||
logger.info("Starting training loop...")
|
||||
|
||||
dashboard.run(host=host, port=port, debug=False)
|
||||
# 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("Dashboard stopped - trying minimal fallback")
|
||||
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("=" * 50)
|
||||
|
||||
try:
|
||||
# Minimal fallback dashboard
|
||||
from web.dashboard import TradingDashboard
|
||||
from core.data_provider import DataProvider
|
||||
# Import and create the main TradingDashboard (simplified approach)
|
||||
from web.dashboard import TradingDashboard
|
||||
from core.data_provider import DataProvider
|
||||
from core.orchestrator import TradingOrchestrator
|
||||
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)
|
||||
|
||||
trading_executor = TradingExecutor()
|
||||
|
||||
# Create the main trading dashboard
|
||||
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")
|
||||
|
||||
# Run the dashboard server (simplified - no async loop)
|
||||
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
|
||||
|
||||
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())
|
||||
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 streamlined web-only operation"""
|
||||
parser = argparse.ArgumentParser(description='Streamlined Trading System - 2-Action Web Dashboard')
|
||||
"""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,
|
||||
@@ -141,16 +230,26 @@ async def main():
|
||||
|
||||
try:
|
||||
logger.info("=" * 70)
|
||||
logger.info("STREAMLINED TRADING SYSTEM - 2-ACTION WEB DASHBOARD")
|
||||
logger.info("STREAMLINED TRADING SYSTEM - TRAINING + MAIN DASHBOARD")
|
||||
logger.info(f"Primary Symbol: {args.symbol}")
|
||||
logger.info(f"Web Port: {args.port}")
|
||||
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 -> Indicators -> CNN -> RL -> Orchestrator -> Execution")
|
||||
logger.info("Flow: Data -> COB -> Indicators -> CNN -> RL -> Orchestrator -> Execution")
|
||||
logger.info("Main Dashboard: Live trading, RL monitoring, Position management")
|
||||
logger.info("=" * 70)
|
||||
|
||||
# Run the web dashboard
|
||||
run_web_dashboard()
|
||||
# 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!")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user