Exemple #1
0
 async def _run(self):
     async with websockets.connect(self.ws_uri) as websocket:
         self.ws_conn = websocket
         added_channels = set()
         while True:
             for channel in self.channels:
                 if channel in added_channels:
                     continue
                 added_channels.add(channel)
                 req = json.dumps({
                     "id": utils.get_req_id(),
                     "method": "subscribeOrderbook",
                     "params": {
                         "symbol": channel
                     }
                 })
                 await websocket.send(req)
             resp = await websocket.recv()
             data = json.loads(resp)
             if 'method' in data and data['method'] in [
                     'snapshotOrderbook', 'updateOrderbook'
             ]:
                 self.notify(data)
             else:
                 logger.warning("unknown data %s", data)
Exemple #2
0
 async def _run(self):
     async with websockets.connect(self.ws_uri) as websocket:
         self.ws_conn = websocket
         added_channels = set()
         while True:
             for channel in self.channels:
                 if channel in added_channels:
                     continue
                 added_channels.add(channel)
                 req = json.dumps({
                     "event":
                     "addChannel",
                     "channel":
                     f"bibox_sub_spot_{channel}_depth"
                 })
                 await websocket.send(req)
             resp = await websocket.recv()
             data = json.loads(resp)
             if 'ping' in data:
                 req = json.dumps({"pong": data['ping']})
                 await websocket.send(req)
             elif 'pong' in data:
                 logger.warning("ping data %s", data)
             else:
                 self.notify(data)
Exemple #3
0
 async def _run(self):
     async with websockets.connect(self.ws_uri,
                                   ping_interval=None) as websocket:
         self.ws_conn = websocket
         added_channels = set()
         while True:
             for channel in self.channels:
                 if channel in added_channels:
                     continue
                 added_channels.add(channel)
                 req = json.dumps(["sub.symbol", {"symbol": channel}])
                 await websocket.send(f"42{req}")
                 req = json.dumps(["get.depth", {"symbol": channel}])
                 await websocket.send(f"42{req}")
             resp = await websocket.recv()
             if resp.startswith('42'):
                 data = json.loads(resp.lstrip('42'))
                 if data[0] in ['push.symbol', 'rs.depth']:
                     self.notify(data)
                 else:
                     logger.warning("unknown data %s", data)
             elif resp.startswith('3'):
                 # ping pong
                 pass
             else:
                 logger.warning("unknown data %s", resp)
Exemple #4
0
 async def _run(self):
     async with websockets.connect(self.ws_uri) as websocket:
         self.ws_conn = websocket
         added_channels = set()
         while True:
             for channel in self.channels:
                 if channel in added_channels:
                     continue
                 added_channels.add(channel)
                 req = json.dumps({"op": "sub", "ch": f"depth:{channel}"})
                 await websocket.send(req)
                 req = json.dumps({
                     "op": "req",
                     "action": "depth-snapshot-top100",
                     "args": {
                         "symbol": channel
                     }
                 })
                 await websocket.send(req)
             resp = await websocket.recv()
             data = json.loads(resp)
             if data['m'] == 'ping':
                 req = json.dumps({'op': 'pong'})
                 await websocket.send(req)
             elif data['m'] in ['depth-snapshot', 'depth']:
                 self.notify(data)
             else:
                 logger.warning("unknown data %s", data)
Exemple #5
0
    def notify(self, data):
        if 'data' not in data:
            logger.warning("unknown data %s", data)
            return
        final_data = {'full': True, 'asks': [], 'bids': []}
        final_data['asks'] = [[float(item[0]), float(item[1])]
                              for item in data['data']['asks']]
        final_data['bids'] = [[float(item[0]), float(item[1])]
                              for item in data['data']['bids']]

        for observer in self.observers:
            if observer.channel not in data['stream']:
                continue
            observer.update(final_data)
