@@ -283,3 +283,103 @@ def get_symbol(self):
283283 :return: symbol
284284 """
285285 return self ._symbol
286+
287+
288+ class OptionsDepthCacheManager (DepthCacheManager ):
289+ _default_refresh = 60 * 30 # 30 minutes
290+
291+ def __init__ (self , client , symbol , callback = None , refresh_interval = _default_refresh , bm = None , limit = 10 ):
292+ """Initialise the OptionsDepthCacheManager
293+
294+ :param client: Binance API client
295+ :type client: binance.Client
296+ :param symbol: Symbol to create depth cache for
297+ :type symbol: string
298+ :param callback: Optional function to receive depth cache updates
299+ :type callback: function
300+ :param refresh_interval: Optional number of seconds between cache refresh, use 0 or None to disable
301+ :type refresh_interval: int
302+ :param limit: Optional number of orders to get from orderbook
303+ :type limit: int
304+
305+ """
306+ self ._client = client
307+ self ._symbol = symbol
308+ self ._limit = limit
309+ self ._callback = callback
310+ self ._bm = bm
311+ self ._refresh_interval = refresh_interval
312+ self ._conn_key = None
313+
314+ self ._init_cache ()
315+ self ._start_socket ()
316+
317+ def _init_cache (self ):
318+ """Initialise the depth cache and set a refresh time
319+
320+ :return:
321+ """
322+
323+ # initialise or clear depth cache
324+ self ._depth_cache = DepthCache (self ._symbol )
325+
326+ # set a time to refresh the depth cache
327+ if self ._refresh_interval :
328+ self ._refresh_time = int (time .time ()) + self ._refresh_interval
329+
330+ def _start_socket (self ):
331+ """Start the depth cache socket
332+
333+ :return:
334+ """
335+ if self ._bm is None :
336+ self ._bm = BinanceSocketManager (self ._client )
337+
338+ self ._conn_key = self ._bm .start_options_depth_socket (self ._symbol , self ._depth_event )
339+ if not self ._bm .is_alive ():
340+ self ._bm .start ()
341+
342+ def _depth_event (self , msg ):
343+ """Handle a depth event
344+
345+ :param msg:
346+ :return:
347+
348+ """
349+
350+ if 'e' in msg and msg ['e' ] == 'error' :
351+ # close the socket
352+ self .close ()
353+
354+ # notify the user by returning a None value
355+ if self ._callback :
356+ self ._callback (None )
357+
358+ self ._process_depth_message (msg )
359+
360+ def _process_depth_message (self , msg , buffer = False ):
361+ """Process a depth event message.
362+
363+ :param msg: Depth event message.
364+ :return:
365+
366+ """
367+
368+ # add any bid or ask values
369+ for bid in msg ['b' ]:
370+ self ._depth_cache .add_bid (bid )
371+ for ask in msg ['a' ]:
372+ self ._depth_cache .add_ask (ask )
373+
374+ # keeping update time
375+ self ._depth_cache .update_time = msg ['E' ]
376+
377+ # call the callback with the updated depth cache
378+ if self ._callback :
379+ self ._callback (self ._depth_cache )
380+
381+ # after processing event see if we need to refresh the depth cache
382+ if self ._refresh_interval and int (time .time ()) > self ._refresh_time :
383+ self .close ()
384+ self ._init_cache ()
385+ self ._start_socket ()
0 commit comments