This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
market_cap.py
119 lines (100 loc) · 2.95 KB
/
market_cap.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
market_cap.py
View the market capitalization of your coinex account.
Uses bitstamp prices.
OUTPUT:
xxxx BTC
xxxx USD
"""
import urllib.request
import models
import json
import sys
from decimal import *
from urllib.error import URLError, HTTPError
# set the decimal precision to 8
getcontext().prec = 8
def get_bitstamp_price():
"""
Get the price from bitstamp.
Returns in units of USD/BTC
"""
req = urllib.request.urlopen('https://www.bitstamp.net/api/ticker/')
ret = req.read().decode()
ret = json.loads(ret)
return Decimal(ret['last'])
def get_balances():
"""
Memoized method to get a list of all balances.
Also, for our purposes the total amount owned should include
held, so do that.
"""
if not hasattr(get_balances, '_balances'):
bals = models.Wallet.get_balances()
for bal in bals:
bal.amount += bal.held
bal.held = Decimal(0)
get_balances._balances = bals
return get_balances._balances
def get_amt_in_btc(bal):
"""
Get a Decimal representing the BTC value of the given Balance.
Converts using an exchange if needed.
Bal - a models.Balance object
"""
if bal.currency.abbreviation == 'BTC':
return bal.amount
# we need to convert. find an appropriate exchange
for exchange in models.Exchange.get_all():
to_correct = exchange.to_currency == bal.currency
from_correct = exchange.from_currency.abbreviation == 'BTC'
if to_correct and from_correct:
break
else:
raise ValueError(
'An exchange was not found that can convert {0} to BTC'.format(
bal.currency.abbreviation
)
)
ordr = exchange.get_highest_bid()
return Decimal(bal.amount * ordr.rate)
def get_amt_in_usd(btc):
"""
Get an amount in USD as a decimal
btc - a Decimal representing bitcoin amount
returns a Decimal of usd
"""
return Decimal(btc * get_bitstamp_price())
def main():
"""
Get the market cap!
"""
btc = Decimal(0)
try:
for balance in get_balances():
try:
btc += get_amt_in_btc(balance)
except ValueError:
sys.stderr.write(
'WARNING: Cannot convert {0} to btc\n'.format(
balance.currency.abbreviation
)
)
# there isn't an exchange for this coin to BTC, ignore it
pass
except HTTPError as e:
print("Error: status code {0}".format(e.code))
print("Is coinex down?")
return
btc_str = '{0:12.8f}'.format(btc)
print('{0} BTC'.format(btc_str))
try:
usd = get_amt_in_usd(btc)
except HTTPError as e:
print("Error: status code {0}".format(e.code))
print("Is bitstramp down?")
return
usd_str = '{0:12.8f}'.format(usd)
print('{0} USD'.format(usd_str))
if __name__ == '__main__':
main()