-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket_client.py
More file actions
640 lines (522 loc) · 23.9 KB
/
websocket_client.py
File metadata and controls
640 lines (522 loc) · 23.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#!/usr/bin/env python3
"""
WebSocket Market Data Client
Real-time market data streaming with auto-reconnection and circuit breaker
"""
import asyncio
import json
import websockets
import logging
from typing import Dict, List, Callable, Optional, Any
from dataclasses import dataclass, asdict
from datetime import datetime, timedelta
from enum import Enum
import time
import traceback
class ConnectionState(Enum):
DISCONNECTED = "disconnected"
CONNECTING = "connecting"
CONNECTED = "connected"
RECONNECTING = "reconnecting"
FAILED = "failed"
@dataclass
class MarketData:
"""Market data structure for WebSocket streams"""
symbol: str
timestamp: datetime
price: float
volume: float
bid: float = 0.0
ask: float = 0.0
high: float = 0.0
low: float = 0.0
open: float = 0.0
close: float = 0.0
change: float = 0.0
change_percent: float = 0.0
@dataclass
class OrderBookData:
"""Order book data structure"""
symbol: str
timestamp: datetime
bids: List[List[float]] # [price, quantity]
asks: List[List[float]] # [price, quantity]
last_update_id: int = 0
@dataclass
class TradeData:
"""Individual trade data"""
symbol: str
timestamp: datetime
price: float
quantity: float
is_buyer_maker: bool
trade_id: int
class CircuitBreaker:
"""Circuit breaker for connection failures"""
def __init__(self, failure_threshold: int = 5, recovery_timeout: int = 60):
self.failure_threshold = failure_threshold
self.recovery_timeout = recovery_timeout
self.failure_count = 0
self.last_failure_time = None
self.state = "closed" # closed, open, half_open
def record_success(self):
"""Record successful operation"""
self.failure_count = 0
self.state = "closed"
def record_failure(self):
"""Record failed operation"""
self.failure_count += 1
self.last_failure_time = time.time()
if self.failure_count >= self.failure_threshold:
self.state = "open"
logging.warning(f"Circuit breaker opened after {self.failure_count} failures")
def can_execute(self) -> bool:
"""Check if operation can be executed"""
if self.state == "closed":
return True
if self.state == "open":
if time.time() - self.last_failure_time > self.recovery_timeout:
self.state = "half_open"
logging.info("Circuit breaker moved to half-open state")
return True
return False
# half_open state
return True
class WebSocketClient:
"""WebSocket client for Binance Futures market data"""
def __init__(self, testnet: bool = True):
self.testnet = testnet
# Use the correct WebSocket URLs for Binance Futures
if testnet:
self.base_url = "wss://stream.binancefuture.com"
else:
self.base_url = "wss://fstream.binance.com"
# Use combined stream approach
self.use_combined_stream = True
# Connection management
self.websocket = None
self.connection_state = ConnectionState.DISCONNECTED
self.reconnect_attempts = 0
self.max_reconnect_attempts = 10
self.reconnect_delay = 5 # seconds
self.max_reconnect_delay = 300 # 5 minutes
# Circuit breaker
self.circuit_breaker = CircuitBreaker()
# Subscriptions and callbacks
self.subscriptions: Dict[str, List[Callable]] = {}
self.stream_callbacks: Dict[str, Callable] = {}
# Health monitoring
self.last_ping_time = None
self.ping_interval = 30 # seconds
self.ping_timeout = 10 # seconds
self.last_message_time = None
self.message_timeout = 60 # seconds
# Statistics
self.stats = {
'messages_received': 0,
'connection_attempts': 0,
'successful_connections': 0,
'reconnections': 0,
'errors': 0,
'last_error': None,
'uptime_start': None
}
# Setup logging
self.logger = logging.getLogger(__name__)
async def connect(self) -> bool:
"""Establish WebSocket connection"""
if not self.circuit_breaker.can_execute():
self.logger.warning("Circuit breaker is open, cannot connect")
return False
try:
self.connection_state = ConnectionState.CONNECTING
self.stats['connection_attempts'] += 1
# Build WebSocket URL
if self.use_combined_stream and self.subscriptions:
# Use combined stream for multiple subscriptions
streams = list(self.subscriptions.keys())
stream_list = '/'.join(streams)
url = f"{self.base_url}/stream?streams={stream_list}"
else:
# Use individual stream endpoint
url = f"{self.base_url}/ws"
self.logger.info(f"Connecting to {url}")
# Connect with timeout
self.websocket = await asyncio.wait_for(
websockets.connect(url, ping_interval=None),
timeout=10
)
self.connection_state = ConnectionState.CONNECTED
self.stats['successful_connections'] += 1
self.stats['uptime_start'] = datetime.now()
self.last_message_time = time.time()
self.reconnect_attempts = 0
self.circuit_breaker.record_success()
self.logger.info("WebSocket connected successfully")
# Start health monitoring
asyncio.create_task(self._health_monitor())
return True
except Exception as e:
self.connection_state = ConnectionState.FAILED
self.stats['errors'] += 1
self.stats['last_error'] = str(e)
self.circuit_breaker.record_failure()
self.logger.error(f"Connection failed: {e}")
return False
async def disconnect(self):
"""Gracefully disconnect WebSocket"""
if self.websocket:
self.connection_state = ConnectionState.DISCONNECTED
await self.websocket.close()
self.websocket = None
self.logger.info("WebSocket disconnected")
async def _reconnect(self) -> bool:
"""Handle reconnection with exponential backoff"""
if self.reconnect_attempts >= self.max_reconnect_attempts:
self.logger.error("Max reconnection attempts reached")
self.connection_state = ConnectionState.FAILED
return False
self.connection_state = ConnectionState.RECONNECTING
self.reconnect_attempts += 1
# Exponential backoff
delay = min(self.reconnect_delay * (2 ** (self.reconnect_attempts - 1)),
self.max_reconnect_delay)
self.logger.info(f"Reconnecting in {delay} seconds (attempt {self.reconnect_attempts})")
await asyncio.sleep(delay)
success = await self.connect()
if success:
self.stats['reconnections'] += 1
# Resubscribe to all streams
await self._resubscribe_all()
return success
async def _resubscribe_all(self):
"""Resubscribe to all active streams after reconnection"""
for stream_name in list(self.subscriptions.keys()):
try:
await self._send_subscription(stream_name, "SUBSCRIBE")
self.logger.info(f"Resubscribed to {stream_name}")
except Exception as e:
self.logger.error(f"Failed to resubscribe to {stream_name}: {e}")
async def _health_monitor(self):
"""Monitor connection health and handle reconnections"""
while self.connection_state in [ConnectionState.CONNECTED, ConnectionState.RECONNECTING]:
try:
current_time = time.time()
# Check message timeout
if (self.last_message_time and
current_time - self.last_message_time > self.message_timeout):
self.logger.warning("Message timeout detected, reconnecting...")
await self._reconnect()
continue
# Send ping if needed
if (not self.last_ping_time or
current_time - self.last_ping_time > self.ping_interval):
await self._send_ping()
await asyncio.sleep(5) # Check every 5 seconds
except Exception as e:
self.logger.error(f"Health monitor error: {e}")
await asyncio.sleep(5)
async def _send_ping(self):
"""Send ping to keep connection alive"""
if self.websocket and self.connection_state == ConnectionState.CONNECTED:
try:
await self.websocket.ping()
self.last_ping_time = time.time()
except Exception as e:
self.logger.error(f"Ping failed: {e}")
await self._reconnect()
async def _send_subscription(self, stream: str, method: str):
"""Send subscription/unsubscription message"""
if not self.websocket or self.connection_state != ConnectionState.CONNECTED:
raise Exception("WebSocket not connected")
message = {
"method": method,
"params": [stream],
"id": int(time.time())
}
await self.websocket.send(json.dumps(message))
self.logger.debug(f"Sent {method} for {stream}")
async def subscribe_ticker(self, symbol: str, callback: Callable[[MarketData], None]):
"""Subscribe to 24hr ticker stream"""
stream = f"{symbol.lower()}@ticker"
if stream not in self.subscriptions:
self.subscriptions[stream] = []
self.subscriptions[stream].append(callback)
self.stream_callbacks[stream] = self._handle_ticker_data
if self.connection_state == ConnectionState.CONNECTED:
await self._send_subscription(stream, "SUBSCRIBE")
self.logger.info(f"Subscribed to ticker for {symbol}")
async def subscribe_orderbook(self, symbol: str, callback: Callable[[OrderBookData], None]):
"""Subscribe to order book stream"""
stream = f"{symbol.lower()}@depth20@100ms"
if stream not in self.subscriptions:
self.subscriptions[stream] = []
self.subscriptions[stream].append(callback)
self.stream_callbacks[stream] = self._handle_orderbook_data
if self.connection_state == ConnectionState.CONNECTED:
await self._send_subscription(stream, "SUBSCRIBE")
self.logger.info(f"Subscribed to orderbook for {symbol}")
async def subscribe_trades(self, symbol: str, callback: Callable[[TradeData], None]):
"""Subscribe to trade stream"""
stream = f"{symbol.lower()}@aggTrade"
if stream not in self.subscriptions:
self.subscriptions[stream] = []
self.subscriptions[stream].append(callback)
self.stream_callbacks[stream] = self._handle_trade_data
if self.connection_state == ConnectionState.CONNECTED:
await self._send_subscription(stream, "SUBSCRIBE")
self.logger.info(f"Subscribed to trades for {symbol}")
async def unsubscribe(self, symbol: str, stream_type: str):
"""Unsubscribe from a stream"""
stream_map = {
'ticker': f"{symbol.lower()}@ticker",
'orderbook': f"{symbol.lower()}@depth20@100ms",
'trades': f"{symbol.lower()}@aggTrade"
}
stream = stream_map.get(stream_type)
if not stream:
raise ValueError(f"Unknown stream type: {stream_type}")
if stream in self.subscriptions:
del self.subscriptions[stream]
del self.stream_callbacks[stream]
if self.connection_state == ConnectionState.CONNECTED:
await self._send_subscription(stream, "UNSUBSCRIBE")
self.logger.info(f"Unsubscribed from {stream}")
def _handle_ticker_data(self, data: Dict[str, Any]) -> MarketData:
"""Parse ticker data from WebSocket"""
return MarketData(
symbol=data['s'],
timestamp=datetime.fromtimestamp(data['E'] / 1000),
price=float(data['c']),
volume=float(data['v']),
bid=float(data['b']),
ask=float(data['a']),
high=float(data['h']),
low=float(data['l']),
open=float(data['o']),
close=float(data['c']),
change=float(data['P']),
change_percent=float(data['p'])
)
def _handle_orderbook_data(self, data: Dict[str, Any]) -> OrderBookData:
"""Parse order book data from WebSocket"""
return OrderBookData(
symbol=data['s'],
timestamp=datetime.fromtimestamp(data['E'] / 1000),
bids=[[float(bid[0]), float(bid[1])] for bid in data['b']],
asks=[[float(ask[0]), float(ask[1])] for ask in data['a']],
last_update_id=data['u']
)
def _handle_trade_data(self, data: Dict[str, Any]) -> TradeData:
"""Parse trade data from WebSocket"""
return TradeData(
symbol=data['s'],
timestamp=datetime.fromtimestamp(data['T'] / 1000),
price=float(data['p']),
quantity=float(data['q']),
is_buyer_maker=data['m'],
trade_id=data['a']
)
async def start_listening(self):
"""Start listening for WebSocket messages"""
if not self.websocket or self.connection_state != ConnectionState.CONNECTED:
if not await self.connect():
return
self.logger.info("Started listening for WebSocket messages")
try:
async for message in self.websocket:
try:
self.last_message_time = time.time()
self.stats['messages_received'] += 1
data = json.loads(message)
# Handle subscription responses
if 'result' in data:
if data.get('result') is None:
self.logger.info(f"Subscription confirmed: {data.get('id')}")
continue
# Handle stream data - Binance sends data directly or in stream format
if 'stream' in data and 'data' in data:
# Multi-stream format
stream_name = data['stream']
stream_data = data['data']
elif 'e' in data:
# Single stream format - construct stream name from event type and symbol
event_type = data['e']
symbol = data.get('s', '').lower()
if event_type == '24hrTicker':
stream_name = f"{symbol}@ticker"
elif event_type == 'depthUpdate':
stream_name = f"{symbol}@depth20@100ms"
elif event_type == 'aggTrade':
stream_name = f"{symbol}@aggTrade"
else:
continue
stream_data = data
else:
continue
if stream_name in self.stream_callbacks:
try:
# Parse data using appropriate handler
parsed_data = self.stream_callbacks[stream_name](stream_data)
# Call all registered callbacks for this stream
if stream_name in self.subscriptions:
for callback in self.subscriptions[stream_name]:
try:
callback(parsed_data)
except Exception as e:
self.logger.error(f"Callback error for {stream_name}: {e}")
except Exception as e:
self.logger.error(f"Data parsing error for {stream_name}: {e}")
except json.JSONDecodeError as e:
self.logger.error(f"JSON decode error: {e}")
except Exception as e:
self.logger.error(f"Message processing error: {e}")
except websockets.exceptions.ConnectionClosed:
self.logger.warning("WebSocket connection closed")
await self._reconnect()
except Exception as e:
self.logger.error(f"WebSocket listening error: {e}")
self.stats['errors'] += 1
self.stats['last_error'] = str(e)
await self._reconnect()
def get_connection_stats(self) -> Dict[str, Any]:
"""Get connection statistics"""
uptime = None
if self.stats['uptime_start']:
uptime = (datetime.now() - self.stats['uptime_start']).total_seconds()
return {
'connection_state': self.connection_state.value,
'messages_received': self.stats['messages_received'],
'connection_attempts': self.stats['connection_attempts'],
'successful_connections': self.stats['successful_connections'],
'reconnections': self.stats['reconnections'],
'errors': self.stats['errors'],
'last_error': self.stats['last_error'],
'uptime_seconds': uptime,
'active_subscriptions': len(self.subscriptions),
'circuit_breaker_state': self.circuit_breaker.state,
'circuit_breaker_failures': self.circuit_breaker.failure_count
}
async def run_forever(self):
"""Run WebSocket client with automatic reconnection"""
while True:
try:
if self.connection_state == ConnectionState.DISCONNECTED:
await self.connect()
if self.connection_state == ConnectionState.CONNECTED:
await self.start_listening()
else:
await asyncio.sleep(5)
except KeyboardInterrupt:
self.logger.info("Shutting down WebSocket client")
await self.disconnect()
break
except Exception as e:
self.logger.error(f"Unexpected error in run_forever: {e}")
await asyncio.sleep(5)
class MarketDataManager:
"""High-level market data manager using WebSocket client"""
def __init__(self, testnet: bool = True):
self.ws_client = WebSocketClient(testnet)
self.market_data_cache: Dict[str, MarketData] = {}
self.orderbook_cache: Dict[str, OrderBookData] = {}
self.trade_callbacks: List[Callable] = []
# Setup logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
self.logger = logging.getLogger(__name__)
async def start(self):
"""Start the market data manager"""
await self.ws_client.connect()
# Start listening in background
asyncio.create_task(self.ws_client.run_forever())
self.logger.info("Market data manager started")
async def stop(self):
"""Stop the market data manager"""
await self.ws_client.disconnect()
self.logger.info("Market data manager stopped")
async def subscribe_symbol(self, symbol: str, include_orderbook: bool = True, include_trades: bool = False):
"""Subscribe to all data streams for a symbol"""
# Subscribe to ticker
await self.ws_client.subscribe_ticker(symbol, self._on_ticker_update)
# Subscribe to orderbook if requested
if include_orderbook:
await self.ws_client.subscribe_orderbook(symbol, self._on_orderbook_update)
# Subscribe to trades if requested
if include_trades:
await self.ws_client.subscribe_trades(symbol, self._on_trade_update)
self.logger.info(f"Subscribed to data streams for {symbol}")
def _on_ticker_update(self, data: MarketData):
"""Handle ticker updates"""
self.market_data_cache[data.symbol] = data
self.logger.debug(f"Updated ticker for {data.symbol}: ${data.price}")
def _on_orderbook_update(self, data: OrderBookData):
"""Handle orderbook updates"""
self.orderbook_cache[data.symbol] = data
self.logger.debug(f"Updated orderbook for {data.symbol}")
def _on_trade_update(self, data: TradeData):
"""Handle trade updates"""
# Call registered trade callbacks
for callback in self.trade_callbacks:
try:
callback(data)
except Exception as e:
self.logger.error(f"Trade callback error: {e}")
def get_market_data(self, symbol: str) -> Optional[MarketData]:
"""Get latest market data for symbol"""
return self.market_data_cache.get(symbol)
def get_orderbook(self, symbol: str) -> Optional[OrderBookData]:
"""Get latest orderbook for symbol"""
return self.orderbook_cache.get(symbol)
def add_trade_callback(self, callback: Callable[[TradeData], None]):
"""Add callback for trade updates"""
self.trade_callbacks.append(callback)
def get_stats(self) -> Dict[str, Any]:
"""Get connection and data statistics"""
ws_stats = self.ws_client.get_connection_stats()
return {
**ws_stats,
'cached_symbols': len(self.market_data_cache),
'cached_orderbooks': len(self.orderbook_cache),
'trade_callbacks': len(self.trade_callbacks)
}
# Example usage and testing
async def example_usage():
"""Example of how to use the WebSocket client"""
# Setup logging
logging.basicConfig(level=logging.INFO)
# Create market data manager
manager = MarketDataManager(testnet=True)
# Define callbacks
def on_trade(trade_data: TradeData):
print(f"Trade: {trade_data.symbol} - ${trade_data.price} x {trade_data.quantity}")
# Add trade callback
manager.add_trade_callback(on_trade)
try:
# Start manager
await manager.start()
# Subscribe to some symbols
symbols = ['BTCUSDT', 'ETHUSDT', 'BNBUSDT']
for symbol in symbols:
await manager.subscribe_symbol(symbol, include_trades=True)
# Run for a while and print stats
for i in range(60): # Run for 1 minute
await asyncio.sleep(1)
if i % 10 == 0: # Print stats every 10 seconds
stats = manager.get_stats()
print(f"Stats: {stats['messages_received']} messages, "
f"{stats['cached_symbols']} symbols, "
f"State: {stats['connection_state']}")
# Print latest prices
for symbol in symbols:
data = manager.get_market_data(symbol)
if data:
print(f"{symbol}: ${data.price} ({data.change_percent:+.2f}%)")
except KeyboardInterrupt:
print("Stopping...")
finally:
await manager.stop()
if __name__ == "__main__":
asyncio.run(example_usage())