From 49bb6860406299e685db7471de3653e37143f651 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sun, 12 Mar 2023 07:37:46 +0000 Subject: [PATCH] Fix parsing the dashboard open trades response --- core/lib/money.py | 89 +++++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 38 deletions(-) diff --git a/core/lib/money.py b/core/lib/money.py index 4fb1042..b5f8b23 100644 --- a/core/lib/money.py +++ b/core/lib/money.py @@ -394,52 +394,65 @@ class Money(object): async def open_trades_usd_parse_dash(self, dash, rates): cumul_usd = 0 - for _, contact in dash.items(): - # We need created at in order to look up the historical prices - created_at = contact["data"]["created_at"] - - # Reformat the date how CoinGecko likes - # 2022-05-02T11:17:14+00:00 - if "+" in created_at: - date_split = created_at.split("+") - date_split[1].replace(".", "") - date_split[1].replace(":", "") - created_at = "+".join(date_split) - date_parsed = datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%S%z") - else: - date_parsed = datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%S.%fZ") - - date_formatted = date_parsed.strftime("%d-%m-%Y") - - # Get the historical rates for the right asset, extract the price - asset = contact["data"]["advertisement"]["asset"] + cache = {} + async with AsyncCoinGeckoAPISession() as cg: + for _, contact in dash.items(): + # We need created at in order to look up the historical prices + created_at = contact["data"]["created_at"] + + # Reformat the date how CoinGecko likes + # 2022-05-02T11:17:14+00:00 + if "+" in created_at: + date_split = created_at.split("+") + date_split[1].replace(".", "") + date_split[1].replace(":", "") + created_at = "+".join(date_split) + date_parsed = datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%S%z") + else: + date_parsed = datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%S.%fZ") + + date_formatted = date_parsed.strftime("%d-%m-%Y") + + # Get the historical rates for the right asset, extract the price + asset = contact["data"]["advertisement"]["asset"] - async with AsyncCoinGeckoAPISession() as cg: if asset == "XMR": amount_crypto = contact["data"]["amount_xmr"] - history = await cg.get_coin_history_by_id( - coin_id="monero", date=date_formatted - ) - if "market_data" not in history: - return False + if (asset, date_formatted) in cache: + history = cache[(asset, date_formatted)] + else: + history = await cg.get_coin_history_by_id( + coin_id="monero", date=date_formatted + ) + if "market_data" not in history: + return False crypto_usd = float(history["market_data"]["current_price"]["usd"]) elif asset == "BTC": amount_crypto = contact["data"]["amount_btc"] - history = await cg.get_coin_history_by_id( - coin_id="bitcoin", date=date_formatted - ) + if (asset, date_formatted) in cache: + history = cache[(asset, date_formatted)] + else: + history = await cg.get_coin_history_by_id( + coin_id="bitcoin", date=date_formatted + ) + if "market_data" not in history: + return False crypto_usd = float(history["market_data"]["current_price"]["usd"]) - # Convert crypto to fiat - amount = float(amount_crypto) * crypto_usd - currency = contact["data"]["currency"] - if not contact["data"]["is_selling"]: - continue - if currency == "USD": + if (asset, date_formatted) not in cache: + cache[(asset, date_formatted)] = history + # Convert crypto to USD + amount = float(amount_crypto) * crypto_usd + # currency = contact["data"]["currency"] + if not contact["data"]["is_selling"]: + continue cumul_usd += float(amount) - else: - rate = rates[currency] - amount_usd = float(amount) / rate - cumul_usd += amount_usd + # else: + # rate = rates[currency] + # print("RATE", rate) + # print("AMOUNT", amount) + # amount_usd = float(amount) / rate + # print("AMOUJT USD", amount_usd) + # cumul_usd += amount_usd return cumul_usd async def gather_dashboards(self, platforms):