Skip to content

Commit

Permalink
HitBTC: Fix Balance Updates for USD and all tests for USD/USDT
Browse files Browse the repository at this point in the history
  • Loading branch information
TheHolyRoger committed Apr 6, 2021
1 parent dd9cc4a commit 29ec54d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
3 changes: 2 additions & 1 deletion hummingbot/connector/exchange/hitbtc/hitbtc_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from hummingbot.connector.exchange.hitbtc.hitbtc_utils import (
convert_from_exchange_trading_pair,
convert_to_exchange_trading_pair,
translate_asset,
get_new_client_order_id,
aiohttp_response_with_errors,
retry_sleep_time,
Expand Down Expand Up @@ -740,7 +741,7 @@ def _process_balance_message(self, balance_update):
local_asset_names = set(self._account_balances.keys())
remote_asset_names = set()
for account in balance_update:
asset_name = account["currency"]
asset_name = translate_asset(account["currency"])
self._account_available_balances[asset_name] = Decimal(str(account["available"]))
self._account_balances[asset_name] = Decimal(str(account["reserved"])) + Decimal(str(account["available"]))
remote_asset_names.add(asset_name)
Expand Down
28 changes: 16 additions & 12 deletions hummingbot/connector/exchange/hitbtc/hitbtc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,22 @@ def split_trading_pair(trading_pair: str) -> Optional[Tuple[str, str]]:
return None


def translate_tokens(hb_trading_pair: str) -> str:
token_replacements = [
def translate_asset(asset_name: str) -> str:
asset_replacements = [
("USD", "USDT"),
]
tokens = hb_trading_pair.split('-')
for token_replacement in token_replacements:
for x in range(len(tokens)):
for inv in [0, 1]:
if tokens[x] == token_replacement[inv]:
tokens[x] = token_replacement[(0 if inv else 1)]
break
return '-'.join(tokens)
for asset_replacement in asset_replacements:
for inv in [0, 1]:
if asset_name == asset_replacement[inv]:
return asset_replacement[(0 if inv else 1)]
return asset_name


def translate_assets(hb_trading_pair: str) -> str:
assets = hb_trading_pair.split('-')
for x in range(len(assets)):
assets[x] = translate_asset(assets[x])
return '-'.join(assets)


def convert_from_exchange_trading_pair(ex_trading_pair: str) -> Optional[str]:
Expand All @@ -77,12 +81,12 @@ def convert_from_exchange_trading_pair(ex_trading_pair: str) -> Optional[str]:
return None
# HitBTC uses uppercase (BTCUSDT)
base_asset, quote_asset = split_trading_pair(ex_trading_pair)
return translate_tokens(f"{base_asset.upper()}-{quote_asset.upper()}")
return translate_assets(f"{base_asset.upper()}-{quote_asset.upper()}")


def convert_to_exchange_trading_pair(hb_trading_pair: str) -> str:
# HitBTC uses uppercase (BTCUSDT)
return translate_tokens(hb_trading_pair).replace("-", "").upper()
return translate_assets(hb_trading_pair).replace("-", "").upper()


def get_new_client_order_id(is_buy: bool, trading_pair: str) -> str:
Expand Down
8 changes: 4 additions & 4 deletions test/connector/exchange/hitbtc/test_hitbtc_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class HitbtcExchangeUnitTest(unittest.TestCase):
]
connector: HitbtcExchange
event_logger: EventLogger
trading_pair = "BTC-USD"
trading_pair = "BTC-USDT"
base_token, quote_token = trading_pair.split("-")
stack: contextlib.ExitStack

Expand Down Expand Up @@ -159,7 +159,7 @@ def test_buy_and_sell(self):
self.assertEqual(order_id, order_completed_event.order_id)
self.assertEqual(amount, order_completed_event.base_asset_amount)
self.assertEqual("BTC", order_completed_event.base_asset)
self.assertEqual("USD", order_completed_event.quote_asset)
self.assertEqual("USDT", order_completed_event.quote_asset)
self.assertAlmostEqual(base_amount_traded, order_completed_event.base_asset_amount)
self.assertAlmostEqual(quote_amount_traded, order_completed_event.quote_asset_amount)
self.assertGreater(order_completed_event.fee_amount, Decimal(0))
Expand Down Expand Up @@ -189,7 +189,7 @@ def test_buy_and_sell(self):
self.assertEqual(order_id, order_completed_event.order_id)
self.assertEqual(amount, order_completed_event.base_asset_amount)
self.assertEqual("BTC", order_completed_event.base_asset)
self.assertEqual("USD", order_completed_event.quote_asset)
self.assertEqual("USDT", order_completed_event.quote_asset)
self.assertAlmostEqual(base_amount_traded, order_completed_event.base_asset_amount)
self.assertAlmostEqual(quote_amount_traded, order_completed_event.quote_asset_amount)
self.assertGreater(order_completed_event.fee_amount, Decimal(0))
Expand Down Expand Up @@ -280,7 +280,7 @@ def test_order_quantized_values(self):

# Make sure there's enough balance to make the limit orders.
self.assertGreater(self.connector.get_balance("BTC"), Decimal("0.0005"))
self.assertGreater(self.connector.get_balance("USD"), Decimal("10"))
self.assertGreater(self.connector.get_balance("USDT"), Decimal("10"))

# Intentionally set some prices with too many decimal places s.t. they
# need to be quantized. Also, place them far away from the mid-price s.t. they won't
Expand Down
10 changes: 5 additions & 5 deletions test/connector/exchange/hitbtc/test_hitbtc_order_book_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class HitbtcOrderBookTrackerUnitTest(unittest.TestCase):
OrderBookEvent.TradeEvent
]
trading_pairs: List[str] = [
"BTC-USD",
"ETH-USD",
"BTC-USDT",
"ETH-USDT",
]

@classmethod
Expand Down Expand Up @@ -87,7 +87,7 @@ def test_tracker_integrity(self):
# Wait 5 seconds to process some diffs.
self.ev_loop.run_until_complete(asyncio.sleep(5.0))
order_books: Dict[str, OrderBook] = self.order_book_tracker.order_books
eth_usd: OrderBook = order_books["ETH-USD"]
eth_usd: OrderBook = order_books["ETH-USDT"]
self.assertIsNot(eth_usd.last_diff_uid, 0)
self.assertGreaterEqual(eth_usd.get_price_for_volume(True, 10).result_price,
eth_usd.get_price(True))
Expand All @@ -96,8 +96,8 @@ def test_tracker_integrity(self):

def test_api_get_last_traded_prices(self):
prices = self.ev_loop.run_until_complete(
HitbtcAPIOrderBookDataSource.get_last_traded_prices(["BTC-USD", "LTC-BTC"]))
HitbtcAPIOrderBookDataSource.get_last_traded_prices(["BTC-USDT", "LTC-BTC"]))
for key, value in prices.items():
print(f"{key} last_trade_price: {value}")
self.assertGreater(prices["BTC-USD"], 1000)
self.assertGreater(prices["BTC-USDT"], 1000)
self.assertLess(prices["LTC-BTC"], 1)
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class HitbtcUserStreamTrackerUnitTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.ev_loop: asyncio.BaseEventLoop = asyncio.get_event_loop()
cls.trading_pairs = ["BTC-USD"]
cls.trading_pairs = ["BTC-USDT"]
cls.user_stream_tracker: HitbtcUserStreamTracker = HitbtcUserStreamTracker(
hitbtc_auth=HitbtcAuth(cls.api_key, cls.api_secret),
trading_pairs=cls.trading_pairs)
Expand Down

0 comments on commit 29ec54d

Please sign in to comment.