COB fixes

This commit is contained in:
Dobromir Popov
2025-08-08 19:15:07 +03:00
parent 0e469c8e2f
commit be1753c96a
5 changed files with 875 additions and 31 deletions

View File

@ -134,9 +134,8 @@ class EnhancedCOBWebSocket:
self.first_event_u: Dict[str, int] = {} # Track first event U for synchronization
self.snapshot_in_progress: Dict[str, bool] = {} # Track snapshot initialization
# Rate limiting for message processing (Binance: max 5 messages per second)
# Message tracking (no artificial throttling; rely on Binance stream pacing)
self.last_message_time: Dict[str, datetime] = {}
self.min_message_interval = 0.2 # 200ms = 5 messages per second compliance
self.message_count: Dict[str, int] = {}
self.message_window_start: Dict[str, datetime] = {}
@ -150,7 +149,8 @@ class EnhancedCOBWebSocket:
# Configuration
self.max_depth = 1000 # Maximum depth for order book
self.update_speed = '1000ms' # Binance update speed - reduced for stability
# Prefer high-frequency depth stream. Binance supports @100ms diff depth
self.update_speed = '100ms'
# Timezone configuration
if self.timezone_offset == '+08:00':
@ -439,7 +439,7 @@ class EnhancedCOBWebSocket:
logger.info("Using UTC timezone for kline stream")
streams = [
f"{ws_symbol}@depth@1000ms", # Order book depth
f"{ws_symbol}@depth@{self.update_speed}", # Order book diff depth
kline_stream, # 1-second candlesticks (with timezone)
f"{ws_symbol}@ticker", # 24hr ticker with volume
f"{ws_symbol}@aggTrade" # Aggregated trades
@ -487,23 +487,14 @@ class EnhancedCOBWebSocket:
# Handle ping frames (though websockets library handles this automatically)
continue
# Rate limiting: Binance allows max 5 messages per second
now = datetime.now()
# Initialize rate limiting tracking
# Track receive rate for monitoring only
if symbol not in self.message_window_start:
self.message_window_start[symbol] = now
self.message_count[symbol] = 0
# Reset counter every second
if (now - self.message_window_start[symbol]).total_seconds() >= 1.0:
self.message_window_start[symbol] = now
self.message_count[symbol] = 0
# Check rate limit (5 messages per second)
if self.message_count[symbol] >= 5:
continue # Skip this message to comply with rate limit
self.message_count[symbol] += 1
self.last_message_time[symbol] = now