binance WS api enhanced
This commit is contained in:
@ -227,8 +227,20 @@ class CleanTradingDashboard:
|
||||
|
||||
# COB data cache - enhanced with price buckets and memory system
|
||||
self.cob_cache: dict = {
|
||||
'ETH/USDT': {'last_update': 0, 'data': None, 'updates_count': 0},
|
||||
'BTC/USDT': {'last_update': 0, 'data': None, 'updates_count': 0}
|
||||
'ETH/USDT': {
|
||||
'last_update': 0,
|
||||
'data': None,
|
||||
'updates_count': 0,
|
||||
'update_times': [],
|
||||
'update_rate': 0.0
|
||||
},
|
||||
'BTC/USDT': {
|
||||
'last_update': 0,
|
||||
'data': None,
|
||||
'updates_count': 0,
|
||||
'update_times': [],
|
||||
'update_rate': 0.0
|
||||
}
|
||||
}
|
||||
self.latest_cob_data: dict = {} # Cache for COB integration data
|
||||
self.cob_predictions: dict = {} # Cache for COB predictions (both ETH and BTC for display)
|
||||
@ -317,20 +329,46 @@ class CleanTradingDashboard:
|
||||
def _on_cob_data_update(self, symbol: str, cob_data: dict):
|
||||
"""Handle COB data updates from data provider"""
|
||||
try:
|
||||
|
||||
# Also update the COB cache for status display
|
||||
if not hasattr(self, 'cob_cache'):
|
||||
self.cob_cache = {}
|
||||
|
||||
if symbol not in self.cob_cache:
|
||||
self.cob_cache[symbol] = {'last_update': 0, 'data': None, 'updates_count': 0}
|
||||
self.cob_cache[symbol] = {
|
||||
'last_update': 0,
|
||||
'data': None,
|
||||
'updates_count': 0,
|
||||
'update_times': [], # Track recent update times for rate calculation
|
||||
'update_rate': 0.0
|
||||
}
|
||||
|
||||
# Update cache
|
||||
current_time = time.time()
|
||||
self.cob_cache[symbol]['data'] = cob_data
|
||||
self.cob_cache[symbol]['last_update'] = time.time()
|
||||
self.cob_cache[symbol]['last_update'] = current_time
|
||||
self.cob_cache[symbol]['updates_count'] += 1
|
||||
self.cob_cache[symbol]['websocket_status'] = 'connected'
|
||||
self.cob_cache[symbol]['source'] = 'data_provider'
|
||||
|
||||
# Track update times for rate calculation (keep last 60 seconds)
|
||||
self.cob_cache[symbol]['update_times'].append(current_time)
|
||||
# Remove updates older than 60 seconds
|
||||
cutoff_time = current_time - 60
|
||||
self.cob_cache[symbol]['update_times'] = [
|
||||
t for t in self.cob_cache[symbol]['update_times'] if t > cutoff_time
|
||||
]
|
||||
|
||||
# Calculate update rate (updates per second)
|
||||
if len(self.cob_cache[symbol]['update_times']) > 1:
|
||||
time_span = current_time - self.cob_cache[symbol]['update_times'][0]
|
||||
if time_span > 0:
|
||||
self.cob_cache[symbol]['update_rate'] = len(self.cob_cache[symbol]['update_times']) / time_span
|
||||
else:
|
||||
self.cob_cache[symbol]['update_rate'] = 0.0
|
||||
else:
|
||||
self.cob_cache[symbol]['update_rate'] = 0.0
|
||||
|
||||
logger.info(f"📊 Updated COB cache for {symbol} from data provider (updates: {self.cob_cache[symbol]['updates_count']})")
|
||||
|
||||
# Continue with existing logic
|
||||
@ -374,7 +412,6 @@ class CleanTradingDashboard:
|
||||
if not hasattr(self, 'cob_last_update'):
|
||||
self.cob_last_update = {}
|
||||
|
||||
import time
|
||||
self.cob_last_update[symbol] = time.time()
|
||||
|
||||
# Update current price from COB data
|
||||
@ -809,21 +846,22 @@ class CleanTradingDashboard:
|
||||
if hasattr(self.trading_executor, 'simulation_mode') and not self.trading_executor.simulation_mode:
|
||||
mexc_status = "LIVE+SYNC" # Indicate live trading with position sync
|
||||
|
||||
# COB WebSocket status
|
||||
# COB WebSocket status with update rate
|
||||
cob_status = self.get_cob_websocket_status()
|
||||
overall_status = cob_status.get('overall_status', 'unknown')
|
||||
warning_message = cob_status.get('warning_message')
|
||||
update_rate = cob_status.get('update_rate', 0.0)
|
||||
|
||||
if overall_status == 'all_connected':
|
||||
cob_status_str = "Connected"
|
||||
cob_status_str = f"Connected ({update_rate:.1f}/s)"
|
||||
elif overall_status == 'partial_fallback':
|
||||
cob_status_str = "Fallback"
|
||||
cob_status_str = f"Fallback ({update_rate:.1f}/s)"
|
||||
elif overall_status == 'degraded':
|
||||
cob_status_str = "Degraded"
|
||||
cob_status_str = f"Degraded ({update_rate:.1f}/s)"
|
||||
elif overall_status == 'unavailable':
|
||||
cob_status_str = "N/A"
|
||||
else:
|
||||
cob_status_str = "Error"
|
||||
cob_status_str = f"Error ({update_rate:.1f}/s)"
|
||||
|
||||
return price_str, session_pnl_str, position_str, trade_str, portfolio_str, multiplier_str, cob_status_str, mexc_status
|
||||
|
||||
@ -6548,13 +6586,38 @@ class CleanTradingDashboard:
|
||||
self.cob_cache = {}
|
||||
|
||||
if symbol not in self.cob_cache:
|
||||
self.cob_cache[symbol] = {'last_update': 0, 'data': None, 'updates_count': 0}
|
||||
self.cob_cache[symbol] = {
|
||||
'last_update': 0,
|
||||
'data': None,
|
||||
'updates_count': 0,
|
||||
'update_times': [], # Track recent update times for rate calculation
|
||||
'update_rate': 0.0
|
||||
}
|
||||
|
||||
# Update cache with orchestrator data
|
||||
current_time = time.time()
|
||||
self.cob_cache[symbol]['data'] = cob_data
|
||||
self.cob_cache[symbol]['last_update'] = time.time()
|
||||
self.cob_cache[symbol]['last_update'] = current_time
|
||||
self.cob_cache[symbol]['updates_count'] += 1
|
||||
|
||||
# Track update times for rate calculation (keep last 60 seconds)
|
||||
self.cob_cache[symbol]['update_times'].append(current_time)
|
||||
# Remove updates older than 60 seconds
|
||||
cutoff_time = current_time - 60
|
||||
self.cob_cache[symbol]['update_times'] = [
|
||||
t for t in self.cob_cache[symbol]['update_times'] if t > cutoff_time
|
||||
]
|
||||
|
||||
# Calculate update rate (updates per second)
|
||||
if len(self.cob_cache[symbol]['update_times']) > 1:
|
||||
time_span = current_time - self.cob_cache[symbol]['update_times'][0]
|
||||
if time_span > 0:
|
||||
self.cob_cache[symbol]['update_rate'] = len(self.cob_cache[symbol]['update_times']) / time_span
|
||||
else:
|
||||
self.cob_cache[symbol]['update_rate'] = 0.0
|
||||
else:
|
||||
self.cob_cache[symbol]['update_rate'] = 0.0
|
||||
|
||||
# Set WebSocket status based on data source
|
||||
if isinstance(cob_data, dict) and 'stats' in cob_data:
|
||||
source = cob_data['stats'].get('source', 'unknown')
|
||||
@ -6591,7 +6654,13 @@ class CleanTradingDashboard:
|
||||
|
||||
# Update COB cache with status
|
||||
if symbol not in self.cob_cache:
|
||||
self.cob_cache[symbol] = {'last_update': 0, 'data': None, 'updates_count': 0}
|
||||
self.cob_cache[symbol] = {
|
||||
'last_update': 0,
|
||||
'data': None,
|
||||
'updates_count': 0,
|
||||
'update_times': [],
|
||||
'update_rate': 0.0
|
||||
}
|
||||
|
||||
self.cob_cache[symbol]['websocket_status'] = status
|
||||
self.cob_cache[symbol]['websocket_message'] = message
|
||||
@ -6687,11 +6756,15 @@ class CleanTradingDashboard:
|
||||
status_summary['overall_status'] = 'error'
|
||||
status_summary['warning_message'] = '❌ COB WebSocket failed - All connections down'
|
||||
|
||||
# Set last update time
|
||||
# Set last update time and calculate overall update rate
|
||||
last_updates = [cache.get('last_update', 0) for cache in self.cob_cache.values()]
|
||||
if last_updates and max(last_updates) > 0:
|
||||
status_summary['last_update'] = datetime.fromtimestamp(max(last_updates)).isoformat()
|
||||
|
||||
# Calculate overall update rate (sum of all symbols)
|
||||
total_update_rate = sum(cache.get('update_rate', 0.0) for cache in self.cob_cache.values())
|
||||
status_summary['update_rate'] = total_update_rate
|
||||
|
||||
return status_summary
|
||||
|
||||
except Exception as e:
|
||||
|
Reference in New Issue
Block a user