forked from sammchardy/python-binance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdepthcache.py
More file actions
273 lines (210 loc) · 7.17 KB
/
depthcache.py
File metadata and controls
273 lines (210 loc) · 7.17 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
# coding=utf-8
from operator import itemgetter
import time
from .websockets import BinanceSocketManager
class DepthCache(object):
def __init__(self, symbol):
"""Initialise the DepthCache
:param symbol: Symbol to create depth cache for
:type symbol: string
"""
self.symbol = symbol
self._bids = {}
self._asks = {}
self.update_time = None
def add_bid(self, bid):
"""Add a bid to the cache
:param bid:
:return:
"""
self._bids[bid[0]] = float(bid[1])
if bid[1] == "0.00000000":
del self._bids[bid[0]]
def add_ask(self, ask):
"""Add an ask to the cache
:param ask:
:return:
"""
self._asks[ask[0]] = float(ask[1])
if ask[1] == "0.00000000":
del self._asks[ask[0]]
def get_bids(self):
"""Get the current bids
:return: list of bids with price and quantity as floats
.. code-block:: python
[
[
0.0001946, # Price
45.0 # Quantity
],
[
0.00019459,
2384.0
],
[
0.00019158,
5219.0
],
[
0.00019157,
1180.0
],
[
0.00019082,
287.0
]
]
"""
return DepthCache.sort_depth(self._bids, reverse=True)
def get_asks(self):
"""Get the current asks
:return: list of asks with price and quantity as floats
.. code-block:: python
[
[
0.0001955, # Price
57.0' # Quantity
],
[
0.00019699,
778.0
],
[
0.000197,
64.0
],
[
0.00019709,
1130.0
],
[
0.0001971,
385.0
]
]
"""
return DepthCache.sort_depth(self._asks, reverse=False)
@staticmethod
def sort_depth(vals, reverse=False):
"""Sort bids or asks by price
"""
lst = [[float(price), quantity] for price, quantity in vals.items()]
lst = sorted(lst, key=itemgetter(0), reverse=reverse)
return lst
class DepthCacheManager(object):
_default_refresh = 60 * 30 # 30 minutes
def __init__(self, client, symbol, callback=None, refresh_interval=_default_refresh, bm=None, limit=500):
"""Initialise the DepthCacheManager
:param client: Binance API client
:type client: binance.Client
:param symbol: Symbol to create depth cache for
:type symbol: string
:param callback: Optional function to receive depth cache updates
:type callback: function
:param refresh_interval: Optional number of seconds between cache refresh, use 0 or None to disable
:type refresh_interval: int
:param limit: Optional number of orders to get from orderbook
:type limit: int
"""
self._client = client
self._symbol = symbol
self._limit = limit
self._callback = callback
self._last_update_id = None
self._depth_message_buffer = []
self._bm = bm
self._depth_cache = DepthCache(self._symbol)
self._refresh_interval = refresh_interval
self._conn_key = None
self._start_socket()
self._init_cache()
def _init_cache(self):
"""Initialise the depth cache calling REST endpoint
:return:
"""
self._last_update_id = None
self._depth_message_buffer = []
res = self._client.get_order_book(symbol=self._symbol, limit=self._limit)
# process bid and asks from the order book
for bid in res['bids']:
self._depth_cache.add_bid(bid)
for ask in res['asks']:
self._depth_cache.add_ask(ask)
# set first update id
self._last_update_id = res['lastUpdateId']
# set a time to refresh the depth cache
if self._refresh_interval:
self._refresh_time = int(time.time()) + self._refresh_interval
# Apply any updates from the websocket
for msg in self._depth_message_buffer:
self._process_depth_message(msg, buffer=True)
# clear the depth buffer
self._depth_message_buffer = []
def _start_socket(self):
"""Start the depth cache socket
:return:
"""
if self._bm is None:
self._bm = BinanceSocketManager(self._client)
self._conn_key = self._bm.start_depth_socket(self._symbol, self._depth_event)
if not self._bm.is_alive():
self._bm.start()
# wait for some socket responses
while not len(self._depth_message_buffer):
time.sleep(1)
def _depth_event(self, msg):
"""Handle a depth event
:param msg:
:return:
"""
if 'e' in msg and msg['e'] == 'error':
# close the socket
self.close()
# notify the user by returning a None value
if self._callback:
self._callback(None)
if self._last_update_id is None:
# Initial depth snapshot fetch not yet performed, buffer messages
self._depth_message_buffer.append(msg)
else:
self._process_depth_message(msg)
def _process_depth_message(self, msg, buffer=False):
"""Process a depth event message.
:param msg: Depth event message.
:return:
"""
if buffer and msg['u'] <= self._last_update_id:
# ignore any updates before the initial update id
return
elif msg['U'] != self._last_update_id + 1:
# if not buffered check we get sequential updates
# otherwise init cache again
self._init_cache()
# add any bid or ask values
for bid in msg['b']:
self._depth_cache.add_bid(bid)
for ask in msg['a']:
self._depth_cache.add_ask(ask)
# keeping update time
self._depth_cache.update_time = msg['E']
# call the callback with the updated depth cache
if self._callback:
self._callback(self._depth_cache)
self._last_update_id = msg['u']
# after processing event see if we need to refresh the depth cache
if self._refresh_interval and int(time.time()) > self._refresh_time:
self._init_cache()
def get_depth_cache(self):
"""Get the current depth cache
:return: DepthCache object
"""
return self._depth_cache
def close(self, close_socket=False):
"""Close the open socket for this manager
:return:
"""
self._bm.stop_socket(self._conn_key)
if close_socket:
self._bm.close()
time.sleep(1)
self._depth_cache = None