fix cob imba history

This commit is contained in:
Dobromir Popov
2025-07-02 00:31:26 +03:00
parent d269a1fe6e
commit 1442e28101

View File

@ -1307,13 +1307,23 @@ class CleanTradingDashboard:
return [] return []
def _add_signals_to_mini_chart(self, fig: go.Figure, symbol: str, ws_data_1s: pd.DataFrame, row: int = 2): 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: try:
if not self.recent_decisions: if not self.recent_decisions or ws_data_1s is None or ws_data_1s.empty:
return return
# Show ALL signals on the mini chart - EXTEND HISTORY for better visibility # Get the time range of the price data
all_signals = self.recent_decisions[-200:] # Last 200 signals (increased from 100) 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 = [] buy_signals = []
sell_signals = [] sell_signals = []
@ -1356,6 +1366,11 @@ class CleanTradingDashboard:
if not signal_time: if not signal_time:
continue 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 # Get signal attributes with safe defaults
signal_price = self._get_signal_attribute(signal, 'price', 0) signal_price = self._get_signal_attribute(signal, 'price', 0)
signal_action = self._get_signal_attribute(signal, 'action', 'HOLD') signal_action = self._get_signal_attribute(signal, 'action', 'HOLD')
@ -1593,7 +1608,7 @@ class CleanTradingDashboard:
if total_signals > 0: if total_signals > 0:
manual_count = len([s for s in buy_signals + sell_signals if s.get('manual', False)]) 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']]) 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: except Exception as e:
logger.warning(f"Error adding signals to mini chart: {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['minute_of_hour'] = now.minute
market_state['day_of_week'] = now.weekday() 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 return market_state
except Exception as e: except Exception as e:
@ -3142,6 +3161,10 @@ class CleanTradingDashboard:
'update_frequency_estimate': self._estimate_cob_update_frequency(symbol) '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) # 5. Cross-symbol reference (BTC for ETH models)
if symbol == 'ETH/USDT': if symbol == 'ETH/USDT':
btc_reference = self._get_btc_reference_for_eth_training() btc_reference = self._get_btc_reference_for_eth_training()
@ -3811,9 +3834,9 @@ class CleanTradingDashboard:
for name, duration in periods.items(): for name, duration in periods.items():
recent_imbalances = [] recent_imbalances = []
for snap in history: for snap in history:
# Check if snap is a valid object with timestamp and stats # Check if snap is a valid dict with timestamp and stats
if hasattr(snap, 'timestamp') and (now - snap.timestamp <= duration) and hasattr(snap, 'stats') and snap.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') imbalance = snap['stats'].get('imbalance')
if imbalance is not None: if imbalance is not None:
recent_imbalances.append(imbalance) recent_imbalances.append(imbalance)
@ -3822,6 +3845,10 @@ class CleanTradingDashboard:
else: else:
stats[name] = 0.0 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 return stats
def _connect_to_orchestrator(self): def _connect_to_orchestrator(self):