Skip to content

Instantly share code, notes, and snippets.

@JasonCrowe
Created October 16, 2018 12:58
Show Gist options
  • Save JasonCrowe/4507c213e4792a30b4c593d3d5c5fcb6 to your computer and use it in GitHub Desktop.
Save JasonCrowe/4507c213e4792a30b4c593d3d5c5fcb6 to your computer and use it in GitHub Desktop.
Balance bitcoins by selling the higher to the lower
coins = [
{'name': 'ETH', 'qty': 0.81945672, 'price': 215.15},
{'name': 'USDT', 'qty': 125.99, 'price': .99},
{'name': 'BTC', 'qty': 0.02497291, 'price': 6895.15},
{'name': 'BNB', 'qty': 20.91476451, 'price': 10.25},
]
def generate_balance(coins, prec=8):
min = 999*999
max = 0
for coin in coins:
value = coin['qty'] * coin['price']
if value > max:
max = value
max_price = coin['price']
if value < min:
min = value
min_price = coin['price']
for coin in coins:
if min == (coin['qty'] * coin['price']):
lo = coin['name']
elif max == (coin['qty'] * coin['price']):
hi = coin['name']
sell_qty = round((((max - min) / 2) / max_price), prec)
return(hi, sell_qty, lo)
print(generate_balance(coins))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment