/
cryptocurrency.py
486 lines (422 loc) · 16.5 KB
/
cryptocurrency.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
from quantiphy import Quantity, UnitConversion, UnknownConversion
from inform import Error, warn, comment, cull, conjoin, dedent, plural
__version__ = '0.1.0'
__released__ = '2019-07-12'
class Dollars(Quantity):
units = '$'
form = 'fixed'
prec = 2
show_commas = True
strip_zeros = False
class Currency:
def __init__(self, tokens, price=None, total=None):
self.tokens = Quantity(tokens, self.UNITS)
if total and price:
raise Error('must not specify both total and price.', culprit=self.name())
if not price:
price = 0
if total:
price = total/abs(tokens)
if price < 0:
raise Error('price must not be negative.', culprit=self.name())
self.price = Dollars(price)
def in_tokens(self):
return self.tokens
def in_dollars(self, price=None):
price = self.price if price is None else price
return Dollars(self.tokens * price)
def name(self):
return self.__class__.__name__
def __str__(self):
return self.tokens.render()
def __repr__(self):
name = self.__class__.__name__
if self.price:
return f"{name}({self.tokens}, {self.price})"
return f"{name}({self.tokens})"
def __float__(self):
return float(self.tokens)
@classmethod
def converter(cls, to, data):
try:
# SYMBOL is not unique
#return UnitConversion(to, (cls.SYMBOL, cls.UNITS), data[cls.UNITS][to[-1]])
return UnitConversion(to, cls.UNITS, data[cls.UNITS][to[-1]])
except KeyError as e:
comment(f'missing price in {e}.', culprit=cls.UNITS)
@classmethod
def names(cls):
for s in cls.__subclasses__():
yield s.__name__
@classmethod
def units(cls):
for s in cls.__subclasses__():
yield s.UNITS
@classmethod
def currencies(cls):
for s in sorted(cls.__subclasses__(), key=lambda s: s.__name__):
yield s
@classmethod
def currency(cls, name):
for s in cls.__subclasses__():
if name == s.__name__:
return s
class USD(Currency):
UNITS = 'USD'
NAME = 'US Dollar'
SYMBOL = '$'
one = Quantity(1, UNITS)
class BTC(Currency):
UNITS = 'BTC'
NAME = 'Bitcoin'
SYMBOL = 'Ƀ'
one = Quantity(1, UNITS)
class ETH(Currency):
UNITS = 'ETH'
NAME = 'Ethereum'
SYMBOL = 'Ξ'
one = Quantity(1, UNITS)
class BCH(Currency):
UNITS = 'BCH'
NAME = 'Bitcoin Cash'
SYMBOL = '฿'
one = Quantity(1, UNITS)
class BTG(Currency):
UNITS = 'BTG'
NAME = 'Bitcoin Gold'
SYMBOL = '฿'
one = Quantity(1, UNITS)
class EOS(Currency):
UNITS = 'EOS'
NAME = 'EOS'
SYMBOL = 'Ȅ'
one = Quantity(1, UNITS)
class XLM(Currency):
UNITS = 'XLM'
NAME = 'Stellar Lumens'
SYMBOL = '*'
one = Quantity(1, UNITS)
class ZEC(Currency):
UNITS = 'ZEC'
NAME = 'Zcash'
SYMBOL = 'ⓩ'
one = Quantity(1, UNITS)
class IOT(Currency):
UNITS = 'MIOTA'
NAME = 'IOTA'
SYMBOL = 'ι'
one = Quantity(1, UNITS)
class ADA(Currency):
UNITS = 'ADA'
NAME = 'Cardano'
SYMBOL = 'ℂ'
one = Quantity(1, UNITS)
# @classmethod
# def converter(cls, to, data):
# # # cannot convert this to '$' directly using cryptocompare.
# # # instead, use ETH as intermediary
# conversion = data[cls.UNITS]['ETH'] * data['ETH'][to[-1]]
# return UnitConversion(to, (cls.SYMBOL, cls.UNITS), conversion)
class BLACK(Currency):
UNITS = 'BLACK'
NAME = 'EOS Black'
one = Quantity(1, UNITS)
@classmethod
def converter(cls, to, data):
# cannot convert this to '$' directly using cryptocompare.
# instead, use EOS as intermediary
conversion = data[cls.UNITS]['EOS'] * data['EOS'][to[-1]]
units = getattr(cls, 'UNITS', None)
symbol = getattr(cls, 'SYMBOL', None)
return UnitConversion(to, cull([symbol, units]), conversion)
class HORUS(Currency):
UNITS = 'HORUS'
NAME = 'Horus Pay'
one = Quantity(1, UNITS)
@classmethod
def converter(cls, to, data):
# cannot convert this to '$' directly using cryptocompare.
# instead, use EOS as intermediary
conversion = data[cls.UNITS]['EOS'] * data['EOS'][to[-1]]
units = getattr(cls, 'UNITS', None)
symbol = getattr(cls, 'SYMBOL', None)
return UnitConversion(to, cull([symbol, units]), conversion)
class IQ(Currency):
UNITS = 'IQ'
NAME = 'Everipedia'
one = Quantity(1, UNITS)
class CET(Currency):
UNITS = 'CET'
NAME = 'CoinEx'
one = Quantity(1, UNITS)
class KARMA(Currency):
UNITS = 'KARMA'
NAME = 'Karma'
one = Quantity(1, UNITS)
accounts = {}
class Account:
def __init__(self, owner, default=True):
self.owner = owner
self.default = default
self.transactions = {}
accounts[owner] = self
self.totals = {} # total value in dollars by token
self.costs = {} # total cost in dollars by token
self.purchased = {} # number of tokens actually purchased by token
class Transaction:
def __init__(self, tokens, date, comment, fees, source):
self.tokens = tokens
self.proceeds = tokens.in_dollars().scale(-1)
self.date = date
self.comment = comment
self.fees = Dollars(fees)
self.source = source
self.tokens_reported = 0
self.cost_basis = None
def compute_cost_basis(self):
source = self.source
tokens = self.tokens
date = self.date
if source and type(source) != list:
source = [source]
if tokens.in_tokens() > 0:
# this is a purchase
if not source:
if self.proceeds == 0:
warn(
'purchase with no cost basis.',
culprit = self.ident()
)
self.cost_basis = self.proceeds.add(self.fees)
self.cost_basis_remaining = self.cost_basis
return
# I'm not sure why this limitation exists
# if self.proceeds != 0:
# raise Error(
# 'cannot specify both an price and a source on a purchase.',
# culprit = self.ident()
# )
basis = Dollars(0)
for src in source:
try:
src, fraction = src
except TypeError:
fraction = 1
assert 0 <= fraction <= 1
if src.cost_basis is None:
raise Error(
f'source transaction ({src.ident()}) has no cost basis.',
culprit = self.ident()
)
if fraction:
if src.proceeds < 0:
raise Error(
f'a purchase ({src.ident()})',
'cannot be used as a source for a purchase.',
culprit = self.ident()
)
# if src.proceeds < 0:
# raise Error(
# f'using already reported asset ({src.ident()})',
# 'as source for purchase.',
# culprit = self.ident()
# )
cost_basis_used = src.cost_basis*fraction
basis = basis.add(cost_basis_used)
src.cost_basis_remaining = src.cost_basis_remaining.add(-cost_basis_used)
self.cost_basis = basis.add(self.fees)
self.cost_basis_remaining = self.cost_basis
return
# this is a sale
tokens_to_consume = -tokens.in_tokens()
basis = Dollars(0)
if not source:
source = []
for src in source:
try:
src, fraction = src
except TypeError:
fraction = 1
assert 0 <= fraction <= 1
if fraction and src.proceeds > 0:
raise Error(
f'a sale ({src.ident()})',
'cannot be used as a source for a sale.',
culprit = self.ident()
)
if tokens.name() != src.tokens.name():
raise Error(
'token type differs from source',
f'({src.tokens.name()}).',
culprit = self.ident()
)
if tokens_to_consume <= 0:
warn(
f'unneeded source ({src.tokens.name()} {src.date}).',
culprit = self.ident()
)
break
tokens_available = src.tokens_remaining()
if tokens_to_consume <= tokens_available:
# take only tokens we need
tokens_consumed = tokens_to_consume
tokens_to_consume = 0
else:
# take all remaining tokens
tokens_consumed = tokens_available
tokens_to_consume -= tokens_available
src.tokens_reported += tokens_consumed
if tokens_consumed:
cost_basis_used = src.cost_basis_remaining * (
tokens_consumed / tokens_available
)
else:
cost_basis_used = 0
basis = basis.add(cost_basis_used)
src.cost_basis_remaining = src.cost_basis_remaining.add(-cost_basis_used)
if tokens_to_consume > 0.001:
# 0.001 is assumed negligible
if source:
msg = (
'insufficient tokens available in',
f'{plural(source):source}',
f'(short by {tokens_to_consume}).'
)
else:
msg = (f'sale without a source.',)
warn(*msg, culprit = self.ident())
elif tokens_to_consume < -0.001:
raise AssertionError(f'overconsumed {tokens_to_consume}')
else:
self.cost_basis = basis.add(self.fees)
self.cost_basis_remaining = self.cost_basis
if self.proceeds:
self.profit = self.proceeds
if self.cost_basis:
self.profit = self.profit.add(-self.cost_basis)
def fee(self):
return Dollars(self.cost - self.tokens.in_dollars())
def reported(self):
return Dollars(self.tokens_reported * self.tokens.price)
def tokens_remaining(self):
return Quantity(
float(self.tokens) - self.tokens_reported,
self.tokens.tokens
)
def remaining(self):
return Dollars(self.tokens_remaining * self.tokens.price)
def ident(self):
with Quantity.prefs(spacer=''):
return ''.join(cull([self.tokens.name(), self.date])).lower()
def __str__(self):
return f'{self.date} {self.tokens}'
def transaction(self, tokens, date=None, comment='', fees=0, source=None):
t = self.Transaction(tokens, date, comment, fees, source)
ident = t.ident()
assert ident not in self.transactions, f'{ident} not unique'
self.transactions[ident] = t
kind = tokens.name()
num_tokens = float(tokens)
self.totals[kind] = Quantity(
num_tokens + self.totals.get(kind, 0), units=tokens.UNITS
)
self.costs[kind] = Dollars(t.proceeds + self.costs.get(kind, 0))
if t.proceeds > 0:
self.purchased[kind] = Quantity(
num_tokens + self.purchased.get(kind, 0), tokens.UNITS
)
return t
def find_transaction(self, ident):
try:
return self.transactions[ident]
except KeyError:
matches = [k for k in self.transactions.keys() if k.startswith(ident)]
if len(matches) == 1:
return matches[0]
elif matches:
raise Error(
"transaction identifier is not unique, matches:",
*matches,
sep='\n ',
culprit=ident,
)
raise Error('ident not found.', culprit=ident)
def confirm_balance(self, kind, tokens):
actual = self.totals[kind.__name__]
expected = Quantity(tokens, kind.UNITS)
if not actual.is_close(expected):
delta = Quantity(actual - tokens, kind.UNITS)
warn(f'expected {expected}, found {actual}, difference {delta}')
def total_value(self):
"Total value of current holdings in dollars."
total_dollars = 0
for token in self.totals.values():
try:
total_dollars += token.scale('$')
except UnknownConversion:
pass # this will get reported elsewhere
return Dollars(total_dollars)
def total_cost(self):
"Total cost of tokens purchased in dollars."
return Dollars(sum(self.costs.values()))
def largest_share(self):
"Value of currency in dollars with the largest holdings."
max_dollars = 0
for token in self.totals.values():
try:
dollars = token.scale('$')
if dollars >= max_dollars:
max_dollars = dollars
except UnknownConversion:
pass # this will get reported elsewhere
return Dollars(max_dollars)
def gen_csv(self, filename):
import csv
for t in self.transactions.values():
try:
t.compute_cost_basis()
except Error as e:
e.terminate()
with open(filename, 'w', newline='') as csvfile:
fieldnames = 'date coin amount price remaining total comment'.split()
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
def add_row_to_totals(row):
coin = row['coin']
if coin not in totals:
totals[coin] = dict(amount=0, price=Dollars(0), total=Dollars(0))
if row['amount']:
totals[coin]['amount'] += row['amount']
if row['total']:
totals[coin]['total'] = totals[coin]['total'].add(Dollars(row['total']))
if row['price']:
# first half of weighted average computation
totals[coin]['price'] = totals[coin]['price'].add(row['amount']*Dollars(row['price']))
totals = {}
writer.writeheader()
for t in sorted(self.transactions.values(), key=lambda t: t.tokens.price):
if t.tokens.name() != 'BTC':
continue
remaining = t.tokens_remaining()
total = t.tokens.in_dollars()
row = dict(
date = f'{t.date[:2]} {t.date[2:4]} {t.date[4:6]} {t.date[6:]}',
# add spaces to dates so they cannot be confused with numbers
# without the dates without suffixes are treated as
# numbers, which fuxs up the sorting in Libre office
# can't use dashes, can't use slashes
coin = t.tokens.name(),
amount = round(float(t.tokens.in_tokens()), 8),
price = str(t.tokens.price) if t.tokens.price else '',
remaining = round(float(remaining), 8) if remaining >= 0 else '',
total = str(total) if total else '',
comment = dedent(t.comment, strip_nl='b'),
)
writer.writerow(row)
add_row_to_totals(row)
writer.writerow(dict())
writer.writerow(dict(amount='total\ncoins', price='weighted\naverage', total='total\ninvested'))
for coin in sorted(totals):
row = totals[coin]
# complete the weighted average computation
row['price'] = Dollars(row['price']/row['amount'])
writer.writerow({k: str(v) for k, v in row.items()})