def test_no_feed_available(self): ExchangePriceManager.set_exchanges_to_feed(['coinbase_pro'], False) ExchangePriceManager.start() self.ev_loop.run_until_complete(ExchangePriceManager.wait_til_ready()) eth_btc_price = ExchangePriceManager.get_price("ETH", "BTC") self.assertEqual(None, eth_btc_price) ExchangePriceManager.stop()
def convert_token_value(self, amount: float, from_currency: str, to_currency: str, source: str = None) -> float: """ Converts a token amount to the amount of another token with equivalent worth :param source: default, config, any :param amount: :param from_currency: :param to_currency: :return: """ if not self._started: self.start() if source is None: rate = ExchangePriceManager.get_price(to_currency, from_currency) if rate is not None: return amount / float(rate) exchange_rate = self.get_exchange_rate(source) from_currency = from_currency.upper() to_currency = to_currency.upper() # assume WETH and ETH are equal value if from_currency == "ETH" and to_currency == "WETH" or from_currency == "WETH" and to_currency == "ETH" \ or from_currency == to_currency: return amount from_currency_usd_rate = exchange_rate.get(from_currency.upper(), NaN) to_currency_usd_rate = exchange_rate.get(to_currency.upper(), NaN) if math.isnan(from_currency_usd_rate) or math.isnan( to_currency_usd_rate): raise ValueError( f"Unable to convert '{from_currency}' to '{to_currency}'. Aborting." ) return amount * from_currency_usd_rate / to_currency_usd_rate
def test_use_binance_when_none_supported(self): ExchangePriceManager.set_exchanges_to_feed(['coinbase_pro']) ExchangePriceManager.start() self.ev_loop.run_until_complete(ExchangePriceManager.wait_til_ready()) self.assertTrue(BinancePriceFeed.get_instance()._ready_event.is_set()) self.assertEqual(len(BinancePriceFeed.get_instance().price_dict), 3) eth_btc_price = ExchangePriceManager.get_price("ETH", "BTC") self.assertEqual(Decimal("0.025"), eth_btc_price) ExchangePriceManager.stop()
def test_binance_mid_price(self): ExchangePriceManager.set_exchanges_to_feed(['binance'], False) ExchangePriceManager.start() self.ev_loop.run_until_complete(ExchangePriceManager.wait_til_ready()) expected = (Decimal(FixtureExchangePrices.BINANCE[0]["bidPrice"]) + Decimal(FixtureExchangePrices.BINANCE[0]["askPrice"]) ) / Decimal("2") eth_btc_price = ExchangePriceManager.get_price("ETH", "BTC") self.assertEqual(expected, eth_btc_price) ExchangePriceManager.stop()
def test_liquid_mid_price(self): ExchangePriceManager.set_exchanges_to_feed(['liquid'], False) ExchangePriceManager.start() self.ev_loop.run_until_complete(ExchangePriceManager.wait_til_ready()) expected = (Decimal(FixtureExchangePrices.LIQUID[0]["market_bid"]) + Decimal(FixtureExchangePrices.LIQUID[0]["market_ask"]) ) / Decimal("2") eth_btc_price = ExchangePriceManager.get_price("ETH", "BTC") self.assertEqual(expected, eth_btc_price) ExchangePriceManager.stop()
def test_price_feeds(self): ExchangePriceManager.set_exchanges_to_feed(['binance', 'liquid']) ExchangePriceManager.start() self.ev_loop.run_until_complete(ExchangePriceManager.wait_til_ready()) self.assertTrue(BinancePriceFeed.get_instance()._ready_event.is_set()) self.assertEqual(len(BinancePriceFeed.get_instance().price_dict), 3) eth_btc_price = ExchangePriceManager.get_price("ETH", "BTC") self.assertEqual(Decimal("0.025"), eth_btc_price) # ZRX-USDT is not in the fixture, this should return None zrx_usdt_price = ExchangePriceManager.get_price("ZRX", "USDT") self.assertEqual(None, zrx_usdt_price) # LINK-ETH is only in Liquid Fixture link_eth_price = ExchangePriceManager.get_price("LINK", "ETH") self.assertAlmostEqual(Decimal('0.000575'), link_eth_price) ExchangePriceManager.stop()
def test_kucoin_mid_price(self): ExchangePriceManager.set_exchanges_to_feed(['kucoin'], False) ExchangePriceManager.start() self.ev_loop.run_until_complete(ExchangePriceManager.wait_til_ready()) expected = ( Decimal(FixtureExchangePrices.KUCOIN["data"]["ticker"][0]["buy"]) + Decimal(FixtureExchangePrices.KUCOIN["data"]["ticker"][0]["sell"]) ) / Decimal("2") eth_btc_price = ExchangePriceManager.get_price("ETH", "BTC") self.assertEqual(expected, eth_btc_price) ExchangePriceManager.stop()
def test_all_price_feeds(self): ev_loop = asyncio.get_event_loop() for ex_name in ExchangePriceManager.supported_exchanges: ExchangePriceManager.set_exchanges_to_feed([ex_name]) ExchangePriceManager.start() ev_loop.run_until_complete(ExchangePriceManager.wait_til_ready()) self.assertTrue( len(ExchangePriceManager.ex_feeds[ex_name].price_dict) > 0) for trading_pair, price in ExchangePriceManager.ex_feeds[ ex_name].price_dict.items(): self.assertTrue("-" in trading_pair) self.assertTrue(isinstance(price, Decimal)) ExchangePriceManager.stop()
def exchange_on_validated(value: str): required_exchanges.append(value) ExchangePriceManager.set_exchanges_to_feed([value]) ExchangePriceManager.start()
def taker_market_on_validated(value: str): required_exchanges.append(value) maker_exchange = cross_exchange_market_making_config_map["maker_market"].value ExchangePriceManager.set_exchanges_to_feed([maker_exchange, value]) ExchangePriceManager.start()
def secondary_market_on_validated(value: str): required_exchanges.append(value) primary_exchange = arbitrage_config_map["primary_market"].value ExchangePriceManager.set_exchanges_to_feed([primary_exchange, value]) ExchangePriceManager.start()