This commit is contained in:
Dobromir Popov
2025-06-24 19:21:47 +03:00
parent 165b3be21a
commit 8a51ef8b8c
3 changed files with 234 additions and 35 deletions

View File

@ -171,19 +171,23 @@ class EnhancedTradingOrchestrator(TradingOrchestrator):
self.symbol_correlation_matrix = {} # Pre-computed correlations
# Initialize COB Integration for real-time market microstructure
# COMMENTED OUT: Causes async runtime error during sync initialization
# self.cob_integration = COBIntegration(
# data_provider=self.data_provider,
# symbols=self.symbols
# )
# # Register COB callbacks for CNN and RL models
# self.cob_integration.add_cnn_callback(self._on_cob_cnn_features)
# self.cob_integration.add_dqn_callback(self._on_cob_dqn_state)
# FIXED: Defer COB integration until async context is available
self.cob_integration = None
self.cob_integration_active = False
self._cob_integration_failed = False
# PROPERLY INITIALIZED: Create the COB integration instance synchronously
try:
self.cob_integration = COBIntegration(
data_provider=self.data_provider,
symbols=self.symbols
)
# Register COB callbacks for CNN and RL models
self.cob_integration.add_cnn_callback(self._on_cob_cnn_features)
self.cob_integration.add_dqn_callback(self._on_cob_dqn_state)
self.cob_integration_active = False # Will be set to True when started
self._cob_integration_failed = False
logger.info("COB Integration: Successfully initialized")
except Exception as e:
logger.warning(f"COB Integration: Failed to initialize - {e}")
self.cob_integration = None
self.cob_integration_active = False
self._cob_integration_failed = True
# COB feature storage for model integration
self.latest_cob_features: Dict[str, np.ndarray] = {}
@ -2897,9 +2901,11 @@ class EnhancedTradingOrchestrator(TradingOrchestrator):
logger.info("Starting COB integration for real-time market microstructure...")
await self.cob_integration.start()
self.cob_integration_active = True
logger.info("COB integration started successfully")
except Exception as e:
logger.error(f"Error starting COB integration: {e}")
self.cob_integration_active = False
async def stop_cob_integration(self):
"""Stop COB integration"""

View File

@ -304,7 +304,7 @@ class RealTimeTickProcessor:
if len(self.processing_times) % 100 == 0:
avg_time = np.mean(list(self.processing_times))
logger.info(f"Average processing time: {avg_time:.2f}ms")
logger.debug(f"RTP: Average processing time: {avg_time:.2f}ms")
# Small sleep to prevent CPU overload
time.sleep(0.001) # 1ms sleep for ultra-low latency

View File

