async def get_ticks(self): ticks = pd.DataFrame(data=[], columns=formats.tick) try: request_url = 'ticker.do' stock_data = await self.rest_send(request_url, params={ 'symbol': 'all'}) current_date = datetime.datetime.now() for market in stock_data: ticks = ticks.append(pd.Series( data=[ self.name, int(time.mktime(current_date.timetuple()))*1000, market['symbol'], market['ticker']['latest'],], index=formats.tick, name=current_date,)) Logger.log_info(ticks) except Exception as e: Logger.log_error(e) finally: return ticks
async def get_ticks(self): ticks = pd.DataFrame(data=[], columns=formats.tick) try: request_url = 'public/Ticker' stock_data = await self.rest_send( request_url, params={'pair':','.join( self._get_markets().index.values.tolist())},) current_date = datetime.datetime.now() for market_name in stock_data['result'].keys(): market = self._get_markets().loc[market_name] ticks = ticks.append(pd.Series( data=[ self.name, int(time.mktime(current_date.timetuple()))*1000, '_'.join([market.at['base'], market.at['quot']]), stock_data['result'][market_name]['c'][0],], index=formats.tick, name=current_date,)) Logger.log_info(ticks) except Exception as e: Logger.log_error(e) finally: return ticks
def __assume_tick(self, message): tick = None try: current_date = datetime.datetime.now() tick = pd.Series( data=[ self.name, int(time.mktime(current_date.timetuple())) * 1000, '_'.join([ message['data']['symbol1'].lower(), message['data']['symbol2'].lower(), ]), message['data']['price'], ], index=formats.tick, name=current_date, ) Logger.log_info(tick) except Exception as e: Logger.log_error(e) finally: return tick
async def get_ticks(self): ticks = pd.DataFrame(data=[], columns=formats.tick) try: request_url = 'pairs' stock_data = await self.rest_send(request_url) current_date = datetime.datetime.now() for market in stock_data['data']: if market['last_price'] is not None: ticks = ticks.append( pd.Series( data=[ self.name, int(time.mktime(current_date.timetuple())) * 1000, '_'.join([ market['base']['name'].lower(), market['quote']['name'].lower() ]), market['last_price'], ], index=formats.tick, name=current_date, )) Logger.log_info(ticks) except Exception as e: Logger.log_error(e) finally: return ticks
async def get_ticks(self): ticks = pd.DataFrame(data=[], columns=formats.tick) try: request_url = 'ticker_utc' stock_data = await self.rest_send(request_url, params={ 'currency': 'all', 'format': 'json', }) current_date = datetime.datetime.now() for market_name in stock_data.keys(): if isinstance(stock_data[market_name], dict): ticks = ticks.append( pd.Series( data=[ self.name, int(time.mktime(current_date.timetuple())) * 1000, '_'.join([market_name, 'krw']), stock_data[market_name]['last'], ], index=formats.tick, name=current_date, )) Logger.log_info(ticks) except Exception as e: Logger.log_error(e) finally: return ticks
async def get_markets(self): markets = pd.DataFrame(data=[], columns=formats.market) try: request_url = 'currency_limits' stock_data = await self.rest_send(request_url) for market_data in stock_data['data']['pairs']: markets = markets.append( pd.Series( data=[ self.name, market_data['symbol1'], market_data['symbol2'], market_data['minLotSize'], market_data['maxLotSize'], ], index=formats.market, name='_'.join( [market_data['symbol1'], market_data['symbol2']]), )) except Exception as e: Logger.log_error(e) finally: return markets
async def get_markets(self): markets = pd.DataFrame(data=[], columns=formats.market) try: request_url = 'v1/mdata' stock_data = await self.rest_send( request_url, params={ 'cmd': 'pairList', }, ) for market in stock_data['result']: markets = markets.append( pd.Series(data=[ self.name, market['pair'].split('_')[0].lower(), market['pair'].split('_')[1].lower(), None, None, ], index=formats.market, name=market['id'])) except Exception as e: Logger.log_error(e) finally: return markets
async def get_ticks(self): ticks = pd.DataFrame(data=[], columns=formats.tick) try: request_url = 'v1/mdata' stock_data = await self.rest_send( request_url, params={ 'cmd': 'marketAll', }, ) current_date = datetime.datetime.now() for market_data in stock_data['result']: market = self._get_markets().loc[market_data['id']] ticks = ticks.append( pd.Series( data=[ self.name, int(time.mktime(current_date.timetuple())) * 1000, '_'.join([market.at['base'], market.at['quot']]), market_data['last'], ], index=formats.tick, name=current_date, )) Logger.log_info(ticks) except Exception as e: Logger.log_error(e) finally: return ticks
async def get_markets(self): markets = pd.DataFrame(data=[], columns=formats.market) try: request_url = 'common/symbols' stock_data = await self.rest_send(request_url) for market_data in stock_data['data']: markets = markets.append( pd.Series( data=[ self.name, market_data['base-currency'].lower(), market_data['quote-currency'].lower(), None, None, ], index=formats.market, name=(market_data['base-currency'].lower() + market_data['quote-currency'].lower()), )) except Exception as e: Logger.log_error(e) finally: return markets
async def execute(self, action, *args, **kwargs): result = None try: nonce = utils.get_nonce() Logger.log_info('execute') Logger.log_info(nonce) self.__await_events[nonce] = asyncio.Event() await self.push({ 'type':'service', 'id': nonce, 'action':action, 'args':args, 'kwargs':kwargs,}) await self.__await_events[nonce].wait() Logger.log_info('executed') result = self.__events_results[nonce] Logger.log_info(result) del self.__events_results[nonce] del self.__await_events[nonce] except Exception as e: Logger.log_error(e) finally: return result
async def __run(self): try: while True: try: await self.__set_status('connection') self.__markets = await self.get_markets() await self._prepare_ws_connection() async with websockets.connect( self._ws_path, extra_headers=self._ws_headers, ) as websocket: self.__ws_socket = websocket self.__clear_channels() await self._ws_auth() await self._subscribe_channels() async for message in websocket: payload = await self._resolve_message(message) if payload is not None: await self.__set_status('connected') await self.__exchanges_buffer.push(payload) except Exception as e: self.__ws_socket = None Logger.log_error(e) finally: await self.__set_status('disconnected') await asyncio.sleep(1) except Exception as e: Logger.log_error(e)
async def get_markets(self): markets = pd.DataFrame(data=[], columns=formats.market) try: request_url = 'exchangeInfo' exchange_info = await self.rest_send(request_url) for market_data in exchange_info['symbols']: if market_data['status'].lower() == 'trading': market = pd.Series( data=[None, None, None, None, None], index=formats.market, ) market.at['stock'] = self.name market.at['base'] = market_data['baseAsset'].lower() market.at['quot'] = market_data['quoteAsset'].lower() for limit in market_data['filters']: if limit['filterType'].lower() == 'lot_size': market.at['trade_min'] = limit['minQty'] market.at['trade_max'] = limit['maxQty'] market.name = market.at['base'] + market.at['quot'] markets = markets.append(market) except Exception as e: Logger.log_error(e) finally: return markets
async def request(self, request_url, params=None, type_='get'): response = None try: full_url = self.__base_url + request_url if type_ == 'get' and params is not None: full_url += '?' + urllib.parse.urlencode(params) now = datetime.datetime.now() self.__timeline = self.__timeline.loc[now - datetime.timedelta( seconds=REQUEST_INTERVAL_SECONDS):now] if (self.__timeline.loc[:, "request"].count() > self.__request_limit): self.__queue.append(full_url) await asyncio.sleep(REQUEST_INTERVAL_SECONDS + len(self._queue) // self.__request_limit * REQUEST_INTERVAL_SECONDS) if type_ == 'get': response = await self.__process_request(full_url) else: response = await self.__post(full_url, params=params) return response except Exception as e: Logger.log_error(e) finally: return response
async def get_ticks(self): ticks = pd.DataFrame(data=[], columns=formats.tick) try: request_url = 'tickerall' stock_data = await self.rest_send(request_url) current_date = datetime.datetime.now() for market_name in stock_data['data'].keys(): ticks = ticks.append( pd.Series( data=[ self.name, int(time.mktime(current_date.timetuple())) * 1000, market_name, stock_data['data'][market_name]['last'], ], index=formats.tick, name=current_date, )) Logger.log_info(ticks) except Exception as e: Logger.log_error(e) finally: return ticks
async def get_markets(self): markets = pd.DataFrame(data=[], columns=formats.market) try: request_url = 'symbols_details' stock_data = await self.rest_send(request_url) for market_data in stock_data: markets = markets.append( pd.Series( data=[ self.name, market_data['pair'].lower()[:3], market_data['pair'].lower()[3:], market_data['minimum_order_size'], market_data['maximum_order_size'], ], index=formats.market, name=market_data['pair'].lower(), )) except Exception as e: Logger.log_error(e) finally: return markets
async def __process_request(self, full_url): try: request = pd.Series(data=[full_url], index=['request']) request.name = datetime.datetime.now() self.__timeline = self.__timeline.append(request) headers = { 'Accept': ('text/html,application/xhtml+xml' + ',application/xml;q=0.9,image/webp,' + 'image/apng,*/*;q=0.8'), 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-US,en;q=0.9', 'Cache-Control': 'max-age=0', 'Connection': 'keep-alive', 'Upgrade-Insecure-Requests': '1', 'User-Agent': ('Mozilla/5.0 (Macintosh;' + ' Intel Mac OS X 10_11_6) AppleWebKit/537.36 ' + '(KHTML, like Gecko) Chrome/66.0.3359.139' + ' Safari/537.36'), } async with aiohttp.ClientSession(headers=headers) as session: async with session.get(full_url) as resp: text = await resp.text() return utils.parse_data(text) except Exception as e: Logger.log_error(e)
async def get_markets(self): markets = pd.DataFrame(data=[], columns=formats.market) try: request_url = 'products' stock_data = await self.rest_send(request_url) for market_data in stock_data: markets = markets.append( pd.Series( data=[ self.name, market_data['base_currency'].lower(), market_data['quote_currency'].lower(), market_data['min_market_funds'], market_data['max_market_funds'], ], index=formats.market, name=market_data['id'].lower(), )) except Exception as e: Logger.log_error(e) finally: return markets
def __assume_tick(self, tick_data): tick = None try: market = self._get_markets().loc[tick_data['params']['symbol']] tick = pd.Series( data=[ self.name, int( datetime.datetime.strptime( tick_data['params']['timestamp'].split('.')[0], '%Y-%m-%dT%H:%M:%S', ).timestamp() * 1000), '_'.join([market.at['base'].lower(), market.at['quot']]), tick_data['params']['last'], ], index=formats.tick, name=datetime.datetime.now(), ) except Exception as e: Logger.log_info(tick_data['params']['timestamp'].split('.')[0]) Logger.log_error(e) finally: return tick
async def ws_send(self, message): try: if self.__ws_socket is not None: self.__request_counter += 1 await self.__ws_socket.send(json.dumps(message)) except Exception as e: Logger.log_error(e)
async def get_ticks(self): ticks = pd.DataFrame(data=[], columns=formats.tick) try: request_url = 'Api_Market/getPriceList' stock_data = await self.rest_send(request_url) current_date = datetime.datetime.now() for quot_name in stock_data.keys(): for market in stock_data[quot_name]: ticks = ticks.append( pd.Series( data=[ self.name, int(time.mktime(current_date.timetuple())) * 1000, '_'.join( [market['coin_from'], market['coin_to']]), market['current'], ], index=formats.tick, name=current_date, )) Logger.log_info(ticks) except Exception as e: Logger.log_error(e) finally: return ticks
async def get_markets(self): markets = pd.DataFrame(data=[], columns=formats.market) try: request_url = 'markets' stock_data = await self.rest_send(request_url) for market_name in stock_data.keys(): markets = markets.append( pd.Series( data=[ self.name, market_name.split('_')[0], market_name.split('_')[1], None, None, ], index=formats.market, name=market_name.replace('_', ''), )) except Exception as e: Logger.log_error(e) finally: return markets
async def __client_connector(self, pure_websocket, path): try: websocket = Websocket(websocket=pure_websocket) self.__exchanges_buffer.connect(socket=websocket) await websocket.listen() except Exception as e: Logger.log_error(e)
def __assume_channel(self, message): try: self._register_channel( channel_name=message['subbed'], market_name=message['subbed'].split('.')[1], ) except Exception as e: Logger.log_error(e)
async def _ws_auth(self): try: await self.signalr_send( 'GetAuthContext', self._key, callback=self.__sign) except Exception as e: Logger.log_error(e)
async def _subscribe_channels(self): try: for market_name, market in self._get_markets().iterrows(): await self.ws_send({ 'event': 'addChannel', 'channel': '%s_ticker' % (market_name), }) except Exception as e: Logger.log_error(e)
async def __subscribe_channels(self, message): try: if message.get('R', None) == True: #await self.signalr_send('SubscribeToExchangeDeltas', 'BTC-ETH') await self.signalr_send('SubscribeToSummaryLiteDeltas') else: raise Warning(*['Auth failed']*2) except Exception as e: Logger.log_error(e)
def __assume_channel(self, message): try: if message.get('event', None) == 'subscribed': self._register_channel( channel_name=message['chanId'], market_name=message['pair'].lower(), ) except Exception as e: Logger.log_error(e)
async def __set_status(self, status): try: if self.status != status: self.status = status await self.__exchanges_buffer.push({ 'exchange': self.name, 'status': status, }) except Exception as e: Logger.log_error(e)
async def _subscribe_channels(self): try: await self.ws_send( { "id": self._get_request_counter(), "method": "ticker.subscribe", "params": self._get_markets().index.values.tolist() }, ) except Exception as e: Logger.log_error(e)
async def _subscribe_channels(self): try: await self.ws_send({ 'type': 'subscribe', 'product_ids': self._get_markets().index.values.tolist(), 'channels': ['ticker'], }) except Exception as e: Logger.log_error(e)