forked from veox/python3-krakenex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print-available-balances.py
executable file
·64 lines (50 loc) · 1.7 KB
/
print-available-balances.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
#!/usr/bin/env python
# This file is part of krakenex.
# Licensed under the Simplified BSD license. See `examples/LICENSE.txt`.
# Get balance available for trading/withdrawal (not on orders).
#
# NOTE: Assumes regular orders. Margin positions are not taken into account!
#
# FIXME: Also shows how current krakenex usage has too much sugar.
import krakenex
from decimal import Decimal as D
import pprint
k = krakenex.API()
k.load_key('kraken.key')
balance = k.query_private('Balance')
orders = k.query_private('OpenOrders')
balance = balance['result']
orders = orders['result']
newbalance = dict()
for currency in balance:
# remove first symbol ('Z' or 'X'), but not for GNO or DASH
newname = currency[1:] if len(currency) == 4 and currency != "DASH" else currency
newbalance[newname] = D(balance[currency]) # type(balance[currency]) == str
balance = newbalance
for _, o in orders['open'].items():
# remaining volume in base currency
volume = D(o['vol']) - D(o['vol_exec'])
# extract for less typing
descr = o['descr']
# order price
price = D(descr['price'])
pair = descr['pair']
base = pair[:3] if pair != "DASHEUR" else "DASH"
quote = pair[3:] if pair != "DASHEUR" else "EUR"
type_ = descr['type']
if type_ == 'buy':
# buying for quote - reduce quote balance
balance[quote] -= volume * price
elif type_ == 'sell':
# selling base - reduce base balance
balance[base] -= volume
for k, v in balance.items():
# convert to string for printing
if v == D('0'):
s = '0'
else:
s = str(v)
# remove trailing zeros (remnant of being decimal)
s = s.rstrip('0').rstrip('.') if '.' in s else s
#
print(k, s)