Created
January 7, 2018 18:51
-
-
Save talesa/41dcb41d021d311de3690400f66cd262 to your computer and use it in GitHub Desktop.
Script to check balances across exchanges implemented in `ccxt`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import ccxt.async as ccxt | |
from collections import defaultdict | |
exchanges = list() | |
exchanges.append(('binance', apiKey, secret)) | |
exchanges.append(('livecoin', apiKey, secret)) | |
tasks = list() | |
tasks += [ccxt.coinmarketcap({'timeout':100000}).fetch_tickers(params={'limit':0})] | |
tasks += [getattr(ccxt, exchange)({'apiKey': apiKey, 'secret': secret}).fetch_balance() for exchange, apiKey, secret in exchanges] | |
tasks = [asyncio.ensure_future(task) for task in tasks] | |
pending = asyncio.Task.all_tasks() | |
loop = asyncio.get_event_loop() | |
_ = loop.run_until_complete(asyncio.gather(*pending)) | |
coinmarketcap = tasks[0].result() | |
balances = [task.result() for task in tasks[1:]] | |
total_balance = defaultdict(float) | |
for balance in balances: | |
for currency, amount in balance['total'].items(): | |
total_balance[currency] += amount | |
usd_balance = 0.0 | |
missing_currencies = [] | |
for currency, amount in total_balance.items(): | |
if currency+'/USD' in coinmarketcap: | |
usd_balance += coinmarketcap[currency+'/USD']['last']*amount | |
elif amount > 0.0: | |
missing_currencies.append(currency) | |
print('Currencies not on coinmarketcap: {}'.format(missing_currencies)) | |
print('Total balance: ${}'.format(usd_balance)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment