| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 42c27d8 commit 7ba13fa
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -137,7 +137,7 @@ def __init__(self, client, symbol, callback=None, refresh_interval=_default_refr | |||
| 137 | 137 | self._client = client | |
| 138 | 138 | self._symbol = symbol | |
| 139 | 139 | self._callback = callback | |
| 140 | - self._first_update_id = None | ||
| 140 | + self._last_update_id = None | ||
| 141 | 141 | self._depth_message_buffer = [] | |
| 142 | 142 | self._bm = None | |
| 143 | 143 | self._depth_cache = DepthCache(self._symbol) | |
@@ -151,7 +151,7 @@ def _init_cache(self): | |||
| 151 | 151 | ||
| 152 | 152 | :return: | |
| 153 | 153 | """ | |
| 154 | - self._first_update_id = None | ||
| 154 | + self._last_update_id = None | ||
| 155 | 155 | self._depth_message_buffer = [] | |
| 156 | 156 | ||
| 157 | 157 | res = self._client.get_order_book(symbol=self._symbol, limit=500) | |
@@ -163,15 +163,15 @@ def _init_cache(self): | |||
| 163 | 163 | self._depth_cache.add_ask(ask) | |
| 164 | 164 | ||
| 165 | 165 | # set first update id | |
| 166 | - self._first_update_id = res['lastUpdateId'] | ||
| 166 | + self._last_update_id = res['lastUpdateId'] | ||
| 167 | 167 | ||
| 168 | 168 | # set a time to refresh the depth cache | |
| 169 | 169 | if self._refresh_interval: | |
| 170 | 170 | self._refresh_time = int(time.time()) + self._refresh_interval | |
| 171 | 171 | ||
| 172 | 172 | # Apply any updates from the websocket | |
| 173 | 173 | for msg in self._depth_message_buffer: | |
| 174 | - self._process_depth_message(msg) | ||
| 174 | + self._process_depth_message(msg, buffer=True) | ||
| 175 | 175 | ||
| 176 | 176 | # clear the depth buffer | |
| 177 | 177 | del self._depth_message_buffer | |
@@ -199,22 +199,27 @@ def _depth_event(self, msg): | |||
| 199 | 199 | ||
| 200 | 200 | """ | |
| 201 | 201 | ||
| 202 | - if self._first_update_id is None: | ||
| 202 | + if self._last_update_id is None: | ||
| 203 | 203 | # Initial depth snapshot fetch not yet performed, buffer messages | |
| 204 | 204 | self._depth_message_buffer.append(msg) | |
| 205 | 205 | else: | |
| 206 | 206 | self._process_depth_message(msg) | |
| 207 | 207 | ||
| 208 | - def _process_depth_message(self, msg): | ||
| 208 | + def _process_depth_message(self, msg, buffer=False): | ||
| 209 | 209 | """Process a depth event message. | |
| 210 | 210 | ||
| 211 | 211 | :param msg: Depth event message. | |
| 212 | 212 | :return: | |
| 213 | 213 | ||
| 214 | 214 | """ | |
| 215 | - # ignore any updates before the initial update id | ||
| 216 | - if msg['u'] <= self._first_update_id: | ||
| 215 | + | ||
| 216 | + if buffer and msg['u'] <= self._last_update_id: | ||
| 217 | + # ignore any updates before the initial update id | ||
| 217 | 218 | return | |
| 219 | + elif msg['U'] != self._last_update_id + 1: | ||
| 220 | + # if not buffered check we get sequential updates | ||
| 221 | + # otherwise init cache again | ||
| 222 | + self._init_cache() | ||
| 218 | 223 | ||
| 219 | 224 | # add any bid or ask values | |
| 220 | 225 | for bid in msg['b']: | |
@@ -226,6 +231,8 @@ def _process_depth_message(self, msg): | |||
| 226 | 231 | if self._callback: | |
| 227 | 232 | self._callback(self._depth_cache) | |
| 228 | 233 | ||
| 234 | + self._last_update_id = msg['u'] | ||
| 235 | + | ||
| 229 | 236 | # after processing event see if we need to refresh the depth cache | |
| 230 | 237 | if self._refresh_interval and int(time.time()) > self._refresh_time: | |
| 231 | 238 | self._init_cache() | |
| Back | FazBrowse Home | New Git URL |
0 commit comments