forked from sammchardy/python-binance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdepth_cache_example.py
More file actions
60 lines (46 loc) · 1.83 KB
/
depth_cache_example.py
File metadata and controls
60 lines (46 loc) · 1.83 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
#!/usr/bin/env python3
import os
import sys
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(root)
import asyncio
import logging
from binance import AsyncClient
from binance.ws.depthcache import DepthCacheManager
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
async def main():
# Initialize the client
client = await AsyncClient.create()
# Symbol to monitor
symbol = 'BTCUSDT'
# Create a depth cache manager instance
async with DepthCacheManager(
client=client,
symbol=symbol,
) as dcm:
logger.info(f"Started depth cache for {symbol}")
# Monitor depth cache updates for 1 minute
for _ in range(100): # 6 iterations * 10 seconds = 1 minute
depth_cache = await dcm.recv()
if isinstance(depth_cache, dict) and depth_cache.get('e') == 'error':
logger.error(f"Received depth cache error in callback: {depth_cache}")
if type == 'BinanceWebsocketClosed':
# ignore as attempts to reconnect
continue
break
# Get current bids and asks
bids = depth_cache.get_bids()[:5] # Top 5 bids
asks = depth_cache.get_asks()[:5] # Top 5 asks
logger.info("Top 5 bids:")
for bid in bids:
logger.info(f"Price: {bid[0]}, Quantity: {bid[1]}")
logger.info("Top 5 asks:")
for ask in asks:
logger.info(f"Price: {ask[0]}, Quantity: {ask[1]}")
logger.info(f"Last update time: {depth_cache.update_time}")
# Close the client
await client.close_connection()
if __name__ == '__main__':
# Run the async example
asyncio.run(main())