From 7b4fba3b4c608bf5f440c095785e910819354525 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Thu, 19 Jun 2025 02:44:00 +0300 Subject: [PATCH] fix dash interval --- web/dashboard.py | 69 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 5 deletions(-) diff --git a/web/dashboard.py b/web/dashboard.py index 112ed55..f6a594b 100644 --- a/web/dashboard.py +++ b/web/dashboard.py @@ -758,10 +758,10 @@ class TradingDashboard: className="text-light mb-0 opacity-75 small") ], className="bg-dark p-2 mb-2"), - # Auto-refresh component - optimized for sub-1s responsiveness + # Auto-refresh component - optimized for efficient updates dcc.Interval( id='interval-component', - interval=300, # Update every 300ms for real-time trading + interval=10000, # Update every 10 seconds for efficiency n_intervals=0 ), @@ -1001,11 +1001,14 @@ class TradingDashboard: start_time = time.time() # Performance monitoring try: # Periodic cleanup to prevent memory leaks - if n_intervals % 60 == 0: # Every 60 seconds + if n_intervals % 6 == 0: # Every 60 seconds (6 * 10 = 60) self._cleanup_old_data() - # Lightweight update every 10 intervals to reduce load - is_lightweight_update = (n_intervals % 10 != 0) + # Send POST request with dashboard status every 10 seconds + self._send_dashboard_status_update(n_intervals) + + # Remove lightweight update as we're now on 10 second intervals + is_lightweight_update = False # Get current prices with improved fallback handling symbol = self.config.symbols[0] if self.config.symbols else "ETH/USDT" current_price = None @@ -5978,6 +5981,62 @@ class TradingDashboard: logger.error(f"Error building basic RL state for {symbol}: {e}") return None + def _send_dashboard_status_update(self, n_intervals: int): + """Send POST request with dashboard status update every 10 seconds""" + try: + # Get current symbol and price + symbol = self.config.symbols[0] if self.config.symbols else "ETH/USDT" + current_price = self.get_realtime_price(symbol) + + # Calculate current metrics + unrealized_pnl = self._calculate_unrealized_pnl(current_price) if current_price else 0.0 + total_session_pnl = self.total_realized_pnl + unrealized_pnl + portfolio_value = self.starting_balance + total_session_pnl + + # Prepare status data + status_data = { + "timestamp": datetime.now().isoformat(), + "interval_count": n_intervals, + "symbol": symbol, + "current_price": current_price, + "session_pnl": total_session_pnl, + "realized_pnl": self.total_realized_pnl, + "unrealized_pnl": unrealized_pnl, + "total_fees": self.total_fees, + "portfolio_value": portfolio_value, + "leverage": self.leverage_multiplier, + "trade_count": len(self.session_trades), + "position": { + "active": bool(self.current_position), + "side": self.current_position['side'] if self.current_position else None, + "size": self.current_position['size'] if self.current_position else 0.0, + "price": self.current_position['price'] if self.current_position else 0.0 + }, + "recent_signals_count": len(self.recent_signals), + "system_status": "active" + } + + # Send POST request to trading server if available + import requests + response = requests.post( + f"{self.trading_server_url}/dashboard_status", + json=status_data, + timeout=5 + ) + + if response.status_code == 200: + logger.debug(f"[DASHBOARD_POST] Status update sent successfully (interval {n_intervals})") + else: + logger.warning(f"[DASHBOARD_POST] Failed to send status update: {response.status_code}") + + except requests.exceptions.Timeout: + logger.debug("[DASHBOARD_POST] Status update timeout - server may not be available") + except requests.exceptions.ConnectionError: + logger.debug("[DASHBOARD_POST] Status update connection error - server not available") + except Exception as e: + logger.debug(f"[DASHBOARD_POST] Status update error: {e}") + + def create_dashboard(data_provider: DataProvider = None, orchestrator: TradingOrchestrator = None, trading_executor: TradingExecutor = None) -> TradingDashboard: """Factory function to create a trading dashboard""" return TradingDashboard(data_provider=data_provider, orchestrator=orchestrator, trading_executor=trading_executor) \ No newline at end of file