small ui fixes

This commit is contained in:
Dobromir Popov
2025-06-18 20:17:11 +03:00
parent f1ef2702d7
commit 72b010631a
2 changed files with 298 additions and 15 deletions

View File

@ -62,6 +62,14 @@ class COBDashboardServer:
symbol: deque(maxlen=100) for symbol in self.symbols
}
# OHLCV data for mini charts (5 minutes = 300 1-second candles)
self.ohlcv_data: Dict[str, deque] = {
symbol: deque(maxlen=300) for symbol in self.symbols
}
# Current candle data (building 1-second candles)
self.current_candles: Dict[str, Dict] = {}
# Setup routes and CORS
self._setup_routes()
self._setup_cors()
@ -312,6 +320,9 @@ class COBDashboardServer:
try:
logger.debug(f"Received COB update for {symbol}")
# Process OHLCV data from mid price
await self._process_ohlcv_update(symbol, data)
# Update cache
self.latest_cob_data[symbol] = data
self.update_timestamps[symbol].append(datetime.now())
@ -323,17 +334,84 @@ class COBDashboardServer:
except Exception as e:
logger.error(f"Error handling COB update for {symbol}: {e}")
async def _process_ohlcv_update(self, symbol: str, data: Dict):
"""Process price updates into 1-second OHLCV candles"""
try:
stats = data.get('stats', {})
mid_price = stats.get('mid_price', 0)
if mid_price <= 0:
return
now = datetime.now()
current_second = now.replace(microsecond=0)
# Get or create current candle
if symbol not in self.current_candles:
self.current_candles[symbol] = {
'timestamp': current_second,
'open': mid_price,
'high': mid_price,
'low': mid_price,
'close': mid_price,
'volume': 0, # We don't have volume from order book, use tick count
'tick_count': 1
}
else:
current_candle = self.current_candles[symbol]
# Check if we need to close current candle and start new one
if current_second > current_candle['timestamp']:
# Close previous candle
finished_candle = {
'timestamp': current_candle['timestamp'].isoformat(),
'open': current_candle['open'],
'high': current_candle['high'],
'low': current_candle['low'],
'close': current_candle['close'],
'volume': current_candle['tick_count'], # Use tick count as volume
'tick_count': current_candle['tick_count']
}
# Add to OHLCV history
self.ohlcv_data[symbol].append(finished_candle)
# Start new candle
self.current_candles[symbol] = {
'timestamp': current_second,
'open': mid_price,
'high': mid_price,
'low': mid_price,
'close': mid_price,
'volume': 0,
'tick_count': 1
}
else:
# Update current candle
current_candle['high'] = max(current_candle['high'], mid_price)
current_candle['low'] = min(current_candle['low'], mid_price)
current_candle['close'] = mid_price
current_candle['tick_count'] += 1
except Exception as e:
logger.error(f"Error processing OHLCV update for {symbol}: {e}")
async def _broadcast_cob_update(self, symbol: str, data: Dict):
"""Broadcast COB update to all connected WebSocket clients"""
if not self.websocket_connections:
return
# Add OHLCV data to the broadcast
enhanced_data = data.copy()
if symbol in self.ohlcv_data:
enhanced_data['ohlcv'] = list(self.ohlcv_data[symbol])
message = {
'type': 'cob_update',
'symbol': symbol,
'timestamp': datetime.now().isoformat(),
'data': data
'data': enhanced_data
}
# Send to all connections
@ -382,6 +460,7 @@ class COBDashboardServer:
'bids': [],
'asks': [],
'svp': {'data': []},
'ohlcv': [],
'stats': {
'mid_price': 0,
'spread_bps': 0,