dash working
This commit is contained in:
@ -2653,6 +2653,7 @@ class DataProvider:
|
||||
# Significantly reduced frequency for REST API fallback only
|
||||
def collect_symbol_data(symbol):
|
||||
rest_api_fallback_count = 0
|
||||
last_rest_api_call = 0 # Track last REST API call time
|
||||
while self.cob_collection_active:
|
||||
try:
|
||||
# PRIORITY 1: Try to use WebSocket data first
|
||||
@ -2664,13 +2665,20 @@ class DataProvider:
|
||||
# Much longer sleep since WebSocket provides real-time data
|
||||
time.sleep(10.0) # Only check every 10 seconds when WS is working
|
||||
else:
|
||||
# FALLBACK: Only use REST API if WebSocket fails
|
||||
# FALLBACK: Only use REST API if WebSocket fails AND rate limit allows
|
||||
rest_api_fallback_count += 1
|
||||
if rest_api_fallback_count <= 3: # Limited fallback attempts
|
||||
logger.warning(f"WebSocket COB data unavailable for {symbol}, using REST API fallback #{rest_api_fallback_count}")
|
||||
self._collect_cob_data_for_symbol(symbol)
|
||||
current_time = time.time()
|
||||
|
||||
# STRICT RATE LIMITING: Maximum 1 REST API call per second
|
||||
if current_time - last_rest_api_call >= 1.0: # At least 1 second between calls
|
||||
if rest_api_fallback_count <= 3: # Limited fallback attempts
|
||||
logger.warning(f"WebSocket COB data unavailable for {symbol}, using REST API fallback #{rest_api_fallback_count}")
|
||||
self._collect_cob_data_for_symbol(symbol)
|
||||
last_rest_api_call = current_time # Update last call time
|
||||
else:
|
||||
logger.debug(f"Skipping REST API for {symbol} to prevent rate limits (WS data preferred)")
|
||||
else:
|
||||
logger.debug(f"Skipping REST API for {symbol} to prevent rate limits (WS data preferred)")
|
||||
logger.debug(f"Rate limiting REST API for {symbol} - waiting {1.0 - (current_time - last_rest_api_call):.1f}s")
|
||||
|
||||
# Much longer sleep when using REST API fallback
|
||||
time.sleep(30.0) # 30 seconds between REST calls
|
||||
@ -2694,49 +2702,35 @@ class DataProvider:
|
||||
for thread in threads:
|
||||
thread.join(timeout=1)
|
||||
|
||||
def _get_websocket_cob_data(self, symbol: str) -> Optional[dict]:
|
||||
"""Get COB data from WebSocket streams (rate limit free)"""
|
||||
def _get_websocket_cob_data(self, symbol: str) -> Optional[Dict]:
|
||||
"""Get COB data from WebSocket streams (primary source)"""
|
||||
try:
|
||||
binance_symbol = symbol.replace('/', '').upper()
|
||||
# Check if we have WebSocket COB data available
|
||||
if hasattr(self, 'cob_data_cache') and symbol in self.cob_data_cache:
|
||||
cached_data = self.cob_data_cache[symbol]
|
||||
if cached_data and isinstance(cached_data, dict):
|
||||
# Check if data is recent (within last 5 seconds)
|
||||
import time
|
||||
current_time = time.time()
|
||||
data_age = current_time - cached_data.get('timestamp', 0)
|
||||
|
||||
if data_age < 5.0: # Data is fresh
|
||||
logger.debug(f"Using WebSocket COB data for {symbol} (age: {data_age:.1f}s)")
|
||||
return cached_data
|
||||
else:
|
||||
logger.debug(f"WebSocket COB data for {symbol} is stale (age: {data_age:.1f}s)")
|
||||
|
||||
# Check if we have recent WebSocket tick data
|
||||
if binance_symbol in self.tick_buffers and len(self.tick_buffers[binance_symbol]) > 10:
|
||||
recent_ticks = list(self.tick_buffers[binance_symbol])[-50:] # Last 50 ticks
|
||||
|
||||
if recent_ticks:
|
||||
# Calculate COB data from WebSocket ticks
|
||||
latest_tick = recent_ticks[-1]
|
||||
|
||||
# Calculate bid/ask liquidity from recent tick patterns
|
||||
buy_volume = sum(tick.volume for tick in recent_ticks if tick.side == 'buy')
|
||||
sell_volume = sum(tick.volume for tick in recent_ticks if tick.side == 'sell')
|
||||
total_volume = buy_volume + sell_volume
|
||||
|
||||
# Calculate metrics
|
||||
imbalance = (buy_volume - sell_volume) / total_volume if total_volume > 0 else 0
|
||||
avg_price = sum(tick.price for tick in recent_ticks) / len(recent_ticks)
|
||||
|
||||
# Create synthetic COB snapshot from WebSocket data
|
||||
cob_snapshot = {
|
||||
'symbol': symbol,
|
||||
'timestamp': datetime.now(),
|
||||
'source': 'websocket', # Mark as WebSocket source
|
||||
'stats': {
|
||||
'mid_price': latest_tick.price,
|
||||
'avg_price': avg_price,
|
||||
'imbalance': imbalance,
|
||||
'buy_volume': buy_volume,
|
||||
'sell_volume': sell_volume,
|
||||
'total_volume': total_volume,
|
||||
'tick_count': len(recent_ticks),
|
||||
'best_bid': latest_tick.price - 0.01, # Approximate
|
||||
'best_ask': latest_tick.price + 0.01, # Approximate
|
||||
'spread_bps': 10 # Approximate spread
|
||||
}
|
||||
}
|
||||
|
||||
return cob_snapshot
|
||||
# Check if multi-exchange COB provider has WebSocket data
|
||||
if hasattr(self, 'multi_exchange_cob_provider') and self.multi_exchange_cob_provider:
|
||||
try:
|
||||
cob_data = self.multi_exchange_cob_provider.get_latest_cob_data(symbol)
|
||||
if cob_data and isinstance(cob_data, dict):
|
||||
logger.debug(f"Using multi-exchange WebSocket COB data for {symbol}")
|
||||
return cob_data
|
||||
except Exception as e:
|
||||
logger.debug(f"Error getting multi-exchange COB data for {symbol}: {e}")
|
||||
|
||||
logger.debug(f"No WebSocket COB data available for {symbol}")
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
|
Reference in New Issue
Block a user