async def get_snapshot(client: aiohttp.ClientSession, trading_pair: str, limit: int = 1000) -> Dict[str, Any]: original_trading_pair: str = trading_pair params: Dict[str, str] = {"count": str(limit), "pair": convert_to_exchange_trading_pair(trading_pair)} if limit != 0 \ else {"pair": convert_to_exchange_trading_pair(trading_pair)} async with client.get(SNAPSHOT_REST_URL, params=params) as response: response: aiohttp.ClientResponse = response if response.status != 200: raise IOError( f"Error fetching Kraken market snapshot for {original_trading_pair}. " f"HTTP status is {response.status}.") response_json = await response.json() if len(response_json["error"]) > 0: raise IOError( f"Error fetching Kraken market snapshot for {original_trading_pair}. " f"Error is {response_json['error']}.") data: Dict[str, Any] = next(iter(response_json["result"].values())) data = {"trading_pair": trading_pair, **data} data["latest_update"] = max( [*map(lambda x: x[2], data["bids"] + data["asks"])], default=0.) # Need to add the symbol into the snapshot message for the Kafka message queue. # Because otherwise, there'd be no way for the receiver to know which market the # snapshot belongs to. return data
async def get_snapshot( cls, rest_assistant: RESTAssistant, trading_pair: str, limit: int = 1000, throttler: Optional[AsyncThrottler] = None, ) -> Dict[str, Any]: throttler = throttler or cls._get_throttler_instance() original_trading_pair: str = trading_pair if limit != 0: params = { "count": str(limit), "pair": convert_to_exchange_trading_pair(trading_pair) } else: params = {"pair": convert_to_exchange_trading_pair(trading_pair)} async with throttler.execute_task(CONSTANTS.SNAPSHOT_PATH_URL): url = f"{CONSTANTS.BASE_URL}{CONSTANTS.SNAPSHOT_PATH_URL}" request = RESTRequest(method=RESTMethod.GET, url=url, params=params) response = await rest_assistant.call(request) if response.status != 200: raise IOError( f"Error fetching Kraken market snapshot for {original_trading_pair}. " f"HTTP status is {response.status}.") response_json = await response.json() if len(response_json["error"]) > 0: raise IOError( f"Error fetching Kraken market snapshot for {original_trading_pair}. " f"Error is {response_json['error']}.") data: Dict[str, Any] = next(iter(response_json["result"].values())) data = {"trading_pair": trading_pair, **data} data["latest_update"] = max( [*map(lambda x: x[2], data["bids"] + data["asks"])], default=0.) return data
def get_mid_price(trading_pair: str) -> Optional[Decimal]: from hummingbot.connector.exchange.kraken.kraken_utils import convert_to_exchange_trading_pair KRAKEN_PRICE_URL = "https://api.kraken.com/0/public/Ticker?pair=" k_pair = convert_to_exchange_trading_pair(trading_pair) resp = requests.get(url=KRAKEN_PRICE_URL + k_pair) resp_json = resp.json() if len(resp_json["error"]) == 0: for record in resp_json["result"]: # assume only one pair is received record = resp_json["result"][record] result = (Decimal(record["a"][0]) + Decimal(record["b"][0])) / Decimal("2") return result
async def get_snapshot( cls, client: aiohttp.ClientSession, trading_pair: str, limit: int = 1000, throttler: Optional[AsyncThrottler] = None, ) -> Dict[str, Any]: throttler = throttler or cls._get_throttler_instance() original_trading_pair: str = trading_pair if limit != 0: params = { "count": str(limit), "pair": convert_to_exchange_trading_pair(trading_pair) } else: params = {"pair": convert_to_exchange_trading_pair(trading_pair)} async with throttler.execute_task(CONSTANTS.SNAPSHOT_PATH_URL): url = f"{CONSTANTS.BASE_URL}{CONSTANTS.SNAPSHOT_PATH_URL}" async with client.get(url, params=params) as response: if response.status != 200: raise IOError( f"Error fetching Kraken market snapshot for {original_trading_pair}. " f"HTTP status is {response.status}.") response_json = await response.json() if len(response_json["error"]) > 0: raise IOError( f"Error fetching Kraken market snapshot for {original_trading_pair}. " f"Error is {response_json['error']}.") data: Dict[str, Any] = next(iter(response_json["result"].values())) data = {"trading_pair": trading_pair, **data} data["latest_update"] = max( [*map(lambda x: x[2], data["bids"] + data["asks"])], default=0.) # Need to add the symbol into the snapshot message for the Kafka message queue. # Because otherwise, there'd be no way for the receiver to know which market the # snapshot belongs to. return data
async def get_ws_subscription_message(self, subscription_type: str): # all_markets: pd.DataFrame = await self.get_active_exchange_markets() trading_pairs: List[str] = [] for tp in self._trading_pairs: trading_pairs.append(convert_to_exchange_trading_pair(tp, '/')) ws_message_dict: Dict[str, Any] = {"event": "subscribe", "pair": trading_pairs, "subscription": {"name": subscription_type, "depth": 1000}} ws_message: str = ujson.dumps(ws_message_dict) return ws_message
async def get_ws_subscription_message(self, subscription_type: str): trading_pairs: List[str] = [] for tp in self._trading_pairs: trading_pairs.append(convert_to_exchange_trading_pair(tp, '/')) ws_message: WSJSONRequest = WSJSONRequest({ "event": "subscribe", "pair": trading_pairs, "subscription": { "name": subscription_type, "depth": 1000 } }) return ws_message
def test_pair_convesion(self): for pair in self.market.trading_rules: exchange_pair = convert_to_exchange_trading_pair(pair) self.assertTrue(exchange_pair in self.market.order_books)