Skip to content

Instantly share code, notes, and snippets.

@dantimofte
Last active September 27, 2018 14:26
Show Gist options
  • Save dantimofte/a8090d811eda142d53dd0155d7ac8d42 to your computer and use it in GitHub Desktop.
Save dantimofte/a8090d811eda142d53dd0155d7ac8d42 to your computer and use it in GitHub Desktop.
@ensure_authorized
def _cb_get_balance(self, bot, update, args):
LOGGER.info(f"{update.message.chat.username} : /getbalance {args}")
chat_id = update.message.chat.id
if not self.userdata[chat_id]['getbalance']:
message = f"<pre> please set a list of currencies using /set </pre>"
bot.send_message(chat_id, text=message, parse_mode='HTML')
balances = self.btfx_client.balances()
formated_balances = utils.format_balance(self.userdata[chat_id]['getbalance'], balances)
message = f"<pre>{formated_balances}</pre>"
bot.send_message(chat_id, text=message, parse_mode='HTML')
def format_balance(currencies, balances):
# remove balance for currencies not in users list
balances = [balance for balance in balances if balance['currency'] in currencies]
# determine max width for each column
width_eamt = max([len(bal['amount']) for bal in balances if bal['type'] == 'exchange'])
width_eavl = max([len(bal['available']) for bal in balances if bal['type'] == 'exchange'])
width_tamt = max([len(bal['amount']) for bal in balances if bal['type'] == 'trading'])
width_tavl = max([len(bal['available']) for bal in balances if bal['type'] == 'trading'])
width_damt = max([len(bal['amount']) for bal in balances if bal['type'] == 'deposit'])
width_davl = max([len(bal['available']) for bal in balances if bal['type'] == 'deposit'])
# initialize dictionary from which lines will be obtained
bal_dict = {}
for currency in currencies:
bal_dict[currency] = {
"exchange": {"amount": "0.0", "available": "0.0"},
"trading": {"amount": "0.0", "available": "0.0"},
"deposit": {"amount": "0.0", "available": "0.0"}
}
# update values with real values from balances
for balance in balances:
currency = balance['currency']
ctype = balance['type']
bal_dict[currency][ctype]["amount"] = balance["amount"]
bal_dict[currency][ctype]["available"] = balance["available"]
# build formated message
lines = []
width_exchange = width_eamt + width_eavl + 4
width_trading = width_tamt + width_tavl + 5
width_deposit = width_damt + width_davl + 4
header_line = (
" "
f"{'exchange':^{width_exchange}}"
"||"
f"{'margin':^{width_trading}}"
"||"
f"{'funding':^{width_deposit}}"
"\n"
)
lines.append(header_line)
for curr, val in bal_dict.items():
eamt = Decimal(val['exchange']['amount'])
eavl = Decimal(val['exchange']['available'])
tamt = Decimal(val['trading']['amount'])
tavl = Decimal(val['trading']['available'])
damt = Decimal(val['deposit']['amount'])
davl = Decimal(val['deposit']['available'])
line = (
f"{curr} "
f"{eamt:{width_eamt}} : {eavl:{width_eavl}} || "
f"{tamt:{width_tamt}} : {tavl:{width_tavl}} || "
f"{damt:{width_damt}} : {davl:{width_davl}}\n"
)
lines.append(line)
balances_message = "".join(lines)
return balances_message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment