-
Notifications
You must be signed in to change notification settings - Fork 5
/
balances_as_btc.py
executable file
·67 lines (48 loc) · 2.33 KB
/
balances_as_btc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
from collections import Counter
from typing import Counter as TCounter
import click
from bitshares.account import Account
from bitsharesscripts.decorators import chain, common_options
from bitsharesscripts.functions import transform_asset
@click.command()
@common_options
@chain
@click.pass_context
def main(ctx):
"""Summarize all assets on all accounts and show BTC equivalent."""
sum_balances: TCounter[str] = Counter()
for acc in ctx.config['my_accounts']:
ctx.log.debug('Calculating balances on account {}'.format(acc))
account = Account(acc, bitshares_instance=ctx.bitshares)
# Avail balances
for i in account.balances:
asset = i['symbol']
sum_balances[asset] += i['amount']
# Balance in orders
for order in account.openorders:
asset = order['for_sale']['symbol']
sum_balances[asset] += order['for_sale']['amount']
# Margin positions
for asset, details in account.callpositions.items():
sum_balances[asset] -= details['debt']['amount']
asset = details['collateral']['asset']['symbol']
sum_balances[asset] += details['collateral']['amount']
for asset, amount in sum_balances.items():
ctx.log.debug('Total: {} {}'.format(asset, amount))
if ctx.config['btc_asset'] not in sum_balances:
# Prevent RuntimeError: dictionary changed size during iteration
sum_balances[ctx.config['btc_asset']] = 0
for asset in ctx.config.get('ignore_assets', []):
ctx.log.debug('Ignoring asset {}'.format(asset))
sum_balances.pop(asset)
for from_asset, to_asset in ctx.config['transform_assets'].items():
ctx.log.debug('Transforming {} to {}'.format(from_asset, to_asset))
sum_balances = transform_asset(ctx.bitshares, sum_balances, from_asset, to_asset)
for asset, amount in sum_balances.items():
if amount > 0 and asset != ctx.config['btc_asset']:
ctx.log.info('Using direct conversion {:.8f} {} -> {}'.format(amount, asset, ctx.config['btc_asset']))
sum_balances = transform_asset(ctx.bitshares, sum_balances, asset, ctx.config['btc_asset'])
print('Accounts value in {}: {:.8f}'.format(ctx.config['btc_asset'], sum_balances[ctx.config['btc_asset']]))
if __name__ == '__main__':
main()