Exemple #6
0
 async def _run(self):
     async with websockets.connect(self.ws_uri) as websocket:
         self.ws_conn = websocket
         is_added = False
         while True:
             params = {"op": "subscribe", "args": [f'spot/depth5:{item}' for item in self.channels]}
             if not is_added:
                 req = json.dumps(params)
                 await websocket.send(req)
                 is_added = True
             resp = await websocket.recv()
             decoded_data = zlib.decompress(resp, -15)
             data = json.loads(decoded_data)
             if 'table' in data and data['table'] == 'spot/depth5':
                 self.notify(data)
             else:
                 logger.warning("unknown data %s", data)
Exemple #7
0
    def notify(self, data):
        if len(data) > 1:
            logger.warning("unknown data %s", data)
            return
        b64_data = data[0]['data']
        gz_data = base64.b64decode(b64_data)
        decoded_data = gzip.decompress(gz_data)
        j_data = json.loads(decoded_data)
        final_data = {'full': True, 'asks': [], 'bids': []}
        final_data['asks'] = [[float(item['price']),
                               float(item['volume'])]
                              for item in j_data['asks']]
        final_data['bids'] = [[float(item['price']),
                               float(item['volume'])]
                              for item in j_data['bids']]

        for observer in self.observers:
            if observer.channel not in data[0]['channel']:
                continue
            observer.update(final_data)
Exemple #8
0
 async def _run(self):
     async with websockets.connect(self.ws_uri,
                                   ping_interval=None) as websocket:
         self.ws_conn = websocket
         is_added = False
         while True:
             if not is_added:
                 params = [[item, 5, '0'] for item in self.channels]
                 req = json.dumps({
                     "id": utils.get_req_id(),
                     "method": "depth.subscribe_multi",
                     "params": params
                 })
                 await websocket.send(req)
                 is_added = True
             resp = await websocket.recv()
             data = json.loads(resp)
             if 'method' in data and data['method'] == 'depth.update':
                 self.notify(data)
             else:
                 logger.warning("unknown data %s", data)
Exemple #9
0
    def notify(self, data):
        final_data = {'asks': [], 'bids': []}
        if len(data) >= 3:
            if data[2][0][0] == 'i':
                final_data['full'] = True
                final_data['asks'] = [[float(p), float(v)] for p, v in data[2][0][1]['orderBook'][0].items()]
                final_data['bids'] = [[float(p), float(v)] for p, v in data[2][0][1]['orderBook'][1].items()]
            elif data[2][0][0] == 'o':
                final_data['full'] = False
                final_data['asks'] = [[float(item[2]), float(item[3])] for item in data[2] if item[0] == 'o' and item[1] == 0]
                final_data['bids'] = [[float(item[2]), float(item[3])] for item in data[2] if item[0] == 'o' and item[1] == 1]
            else:
                logger.warning("unknown data %s", data)
                return
        else:
            logger.warning("unknown data %s", data)
            return

        for observer in self.observers:
            if observer.channel != data[0]:
                continue
            observer.update(final_data)
Exemple #10
0
 async def _run(self):
     exchange = self.observers[0].exchange
     resp = await exchange.publicPostBulletPublic()
     endpoint = resp['data']['instanceServers'][0]['endpoint']
     self.ping_sleep_time = float(
         resp['data']['instanceServers'][0]['pingInterval']) / 1000
     token = resp['data']['token']
     ws_uri = f'{endpoint}?token={token}'
     async with websockets.connect(ws_uri) as websocket:
         self.ws_conn = websocket
         is_available = False
         is_added = False
         while True:
             if not is_available:
                 resp = await websocket.recv()
                 data = json.loads(resp)
                 if data['type'] == 'welcome':
                     is_available = True
                 else:
                     asyncio.sleep(1)
                     continue
             if not is_added:
                 params = {
                     "id": utils.get_req_id(),
                     "type": "subscribe",
                     "topic":
                     f"/spotMarket/level2Depth5:{','.join(self.channels)}",
                     "privateChannel": False,
                     "response": True
                 }
                 req = json.dumps(params)
                 await websocket.send(req)
                 is_added = True
             resp = await websocket.recv()
             data = json.loads(resp)
             if 'subject' in data and data['subject'] == 'level2':
                 self.notify(data)
             else:
                 logger.warning("unknown data %s", data)