From 1442e2810134bf5a61839b9469bef3d1b7d28765 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Wed, 2 Jul 2025 00:31:26 +0300 Subject: [PATCH] fix cob imba history --- web/clean_dashboard.py | 43 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/web/clean_dashboard.py b/web/clean_dashboard.py index e903f48..adb5b0d 100644 --- a/web/clean_dashboard.py +++ b/web/clean_dashboard.py @@ -1307,13 +1307,23 @@ class CleanTradingDashboard: return [] def _add_signals_to_mini_chart(self, fig: go.Figure, symbol: str, ws_data_1s: pd.DataFrame, row: int = 2): - """Add ALL signals (executed and non-executed) to the 1s mini chart - FIXED PERSISTENCE""" + """Add signals to the 1s mini chart - LIMITED TO PRICE DATA TIME RANGE""" try: - if not self.recent_decisions: + if not self.recent_decisions or ws_data_1s is None or ws_data_1s.empty: return - # Show ALL signals on the mini chart - EXTEND HISTORY for better visibility - all_signals = self.recent_decisions[-200:] # Last 200 signals (increased from 100) + # Get the time range of the price data + try: + price_start_time = pd.to_datetime(ws_data_1s.index.min()) + price_end_time = pd.to_datetime(ws_data_1s.index.max()) + except Exception: + # Fallback if index is not datetime + logger.debug(f"[MINI-CHART] Could not parse datetime index, skipping signal filtering") + price_start_time = None + price_end_time = None + + # Filter signals to only show those within the price data time range + all_signals = self.recent_decisions[-200:] # Last 200 signals buy_signals = [] sell_signals = [] @@ -1356,6 +1366,11 @@ class CleanTradingDashboard: if not signal_time: continue + # FILTER: Only show signals within the price data time range + if price_start_time is not None and price_end_time is not None: + if signal_time < price_start_time or signal_time > price_end_time: + continue + # Get signal attributes with safe defaults signal_price = self._get_signal_attribute(signal, 'price', 0) signal_action = self._get_signal_attribute(signal, 'action', 'HOLD') @@ -1593,7 +1608,7 @@ class CleanTradingDashboard: if total_signals > 0: manual_count = len([s for s in buy_signals + sell_signals if s.get('manual', False)]) ml_count = len([s for s in buy_signals + sell_signals if not s.get('manual', False) and s['executed']]) - logger.debug(f"[MINI-CHART] Added {total_signals} signals: {len(buy_signals)} BUY, {len(sell_signals)} SELL ({manual_count} manual, {ml_count} ML)") + logger.debug(f"[MINI-CHART] Added {total_signals} signals within price range {price_start_time} to {price_end_time}: {len(buy_signals)} BUY, {len(sell_signals)} SELL ({manual_count} manual, {ml_count} ML)") except Exception as e: logger.warning(f"Error adding signals to mini chart: {e}") @@ -2956,6 +2971,10 @@ class CleanTradingDashboard: market_state['minute_of_hour'] = now.minute market_state['day_of_week'] = now.weekday() + # Add cumulative imbalance features + cumulative_imbalance = self._calculate_cumulative_imbalance(symbol) + market_state.update(cumulative_imbalance) + return market_state except Exception as e: @@ -3142,6 +3161,10 @@ class CleanTradingDashboard: 'update_frequency_estimate': self._estimate_cob_update_frequency(symbol) } + # 5. Cumulative imbalance data for model training + cumulative_imbalance = self._calculate_cumulative_imbalance(symbol) + cob_snapshot['cumulative_imbalance'] = cumulative_imbalance + # 5. Cross-symbol reference (BTC for ETH models) if symbol == 'ETH/USDT': btc_reference = self._get_btc_reference_for_eth_training() @@ -3811,9 +3834,9 @@ class CleanTradingDashboard: for name, duration in periods.items(): recent_imbalances = [] for snap in history: - # Check if snap is a valid object with timestamp and stats - if hasattr(snap, 'timestamp') and (now - snap.timestamp <= duration) and hasattr(snap, 'stats') and snap.stats: - imbalance = snap.stats.get('imbalance') + # Check if snap is a valid dict with timestamp and stats + if isinstance(snap, dict) and 'timestamp' in snap and (now - snap['timestamp'] <= duration) and 'stats' in snap and snap['stats']: + imbalance = snap['stats'].get('imbalance') if imbalance is not None: recent_imbalances.append(imbalance) @@ -3822,6 +3845,10 @@ class CleanTradingDashboard: else: stats[name] = 0.0 + # Debug logging to verify cumulative imbalance calculation + if any(value != 0.0 for value in stats.values()): + logger.debug(f"[CUMULATIVE-IMBALANCE] {symbol}: {stats}") + return stats def _connect_to_orchestrator(self):