@ -878,31 +878,52 @@ class TradingDashboard:
], className="card", style={"width": "28%", "marginLeft": "2%"}),
], className="row g-2 mb-3"),
# CNN Model Monitoring Section
# CNN Model Monitoring & COB Integration - MERGED into 1 row with 4 columns
html.Div([
# CNN Status Column
html.Div([
html.Div([
html.H6([
html.I(className="fas fa-brain me-2"),
"CNN Model Analysis & Predictions"
"CNN Model Analysis"
], className="card-title mb-2"),
html.Div(id="cnn-monitoring-content", style={"height": "350px", "overflowY": "auto"})
html.Div(id="cnn-monitoring-content", style={"height": "280px", "overflowY": "auto"})
], className="card-body p-2")
], className="card")
], className="mb-3"),
], className="card", style={"width": "23%"}),
# COB (Consolidated Order Book) Visualization Section
html.Div([
# COB Status Column
html.Div([
html.Div([
html.H6([
html.I(className="fas fa-layer-group me-2"),
"Consolidated Order Book (COB) - Real-time Market Microstructure"
"COB → Training Pipeline"
], className="card-title mb-2"),
html.Div(id="cob-visualization-content", style={"height": "400px", "overflowY": "auto"})
html.Div(id="cob-status-content", style={"height": "280px", "overflowY": "auto"})
], className="card-body p-2")
], className="card")
], className="mb-3"),
], className="card", style={"width": "23%", "marginLeft": "2%"}),
# ETH/USDT COB Details Column
html.Div([
html.Div([
html.H6([
html.I(className="fas fa-ethereum me-2", style={"color": "#627EEA"}),
"ETH/USDT - COB"
], className="card-title mb-2"),
html.Div(id="eth-cob-content", style={"height": "280px", "overflowY": "auto"})
], className="card-body p-2")
], className="card", style={"width": "23%", "marginLeft": "2%"}),
# BTC/USDT COB Details Column
html.Div([
html.Div([
html.H6([
html.I(className="fas fa-bitcoin me-2", style={"color": "#F7931A"}),
"BTC/USDT - COB"
], className="card-title mb-2"),
html.Div(id="btc-cob-content", style={"height": "280px", "overflowY": "auto"})
], className="card-body p-2")
], className="card", style={"width": "23%", "marginLeft": "2%"}),
], className="d-flex mb-3"),
# Bottom row - Session performance and system status
html.Div([
@ -1026,7 +1047,9 @@ class TradingDashboard:
Output('current-leverage', 'children'),
Output('leverage-risk', 'children'),
Output('cnn-monitoring-content', 'children'),
Output('cob-visualization-content', 'children')
Output('cob-status-content', 'children'),
Output('eth-cob-content', 'children'),
Output('btc-cob-content', 'children')
],
[Input('interval-component', 'n_intervals')]
)
@ -1125,7 +1148,7 @@ class TradingDashboard:
trade_count_text = f"{len(self.session_trades)}"
portfolio_text = f"${portfolio_value:,.2f}"
# OPTIMIZED POSITION INFO
# OPTIMIZED POSITION INFO with separate colors for position and P&L
if self.current_position:
pos_side = self.current_position['side']
pos_size = self.current_position['size']
@ -1133,9 +1156,18 @@ class TradingDashboard:
side_icon = "[LONG]" if pos_side == 'LONG' else "[SHORT]"
side_color = "success" if pos_side == 'LONG' else "danger"
pnl_color = "success" if unrealized_pnl > 0 else "danger"
pnl_sign = "+" if unrealized_pnl > 0 else ""
position_text = f"{side_icon} {pos_size} @ ${pos_price:.2f} | P&L: {pnl_sign}${unrealized_pnl:.2f}"
position_class = f"text-{side_color} fw-bold mb-0 small"
# Create position text with separate colors for position type and P&L
from dash import html
position_text = [
html.Span(f"{side_icon} {pos_size} @ ${pos_price:.2f} | P&L: ",
className=f"text-{side_color}"),
html.Span(f"{pnl_sign}${unrealized_pnl:.2f}",
className=f"text-{pnl_color}")
]
position_class = "fw-bold mb-0 small"
else:
position_text = "No Position"
position_class = "text-muted mb-0 small"
@ -1234,12 +1266,16 @@ class TradingDashboard:
else:
risk_level = "Extreme Risk"
# Generate COB visualization content
# Generate COB 4-column content
try:
cob_content = self._create_cob_visualization_content()
cob_status_content = self._create_cob_status_content()
eth_cob_content = self._create_symbol_cob_content('ETH/USDT')
btc_cob_content = self._create_symbol_cob_content('BTC/USDT')
except Exception as e:
logger.warning(f"COB visualization error: {e}")
cob_content = [html.P("COB data loading...", className="text-muted")]
logger.warning(f"COB content error: {e}")
cob_status_content = [html.P("COB data loading...", className="text-muted")]
eth_cob_content = [html.P("ETH COB loading...", className="text-muted")]
btc_cob_content = [html.P("BTC COB loading...", className="text-muted")]
# BUILD FINAL RESULT
result = (
@ -1247,7 +1283,7 @@ class TradingDashboard:
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, cob_content
cnn_monitoring_content, cob_status_content, eth_cob_content, btc_cob_content
)
# Cache the result for emergencies
@ -6140,6 +6176,163 @@ class TradingDashboard:
except:
return [html.P("CNN monitoring unavailable", className="text-muted")]
def _create_cob_status_content(self) -> List:
"""Create COB status and training pipeline 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([
html.I(className="fas fa-exclamation-triangle text-warning me-2"),
"COB integration not available"
], className="small"))
content.append(html.P("Using basic orchestrator", className="text-muted small"))
return content
# COB Integration Status
if self.orchestrator.cob_integration:
content.append(html.P([
html.I(className="fas fa-check-circle text-success me-2"),
"COB integration active"
], className="small"))
else:
content.append(html.P([
html.I(className="fas fa-exclamation-triangle text-warning me-2"),
"COB integration inactive"
], className="small"))
# Training Pipeline Status
if hasattr(self.orchestrator, 'enhanced_rl_training') and self.orchestrator.enhanced_rl_training:
content.append(html.P([
html.I(className="fas fa-brain text-info me-2"),
"COB → RL pipeline enabled"
], className="small"))
content.append(html.P([
html.I(className="fas fa-arrow-right text-secondary me-1"),
"Real-time market microstructure"
], className="small"))
content.append(html.P([
html.I(className="fas fa-arrow-right text-secondary me-1"),
"CNN features generation"
], className="small"))
content.append(html.P([
html.I(className="fas fa-arrow-right text-secondary me-1"),
"RL state building"
], className="small"))
else:
content.append(html.P([
html.I(className="fas fa-times-circle text-danger me-2"),
"Training pipeline inactive"
], className="small"))
# Performance metrics
cob_update_count = 0
for symbol in ['ETH/USDT', 'BTC/USDT']:
if symbol in getattr(self.orchestrator, 'latest_cob_features', {}):
cob_update_count += 1
content.append(html.Hr())
content.append(html.P([
html.Strong("Active Symbols: "),
f"{cob_update_count}/2"
], className="text-info small"))
return content
except Exception as e:
logger.error(f"Error creating COB status content: {e}")
return [html.P(f"COB status error: {str(e)}", className="text-danger")]
def _create_symbol_cob_content(self, symbol: str) -> List:
"""Create COB content for a specific symbol"""
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", className="text-warning small"))
return content
# 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)
# CNN Features Status
if cob_features is not None:
content.append(html.P([
html.Strong("CNN Features: "),
html.Span("Available", className="text-success")
], className="small"))
content.append(html.P([
html.Span(f"Shape: {cob_features.shape}", className="text-muted")
], className="small"))
else:
content.append(html.P([
html.Strong("CNN Features: "),
html.Span("Not available", className="text-warning")
], className="small"))
# RL State Status
if cob_state is not None:
content.append(html.P([
html.Strong("RL State: "),
html.Span("Available", className="text-success")
], className="small"))
content.append(html.P([
html.Span(f"Shape: {cob_state.shape}", className="text-muted")
], className="small"))
else:
content.append(html.P([
html.Strong("RL State: "),
html.Span("Not available", className="text-warning")
], className="small"))
# 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
# COB Snapshot Details
if cob_snapshot:
content.append(html.Hr())
content.append(html.P([
html.Strong("Mid Price: "),
f"${cob_snapshot.volume_weighted_mid:.2f}"
], className="text-info small"))
content.append(html.P([
html.Strong("Spread: "),
f"{cob_snapshot.spread_bps:.1f} bps"
], className="text-info small"))
content.append(html.P([
html.Strong("Bid Liquidity: "),
f"${cob_snapshot.total_bid_liquidity:,.0f}"
], className="text-success small"))
content.append(html.P([
html.Strong("Ask Liquidity: "),
f"${cob_snapshot.total_ask_liquidity:,.0f}"
], className="text-success small"))
content.append(html.P([
html.Strong("Exchanges: "),
", ".join(cob_snapshot.exchanges_active)
], className="text-secondary small"))
content.append(html.P([
html.Strong("Levels: "),
f"{len(cob_snapshot.consolidated_bids)} bids, {len(cob_snapshot.consolidated_asks)} asks"
], className="text-secondary small"))
else:
content.append(html.Hr())
content.append(html.P("COB snapshot not available", className="text-muted small"))
return content
except Exception as e:
logger.error(f"Error creating COB content for {symbol}: {e}")
return [html.P(f"COB error: {str(e)}", className="text-danger")]
def _create_cob_visualization_content(self) -> List:
"""Create COB (Consolidated Order Book) visualization content"""
try: