integratoin fixes - COB and CNN

This commit is contained in:
Dobromir Popov
2025-07-23 17:33:43 +03:00
parent f1d63f9da6
commit 2a0f8f5199
8 changed files with 883 additions and 230 deletions

View File

@ -1453,17 +1453,37 @@ class DataProvider:
async def _on_enhanced_cob_data(self, symbol: str, cob_data: Dict):
"""Handle COB data from Enhanced WebSocket"""
try:
# Ensure cob_websocket_data is initialized
if not hasattr(self, 'cob_websocket_data'):
self.cob_websocket_data = {}
# Store the latest COB data
self.cob_websocket_data[symbol] = cob_data
# Ensure cob_data_cache is initialized
if not hasattr(self, 'cob_data_cache'):
self.cob_data_cache = {}
# Update COB data cache for distribution
binance_symbol = symbol.replace('/', '').upper()
if binance_symbol in self.cob_data_cache:
self.cob_data_cache[binance_symbol].append({
'timestamp': cob_data.get('timestamp', datetime.now()),
'data': cob_data,
'source': 'enhanced_websocket'
})
if binance_symbol not in self.cob_data_cache or self.cob_data_cache[binance_symbol] is None:
from collections import deque
self.cob_data_cache[binance_symbol] = deque(maxlen=300)
# Ensure the deque is properly initialized
if not isinstance(self.cob_data_cache[binance_symbol], deque):
from collections import deque
self.cob_data_cache[binance_symbol] = deque(maxlen=300)
self.cob_data_cache[binance_symbol].append({
'timestamp': cob_data.get('timestamp', datetime.now()),
'data': cob_data,
'source': 'enhanced_websocket'
})
# Ensure cob_data_callbacks is initialized
if not hasattr(self, 'cob_data_callbacks'):
self.cob_data_callbacks = []
# Distribute to COB data callbacks
for callback in self.cob_data_callbacks:
@ -1472,6 +1492,13 @@ class DataProvider:
except Exception as e:
logger.error(f"Error in COB data callback: {e}")
# Ensure distribution_stats is initialized
if not hasattr(self, 'distribution_stats'):
self.distribution_stats = {
'total_ticks_received': 0,
'last_tick_time': {}
}
# Update distribution stats
self.distribution_stats['total_ticks_received'] += 1
self.distribution_stats['last_tick_time'][symbol] = datetime.now()
@ -1479,7 +1506,7 @@ class DataProvider:
logger.debug(f"Enhanced COB data received for {symbol}: {len(cob_data.get('bids', []))} bids, {len(cob_data.get('asks', []))} asks")
except Exception as e:
logger.error(f"Error handling enhanced COB data for {symbol}: {e}")
logger.error(f"Error handling enhanced COB data for {symbol}: {e}", exc_info=True)
async def _on_websocket_status_update(self, status_data: Dict):
"""Handle WebSocket status updates"""
@ -1488,12 +1515,16 @@ class DataProvider:
status = status_data.get('status')
message = status_data.get('message', '')
# Ensure cob_websocket_status is initialized
if not hasattr(self, 'cob_websocket_status'):
self.cob_websocket_status = {}
if symbol:
self.cob_websocket_status[symbol] = status
logger.info(f"🔌 Enhanced WebSocket status for {symbol}: {status} - {message}")
except Exception as e:
logger.error(f"Error handling WebSocket status update: {e}")
logger.error(f"Error handling WebSocket status update: {e}", exc_info=True)
async def _start_fallback_websocket_streaming(self):
"""Fallback to old WebSocket method if Enhanced COB WebSocket fails"""
@ -3499,9 +3530,14 @@ class DataProvider:
# Convert datetime to timestamp
cob_data['timestamp'] = cob_data['timestamp'].timestamp()
# Store raw tick
# Store raw tick - ensure proper initialization
if not hasattr(self, 'cob_raw_ticks'):
self.cob_raw_ticks = {'ETH/USDT': [], 'BTC/USDT': []}
self.cob_raw_ticks = {}
# Ensure symbol keys exist in the dictionary
for sym in ['ETH/USDT', 'BTC/USDT']:
if sym not in self.cob_raw_ticks:
self.cob_raw_ticks[sym] = []
# Add to raw ticks with size limit (keep last 10 seconds of data)
max_ticks = 1000 # ~10 seconds at 100 updates/sec
@ -3514,6 +3550,7 @@ class DataProvider:
if not hasattr(self, 'cob_data_cache'):
self.cob_data_cache = {}
# Ensure symbol key exists in the cache
if symbol not in self.cob_data_cache:
self.cob_data_cache[symbol] = []
@ -3537,7 +3574,7 @@ class DataProvider:
logger.debug(f"Processed WebSocket COB tick for {symbol}: {len(cob_data.get('bids', []))} bids, {len(cob_data.get('asks', []))} asks")
except Exception as e:
logger.error(f"Error processing WebSocket COB data for {symbol}: {e}")
logger.error(f"Error processing WebSocket COB data for {symbol}: {e}", exc_info=True)
def _on_cob_websocket_status(self, status_data: dict):
"""Handle WebSocket status updates"""