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,52 +394,65 @@ 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
for _, contact in dash.items(): cache = {}
# We need created at in order to look up the historical prices async with AsyncCoinGeckoAPISession() as cg:
created_at = contact["data"]["created_at"] for _, contact in dash.items():
# We need created at in order to look up the historical prices
# Reformat the date how CoinGecko likes created_at = contact["data"]["created_at"]
# 2022-05-02T11:17:14+00:00
if "+" in created_at: # Reformat the date how CoinGecko likes
date_split = created_at.split("+") # 2022-05-02T11:17:14+00:00
date_split[1].replace(".", "") if "+" in created_at:
date_split[1].replace(":", "") date_split = created_at.split("+")
created_at = "+".join(date_split) date_split[1].replace(".", "")
date_parsed = datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%S%z") date_split[1].replace(":", "")
else: created_at = "+".join(date_split)
date_parsed = datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%S.%fZ") date_parsed = datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%S%z")
else:
date_formatted = date_parsed.strftime("%d-%m-%Y") date_parsed = datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%S.%fZ")
# Get the historical rates for the right asset, extract the price date_formatted = date_parsed.strftime("%d-%m-%Y")
asset = contact["data"]["advertisement"]["asset"]
# Get the historical rates for the right asset, extract the price
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"]
history = await cg.get_coin_history_by_id( if (asset, date_formatted) in cache:
coin_id="monero", date=date_formatted history = cache[(asset, date_formatted)]
) else:
if "market_data" not in history: history = await cg.get_coin_history_by_id(
return False coin_id="monero", 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"])
elif asset == "BTC": elif asset == "BTC":
amount_crypto = contact["data"]["amount_btc"] amount_crypto = contact["data"]["amount_btc"]
history = await cg.get_coin_history_by_id( if (asset, date_formatted) in cache:
coin_id="bitcoin", date=date_formatted 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"]) crypto_usd = float(history["market_data"]["current_price"]["usd"])
# Convert crypto to fiat if (asset, date_formatted) not in cache:
amount = float(amount_crypto) * crypto_usd cache[(asset, date_formatted)] = history
currency = contact["data"]["currency"] # Convert crypto to USD
if not contact["data"]["is_selling"]: amount = float(amount_crypto) * crypto_usd
continue # currency = contact["data"]["currency"]
if currency == "USD": if not contact["data"]["is_selling"]:
continue
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