Fix parsing the dashboard open trades response

master
Mark Veidemanis 2 years ago
parent fa2a6c9c77
commit 49bb686040
Signed by: m
GPG Key ID: 5ACFCEED46C0904F

@ -394,6 +394,8 @@ class Money(object):
async def open_trades_usd_parse_dash(self, dash, rates): async def open_trades_usd_parse_dash(self, dash, rates):
cumul_usd = 0 cumul_usd = 0
cache = {}
async with AsyncCoinGeckoAPISession() as cg:
for _, contact in dash.items(): for _, contact in dash.items():
# We need created at in order to look up the historical prices # We need created at in order to look up the historical prices
created_at = contact["data"]["created_at"] created_at = contact["data"]["created_at"]
@ -414,9 +416,11 @@ class Money(object):
# Get the historical rates for the right asset, extract the price # Get the historical rates for the right asset, extract the price
asset = contact["data"]["advertisement"]["asset"] asset = contact["data"]["advertisement"]["asset"]
async with AsyncCoinGeckoAPISession() as cg:
if asset == "XMR": if asset == "XMR":
amount_crypto = contact["data"]["amount_xmr"] amount_crypto = contact["data"]["amount_xmr"]
if (asset, date_formatted) in cache:
history = cache[(asset, date_formatted)]
else:
history = await cg.get_coin_history_by_id( history = await cg.get_coin_history_by_id(
coin_id="monero", date=date_formatted coin_id="monero", date=date_formatted
) )
@ -425,21 +429,30 @@ class Money(object):
crypto_usd = float(history["market_data"]["current_price"]["usd"]) crypto_usd = float(history["market_data"]["current_price"]["usd"])
elif asset == "BTC": elif asset == "BTC":
amount_crypto = contact["data"]["amount_btc"] amount_crypto = contact["data"]["amount_btc"]
if (asset, date_formatted) in cache:
history = cache[(asset, date_formatted)]
else:
history = await cg.get_coin_history_by_id( history = await cg.get_coin_history_by_id(
coin_id="bitcoin", date=date_formatted coin_id="bitcoin", date=date_formatted
) )
if "market_data" not in history:
return False
crypto_usd = float(history["market_data"]["current_price"]["usd"]) crypto_usd = float(history["market_data"]["current_price"]["usd"])
# Convert crypto to fiat if (asset, date_formatted) not in cache:
cache[(asset, date_formatted)] = history
# Convert crypto to USD
amount = float(amount_crypto) * crypto_usd amount = float(amount_crypto) * crypto_usd
currency = contact["data"]["currency"] # currency = contact["data"]["currency"]
if not contact["data"]["is_selling"]: if not contact["data"]["is_selling"]:
continue continue
if currency == "USD":
cumul_usd += float(amount) cumul_usd += float(amount)
else: # else:
rate = rates[currency] # rate = rates[currency]
amount_usd = float(amount) / rate # print("RATE", rate)
cumul_usd += amount_usd # print("AMOUNT", amount)
# amount_usd = float(amount) / rate
# print("AMOUJT USD", amount_usd)
# cumul_usd += amount_usd
return cumul_usd return cumul_usd
async def gather_dashboards(self, platforms): async def gather_dashboards(self, platforms):

Loading…
Cancel
Save