fix dash interval

This commit is contained in:
Dobromir Popov
2025-06-19 02:44:00 +03:00
parent f9310c880d
commit 7b4fba3b4c

View File

@ -758,10 +758,10 @@ class TradingDashboard:
className="text-light mb-0 opacity-75 small") className="text-light mb-0 opacity-75 small")
], className="bg-dark p-2 mb-2"), ], className="bg-dark p-2 mb-2"),
# Auto-refresh component - optimized for sub-1s responsiveness # Auto-refresh component - optimized for efficient updates
dcc.Interval( dcc.Interval(
id='interval-component', id='interval-component',
interval=300, # Update every 300ms for real-time trading interval=10000, # Update every 10 seconds for efficiency
n_intervals=0 n_intervals=0
), ),
@ -1001,11 +1001,14 @@ class TradingDashboard:
start_time = time.time() # Performance monitoring start_time = time.time() # Performance monitoring
try: try:
# Periodic cleanup to prevent memory leaks # 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() self._cleanup_old_data()
# Lightweight update every 10 intervals to reduce load # Send POST request with dashboard status every 10 seconds
is_lightweight_update = (n_intervals % 10 != 0) 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 # Get current prices with improved fallback handling
symbol = self.config.symbols[0] if self.config.symbols else "ETH/USDT" symbol = self.config.symbols[0] if self.config.symbols else "ETH/USDT"
current_price = None current_price = None
@ -5978,6 +5981,62 @@ class TradingDashboard:
logger.error(f"Error building basic RL state for {symbol}: {e}") logger.error(f"Error building basic RL state for {symbol}: {e}")
return None 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: def create_dashboard(data_provider: DataProvider = None, orchestrator: TradingOrchestrator = None, trading_executor: TradingExecutor = None) -> TradingDashboard:
"""Factory function to create a trading dashboard""" """Factory function to create a trading dashboard"""
return TradingDashboard(data_provider=data_provider, orchestrator=orchestrator, trading_executor=trading_executor) return TradingDashboard(data_provider=data_provider, orchestrator=orchestrator, trading_executor=trading_executor)