Skip to content

Commit

Permalink
Added options websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
Ollie-Hooper committed Apr 8, 2021
1 parent 4f8c5f0 commit 0c19ba8
Showing 1 changed file with 79 additions and 6 deletions.
85 changes: 79 additions & 6 deletions binance/websockets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding=utf-8
import gzip
import threading

from autobahn.twisted.websocket import WebSocketClientFactory, \
Expand All @@ -22,13 +23,18 @@ def onConnect(self, response):
self.factory.resetDelay()

def onMessage(self, payload, isBinary):
if not isBinary:
if isBinary:
try:
payload_obj = json.loads(payload.decode('utf8'))
except ValueError:
pass
else:
self.factory.callback(payload_obj)
payload = gzip.decompress(payload)
except:
print('Could not interpret binary response payload')

try:
payload_obj = json.loads(payload.decode('utf8'))
except ValueError:
pass
else:
self.factory.callback(payload_obj)


class BinanceReconnectingClientFactory(ReconnectingClientFactory):
Expand Down Expand Up @@ -64,6 +70,7 @@ class BinanceSocketManager(threading.Thread):

STREAM_URL = 'wss://stream.binance.com:9443/'
FSTREAM_URL = 'wss://fstream.binance.com/'
VSTREAM_URL = 'wss://vstream.binance.com/'

WEBSOCKET_DEPTH_5 = '5'
WEBSOCKET_DEPTH_10 = '10'
Expand Down Expand Up @@ -120,6 +127,20 @@ def _start_futures_socket(self, path, callback, prefix='stream?streams='):
self._conns[path] = connectWS(factory, context_factory)
return path

def _start_options_socket(self, path, callback, prefix='ws/'):
if path in self._conns:
return False

factory_url = self.VSTREAM_URL + prefix + path
factory = BinanceClientFactory(factory_url)
factory.protocol = BinanceClientProtocol
factory.callback = callback
factory.reconnect = True
context_factory = ssl.ClientContextFactory()

self._conns[path] = connectWS(factory, context_factory)
return path

def start_depth_socket(self, symbol, callback, depth=None, interval=None):
"""Start a websocket for symbol market depth returning either a diff or a partial book
Expand Down Expand Up @@ -698,6 +719,58 @@ def start_isolated_margin_socket(self, symbol, callback):
# and start the socket with this specific kek
return self._start_account_socket(symbol, isolated_margin_listen_key, callback)

def start_options_ticker_socket(self, symbol, callback):
"""Subscribe to a 24 hour ticker info stream
https://binance-docs.github.io/apidocs/voptions/en/#market-streams-payload-24-hour-ticker
:param symbol: required
:type symbol: str
:param callback: callback function to handle messages
:type callback: function
"""
return self._start_options_socket(symbol.lower() + '@ticker', callback)

def start_options_recent_trades_socket(self, symbol, callback):
"""Subscribe to a latest completed trades stream
https://binance-docs.github.io/apidocs/voptions/en/#market-streams-payload-latest-completed-trades
:param symbol: required
:type symbol: str
:param callback: callback function to handle messages
:type callback: function
"""
return self._start_options_socket(symbol.lower() + '@trade', callback)

def start_options_kline_socket(self, symbol, callback, interval=Client.KLINE_INTERVAL_1MINUTE):
"""Subscribe to a candlestick data stream
https://binance-docs.github.io/apidocs/voptions/en/#market-streams-payload-candle
:param symbol: required
:type symbol: str
:param callback: callback function to handle messages
:type callback: function
:param interval: Kline interval, default KLINE_INTERVAL_1MINUTE
:type interval: str
"""
return self._start_options_socket(symbol.lower() + '@kline_' + interval, callback)

def start_options_depth_socket(self, symbol, callback, depth='10'):
"""Subscribe to a depth data stream
https://binance-docs.github.io/apidocs/voptions/en/#market-streams-payload-depth
:param symbol: required
:type symbol: str
:param callback: callback function to handle messages
:type callback: function
:param depth: optional Number of depth entries to return, default 10.
:type depth: str
"""
return self._start_options_socket(symbol.lower() + '@depth' + str(depth), callback)

def _start_account_socket(self, socket_type, listen_key, callback):
"""Starts one of user or margin socket"""
self._check_account_socket_open(listen_key)
Expand Down

0 comments on commit 0c19ba8

Please sign in to comment.