SHARE
TWEET

bybit ws

a guest Feb 22nd, 2020 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class BybitWS:
  2.     PRIVATE_TOPIC = ["position", "execution", "order", "stop_order"]
  3.     def __init__(self, ws_url, api_key, api_secret):
  4.  
  5.         self.api_key = api_key
  6.         self.api_secret = api_secret
  7.  
  8.         self.auth = False
  9.         self.data = {}
  10.         self.exited = False
  11.  
  12.         self.__connect(ws_url)
  13.  
  14.     def exit(self):
  15.         self.exited = True
  16.         self.ws.close()
  17.  
  18.     def __connect(self, ws_url):
  19.         self.ws = websocket.WebSocketApp(ws_url,
  20.                                          on_message=self.__on_message,
  21.                                          on_close=self.__on_close,
  22.                                          on_open=self.__on_open,
  23.                                          on_error=self.__on_error,
  24.                                          keep_running=True)
  25.         self.wst = threading.Thread(target=lambda: self.ws.run_forever())
  26.         self.wst.daemon = True
  27.         self.wst.start()
  28.  
  29.         retry_times = 5
  30.         while not self.ws.sock or not self.ws.sock.connected and retry_times:
  31.             sleep(1)
  32.             retry_times -= 1
  33.         if retry_times == 0 and not self.ws.sock.connected:
  34.             self.exit()
  35.  
  36.         if self.api_key and self.api_secret:
  37.             self.__auth()
  38.  
  39.     def signature(self, expires):
  40.         constructed_params = str("GET/realtime") + str(expires)
  41.  
  42.         hash = hmac.new(bytes(self.api_secret, "utf-8"), bytes(constructed_params, "utf-8"), hashlib.sha256)
  43.         return hash.hexdigest()
  44.  
  45.     def __auth(self):
  46.         expires = str(int(round(time.time())+1))+"000"
  47.         signature = self.signature(expires)
  48.  
  49.         auth = {}
  50.         auth["op"] = "auth"
  51.         auth["args"] = [self.api_key, expires, signature]
  52.  
  53.         args = json.dumps(auth)
  54.  
  55.         self.ws.send(args)
  56.  
  57.     def __on_message(self, message):
  58.         message = json.loads(message)
  59.         if "success" in message and message ["success"]:
  60.             if "request" in message and message["request"]["op"] == "auth":
  61.                 self.auth = True
  62.             if "ret_msg" in message and message["ret_msg"] == "pong":
  63.                 self.data["pong"].append("PING successful")
  64.  
  65.         if "topic" in message:
  66.             self.data[message["topic"]].append(message["data"])
  67.  
  68.     def __on_error(self, error):
  69.         if not self.exited:
  70.             raise websocket.WebSocketException(error)
  71.  
  72.     def __on_open(self):
  73.         pass
  74.  
  75.     def __on_close(self):
  76.         pass
  77.  
  78.     def ping(self):
  79.         message = {"op": "ping"}
  80.         self.ws.send(json.dumps(message))
  81.  
  82.     def subscribe_kline(self, symbol: str, interval: str):
  83.         self.ws.send(json.dumps({"op": "subscribe", "args": ["klineV2." + interval + "." + symbol]}))
  84.         if "kline." + symbol + "." + interval not in self.data:
  85.             self.data["kline." + symbol + "." + interval] = []
  86.  
  87.     def subscribe_trade(self):
  88.         self.ws.send(json.dumps({"op": "subscribe", "args": ["trade"]}))
  89.         if "trade.BTCUSD" not in self.data:
  90.             self.data["trade.BTCUSD"] = []
  91.             self.data["trade.ETHUSD"] = []
  92.             self.data["trade.EOSUSD"] = []
  93.             self.data["trade.XRPUSD"] = []
  94.  
  95.     def subscribe_insurance(self):
  96.         self.ws.send(json.dumps({"op": "subscribe", "args": ["insurance"]}))
  97.         if "insurance.BTC" not in self.data:
  98.             self.data["insurance.BTC"] = []
  99.             self.data["insurance.XRP"] = []
  100.             self.data["insurance.EOS"] = []
  101.             self.data["insurance.ETH"] = []
  102.  
  103.     def subscribe_orderBookL2_25(self, symbol):
  104.         self.ws.send(json.dumps({"op": "subscribe", "args": ["orderBookL2_25." + symbol]}))
  105.         if "orderBookL2_25." + symbol not in self.data:
  106.             self.data["orderBookL2_25." + symbol] = []
  107.  
  108.     def subscribe_orderBookL2_200(self, symbol):
  109.         self.ws.send(json.dumps({"op": "subscribe", "args": ["orderBook_200.100ms" + symbol]}))
  110.         if "orderBook_200.100ms" + symbol not in self.data:
  111.             self.data["orderBook_200.100ms" + symbol] = []
  112.  
  113.     def subscribe_instrument_info(self, symbol):
  114.         self.ws.send(json.dumps({"op": "subscribe", "args": ["instrument_info.100ms." + symbol]}))
  115.         if "instrument_info.100ms." + symbol not in self.data:
  116.             self.data["instrument_info.100ms." + symbol] = []
  117.  
  118.     def subscribe_position(self):
  119.         self.ws.send(json.dumps({"op": "subscribe", "args": ["position"]}))
  120.         if "position" not in self.data:
  121.             self.data["position"] = []
  122.  
  123.     def subscribe_execution(self):
  124.         self.ws.send(json.dumps({"op": "subscribe", "args": ["execution"]}))
  125.         if "execution" not in self.data:
  126.             self.data["execution"] = []
  127.  
  128.     def subscribe_order(self):
  129.         self.ws.send(json.dumps({"op": "subscribe", "args": ["order"]}))
  130.         if "order" not in self.data:
  131.             self.data["order"] = []
  132.  
  133.     def subscribe_stop_order(self):
  134.         self.ws.send(json.dumps({"op": "subscribe", "args": ["stop_order"]}))
  135.         if "stop_order" not in self.data:
  136.             self.data["stop_order"] = []
  137.  
  138.     def get_data(self, topic):
  139.         if topic not in self.data:
  140.             return []
  141.         if topic.split(".")[0] in BybitWS.PRIVATE_TOPIC and not self.auth:
  142.             return []
  143.         else:
  144.             if len(self.data[topic]) == 0:
  145.                 return []
  146.             return self.data[topic].pop()
  147.  
  148.  
  149. BybitWS = Bybit.BybitWS(ws_base_url, api_key, api_secret)
  150. BybitWS.subscribe_instrument_info("BTCUSD")
  151. instrument_response = BybitWS.get_data("instrument_info.100ms.BTCUSD")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top