forked from sammchardy/python-binance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdepth_cache_threaded_example.py
More file actions
38 lines (28 loc) · 1.14 KB
/
depth_cache_threaded_example.py
File metadata and controls
38 lines (28 loc) · 1.14 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
#!/usr/bin/env python3
import os
import sys
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(root)
import logging
from binance.ws.depthcache import ThreadedDepthCacheManager
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
def main():
dcm = ThreadedDepthCacheManager()
dcm.start()
def handle_depth_cache(depth_cache):
if isinstance(depth_cache, dict) and depth_cache.get('e') == 'error':
logger.error(f"Received depth cache error in callback: {depth_cache}")
type = depth_cache.get('type')
if type == 'BinanceWebsocketClosed':
# Automatically attempts to reconnect
return
logger.error(f"Error received - Closing depth cache: {depth_cache}")
dcm.stop()
return
logger.info(f"symbol {depth_cache.symbol}")
logger.info(depth_cache.get_bids()[:5])
dcm.start_depth_cache(handle_depth_cache, symbol='BNBBTC')
dcm.join()
if __name__ == "__main__":
main()