From b47805dafca50e1408c217c46c62424f7f857c19 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Wed, 2 Jul 2025 03:31:37 +0300 Subject: [PATCH] cob signas --- core/cob_integration.py | 26 +++++++++++++++-- web/clean_dashboard.py | 60 +++++++++++++++++++++++++++++++++++++++- web/component_manager.py | 11 +++++++- 3 files changed, 92 insertions(+), 5 deletions(-) diff --git a/core/cob_integration.py b/core/cob_integration.py index 47c38fa..df51db7 100644 --- a/core/cob_integration.py +++ b/core/cob_integration.py @@ -476,16 +476,36 @@ class COBIntegration: async def _analyze_cob_patterns(self, symbol: str, cob_snapshot: COBSnapshot): """Analyze COB data for trading patterns and signals""" try: - # Large liquidity imbalance detection - if abs(cob_snapshot.liquidity_imbalance) > 0.4: + # Enhanced liquidity imbalance detection with dynamic thresholds + imbalance = abs(cob_snapshot.liquidity_imbalance) + + # Dynamic threshold based on imbalance strength + if imbalance > 0.8: # Very strong imbalance (>80%) + threshold = 0.05 # 5% threshold for very strong signals + confidence_multiplier = 3.0 + elif imbalance > 0.5: # Strong imbalance (>50%) + threshold = 0.1 # 10% threshold for strong signals + confidence_multiplier = 2.5 + elif imbalance > 0.3: # Moderate imbalance (>30%) + threshold = 0.15 # 15% threshold for moderate signals + confidence_multiplier = 2.0 + else: # Weak imbalance + threshold = 0.2 # 20% threshold for weak signals + confidence_multiplier = 1.5 + + # Generate signal if imbalance exceeds threshold + if abs(cob_snapshot.liquidity_imbalance) > threshold: signal = { 'timestamp': cob_snapshot.timestamp.isoformat(), 'type': 'liquidity_imbalance', 'side': 'buy' if cob_snapshot.liquidity_imbalance > 0 else 'sell', 'strength': abs(cob_snapshot.liquidity_imbalance), - 'confidence': min(1.0, abs(cob_snapshot.liquidity_imbalance) * 2) + 'confidence': min(1.0, abs(cob_snapshot.liquidity_imbalance) * confidence_multiplier), + 'threshold_used': threshold, + 'signal_strength': 'very_strong' if imbalance > 0.8 else 'strong' if imbalance > 0.5 else 'moderate' if imbalance > 0.3 else 'weak' } self.cob_signals[symbol].append(signal) + logger.info(f"COB SIGNAL: {symbol} {signal['side'].upper()} signal generated - imbalance: {cob_snapshot.liquidity_imbalance:.3f}, confidence: {signal['confidence']:.3f}") # Cleanup old signals self.cob_signals[symbol] = self.cob_signals[symbol][-100:] diff --git a/web/clean_dashboard.py b/web/clean_dashboard.py index 0123d1c..e5204ff 100644 --- a/web/clean_dashboard.py +++ b/web/clean_dashboard.py @@ -432,7 +432,7 @@ class CleanTradingDashboard: [Input('interval-component', 'n_intervals')] ) def update_recent_decisions(n): - """Update recent trading signals - FILTER OUT HOLD signals""" + """Update recent trading signals - FILTER OUT HOLD signals and highlight COB signals""" try: # Filter out HOLD signals before displaying filtered_decisions = [] @@ -441,6 +441,11 @@ class CleanTradingDashboard: if action != 'HOLD': filtered_decisions.append(decision) + # Log COB signal activity + cob_signals = [d for d in filtered_decisions if d.get('type') == 'cob_liquidity_imbalance'] + if cob_signals: + logger.info(f"COB signals active: {len(cob_signals)} recent COB signals") + return self.component_manager.format_trading_signals(filtered_decisions) except Exception as e: logger.error(f"Error updating decisions: {e}") @@ -3682,6 +3687,9 @@ class CleanTradingDashboard: # Generate bucketed data for models self._generate_bucketed_cob_data(symbol, cob_snapshot) + # Generate COB signals based on imbalance + self._generate_cob_signal(symbol, cob_snapshot) + logger.debug(f"COB data collected for {symbol}: {len(bids)} bids, {len(asks)} asks") except Exception as e: @@ -3731,6 +3739,56 @@ class CleanTradingDashboard: except Exception as e: logger.debug(f"Error generating bucketed COB data: {e}") + def _generate_cob_signal(self, symbol: str, cob_snapshot: dict): + """Generate COB-based trading signals from imbalance data""" + try: + imbalance = cob_snapshot['stats']['imbalance'] + abs_imbalance = abs(imbalance) + + # Dynamic threshold based on imbalance strength + if abs_imbalance > 0.8: # Very strong imbalance (>80%) + threshold = 0.05 # 5% threshold for very strong signals + confidence_multiplier = 3.0 + elif abs_imbalance > 0.5: # Strong imbalance (>50%) + threshold = 0.1 # 10% threshold for strong signals + confidence_multiplier = 2.5 + elif abs_imbalance > 0.3: # Moderate imbalance (>30%) + threshold = 0.15 # 15% threshold for moderate signals + confidence_multiplier = 2.0 + else: # Weak imbalance + threshold = 0.2 # 20% threshold for weak signals + confidence_multiplier = 1.5 + + # Generate signal if imbalance exceeds threshold + if abs_imbalance > threshold: + signal = { + 'timestamp': datetime.now(), + 'type': 'cob_liquidity_imbalance', + 'action': 'BUY' if imbalance > 0 else 'SELL', + 'symbol': symbol, + 'confidence': min(1.0, abs_imbalance * confidence_multiplier), + 'strength': abs_imbalance, + 'threshold_used': threshold, + 'signal_strength': 'very_strong' if abs_imbalance > 0.8 else 'strong' if abs_imbalance > 0.5 else 'moderate' if abs_imbalance > 0.3 else 'weak', + 'reasoning': f"COB liquidity imbalance: {imbalance:.3f} ({'bid' if imbalance > 0 else 'ask'} heavy)", + 'executed': False, + 'blocked': False, + 'manual': False + } + + # Add to recent decisions + self.recent_decisions.append(signal) + if len(self.recent_decisions) > 200: + self.recent_decisions.pop(0) + + logger.info(f"COB SIGNAL: {symbol} {signal['action']} signal generated - imbalance: {imbalance:.3f}, confidence: {signal['confidence']:.3f}") + + # Process the signal for potential execution + self._process_dashboard_signal(signal) + + except Exception as e: + logger.debug(f"Error generating COB signal for {symbol}: {e}") + def _feed_cob_data_to_models(self, symbol: str, cob_snapshot: dict): """Feed COB data to models for training and inference""" try: diff --git a/web/component_manager.py b/web/component_manager.py index c7b062b..d359478 100644 --- a/web/component_manager.py +++ b/web/component_manager.py @@ -59,10 +59,19 @@ class DashboardComponentManager: action_color = "text-success" if action == "BUY" else "text-danger" manual_indicator = " [M]" if manual else "" + # Highlight COB signals + cob_indicator = "" + if hasattr(decision, 'type') and getattr(decision, 'type', '') == 'cob_liquidity_imbalance': + cob_indicator = " [COB]" + badge_class = "bg-info" # Use blue for COB signals + elif isinstance(decision, dict) and decision.get('type') == 'cob_liquidity_imbalance': + cob_indicator = " [COB]" + badge_class = "bg-info" # Use blue for COB signals + signal_div = html.Div([ html.Span(f"{timestamp}", className="small text-muted me-2"), html.Span(f"{status}", className=f"badge {badge_class} me-2"), - html.Span(f"{action}{manual_indicator}", className=f"{action_color} fw-bold me-2"), + html.Span(f"{action}{manual_indicator}{cob_indicator}", className=f"{action_color} fw-bold me-2"), html.Span(f"({confidence:.1f}%)", className="small text-muted me-2"), html.Span(f"${price:.2f}", className="small") ], className="mb-1")