This repository has been archived by the owner on Jul 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mistbat.py
435 lines (383 loc) · 14.5 KB
/
mistbat.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
import click
import time
import loaders
import yaml
from prettytable import PrettyTable
from xdg import XDG_CONFIG_HOME, XDG_DATA_HOME
from cryptocompare import get_historical_close, get_coin_spot_prices
from events import get_events
from transactions import (
get_transactions,
annotate_transactions,
fmv_transactions,
imply_fees,
)
from tax import Form8949
def print_usd_exposure():
"""Calculate total amount of USD invested and not redeemed and total fees spent."""
fiat_events = get_events(loaders.all, "FiatExchange")
invested = round(sum(ev.sell_amount for ev in fiat_events if ev.investing), 2)
redeemed = round(sum(ev.buy_amount for ev in fiat_events if ev.redeeming), 2)
net_invested = round(invested - redeemed, 2)
fees = round(sum(ev.fee_amount for ev in fiat_events), 2)
print(
"USD Exposure: {} + {} fees (FIAT ONLY) = {:.2f}".format(
net_invested, fees, net_invested + fees
)
)
print("Aggregate Fee %: {:.2f}%".format(fees * 100 / (invested - redeemed)))
@click.group()
def cli():
pass
@cli.command()
@click.option(
"--remote-update",
help="Request updated events from exchange APIs",
is_flag=True,
default=False,
)
def lsev(remote_update):
"""List all events parsed from observations."""
events = get_events(loaders.all, remote_update=remote_update)
for ev in events:
print(ev)
print("--------------------")
print("{} total events".format(len(events)))
print_usd_exposure()
@cli.command()
@click.option(
"--no-group",
help="Only show transactions without a group",
is_flag=True,
default=False,
)
@click.option("--no-annotations", help="Omit annotations", is_flag=True, default=False)
@click.option(
"--minimal", help="Omit everything other than headline", is_flag=True, default=False
)
def lstx(no_group, no_annotations, minimal):
"""List all transactions that have been derived from events and annotated."""
events = get_events(loaders.all)
transactions = get_transactions(events, XDG_CONFIG_HOME + "/mistbat/tx_match.yaml")
if not no_annotations:
transactions = annotate_transactions(
transactions, XDG_CONFIG_HOME + "/mistbat/tx_annotations.yaml"
)
transactions = fmv_transactions(
transactions, XDG_DATA_HOME + "/mistbat/tx_fmv.yaml"
)
transactions = imply_fees(transactions)
if no_group:
transactions = [
tx for tx in transactions if getattr(tx, "groups", None) is None
]
# Print transactions
for tx in transactions:
if minimal:
print(tx)
else:
print(tx.description())
print("--------------------")
print("{} total transactions".format(len(transactions)))
print_usd_exposure()
@cli.command()
def fees():
events = get_events(loaders.all)
transactions = get_transactions(events, XDG_CONFIG_HOME + "/mistbat/tx_match.yaml")
transactions = fmv_transactions(
transactions, XDG_DATA_HOME + "/mistbat/tx_fmv.yaml"
)
transactions = imply_fees(transactions)
print("\nFees Incurred")
print("-------------")
fees = {}
for tx in transactions:
fees[tx.__class__.__name__] = fees.get(tx.__class__.__name__, 0) + tx.fee_usd
for k, v in fees.items():
print(f"{k}: USD {v:0.2f}")
print("TOTAL: USD {:0.2f}\n".format(sum(fees.values())))
print("\nFees Incurred (negative values ignored)")
print("-----------------------------------------")
fees = {}
for tx in transactions:
fees[tx.__class__.__name__] = fees.get(tx.__class__.__name__, 0) + max(
tx.fee_usd, 0
)
for k, v in fees.items():
print(f"{k}: USD {v:0.2f}")
print("TOTAL: USD {:0.2f}\n".format(sum(fees.values())))
@cli.command()
@click.option("--verbose", help="Print progress", is_flag=True, default=False)
def updatefmv(verbose):
"""Update the tx_fmv.yaml file for any missing figures"""
# Load storage file and events and transactions
try:
fmv_raw = yaml.load(open(XDG_DATA_HOME + "/mistbat/tx_fmv.yaml"))
except FileNotFoundError:
fmv_raw = {}
fmv_data = {}
for id in fmv_raw:
fmvs = fmv_raw[id].split(" -- ")
comment = None
if len(fmvs) == 2: # If there is a comment
comment = fmvs[1]
fmvs = fmvs[0].split()
fmvs = {fmv.split("@")[0]: fmv.split("@")[1] for fmv in fmvs}
fmvs["comment"] = comment
fmv_data[id] = fmvs
events = get_events(loaders.all)
transactions = get_transactions(events, XDG_CONFIG_HOME + "/mistbat/tx_match.yaml")
# Identify missing transactions
missing = [tx for tx in transactions if tx.missing_fmv and tx.id not in fmv_data]
# Error-check that stored transactions have necessary FMV info
stored = [tx for tx in transactions if tx.id in fmv_data]
for tx in stored:
stored_coins = set(fmv_data[tx.id].keys())
stored_coins.remove("comment")
if set(tx.affected_coins) != stored_coins:
raise RuntimeError(f"Transaction {tx.id} does not have correct fmv info")
# Confirm that the tx_fmv file doesn't have any unknown tx ids
diff = set(id for id in fmv_data) - set(tx.id for tx in transactions)
diff = ", ".join(diff)
if len(diff) != 0:
raise RuntimeError(
f"Unrecognized transaction ids in tx_fmv.yaml: {diff}. Tip: Dont inlude fiat transaction fmvs."
)
# Fill remaining missing transactions with public closing price
print(f"{len(missing)} missing transactions") if verbose else None
for tx in missing:
print(f"{tx.id}")
fmv_data[tx.id] = {"comment": "from crytpocompare daily close api"}
for coin in tx.affected_coins:
coin_fmv = get_historical_close(coin, int(tx.time.timestamp()))
fmv_data[tx.id][coin] = coin_fmv
print(f"{coin}@{coin_fmv}\n") if verbose else None
time.sleep(0.1)
# Convert fmv_data back into fmv_raw and dump to disk
fmv_raw = {}
for id, coins in fmv_data.items():
comment = coins.pop("comment")
fmv_raw[id] = " ".join(f"{coin}@{price}" for coin, price in coins.items())
if comment:
fmv_raw[id] += " -- " + comment
yaml.dump(
fmv_raw,
open(XDG_DATA_HOME + "/mistbat/tx_fmv.yaml", "w"),
default_flow_style=False,
)
@cli.command()
@click.option(
"--aggregated",
help="Aggregate single dispositions that can be traced to multiple acquisitions",
is_flag=True,
default=False,
)
@click.option(
"--year", help="Limit report to a particular year", is_flag=False, default=None
)
def tax(aggregated, year):
"""Generate the information needed for IRS Form 8949"""
events = get_events(loaders.all)
transactions = get_transactions(events, XDG_CONFIG_HOME + "/mistbat/tx_match.yaml")
transactions = annotate_transactions(
transactions, XDG_CONFIG_HOME + "/mistbat/tx_annotations.yaml"
)
transactions = fmv_transactions(
transactions, XDG_DATA_HOME + "/mistbat/tx_fmv.yaml"
)
transactions = imply_fees(transactions)
form_8949 = Form8949(transactions)
print("SHORT-TERM CAPITAL GAINS")
table = PrettyTable(
[
"(a) Description",
"(b) Date acquired",
"(c) Date sold",
"(d) Proceeds",
"(e) Basis",
"(h) Gain",
]
)
total_gain = 0.00
for line in form_8949.generate_form(term="short", aggregated=aggregated, year=year):
table.add_row(line)
if str(line[-1]).strip():
total_gain += line[-1]
print(table)
print(f"TOTAL SHORT-TERM CAPITAL GAIN: USD {total_gain:0.2f}")
print("\nLONG-TERM CAPITAL GAINS")
table = PrettyTable(
[
"(a) Description",
"(b) Date acquired",
"(c) Date sold",
"(d) Proceeds",
"(e) Basis",
"(h) Gain",
]
)
total_gain = 0.00
for line in form_8949.generate_form(term="long", aggregated=aggregated, year=year):
table.add_row(line)
if str(line[-1]).strip():
total_gain += line[-1]
print(table)
print(f"TOTAL LONG-TERM CAPITAL GAIN: USD {total_gain:0.2f}")
@cli.command()
@click.option(
"--harvest",
help="Add column showing cumulative gain or loss of selling that particular coin",
is_flag=True,
default=False,
)
def currentbasis(harvest):
"""See available basis by coin"""
events = get_events(loaders.all)
transactions = get_transactions(events, XDG_CONFIG_HOME + "/mistbat/tx_match.yaml")
transactions = annotate_transactions(
transactions, XDG_CONFIG_HOME + "/mistbat/tx_annotations.yaml"
)
transactions = fmv_transactions(
transactions, XDG_DATA_HOME + "/mistbat/tx_fmv.yaml"
)
transactions = imply_fees(transactions)
form_8949 = Form8949(transactions)
print("\nAVAILABLE BASIS REPORT")
print(
"Note: Coin totals will slighly deviate from 'holdings' since SENDRECV fees do not impact basis.\n"
)
table_headings = [
"Coin",
"Date Acquired",
"Amount",
"Basis per Coin",
"Total Basis",
]
if harvest:
table_headings.append("Cum. G/L at Spot Price")
spot_prices = get_coin_spot_prices(
set(form_8949.current_available_basis().keys()))
table = PrettyTable(table_headings)
for coin, available_basis in form_8949.current_available_basis().items():
coin_usd_total = 0.00
coin_amount_total = 0.00
cumulative_gain_or_loss = 0.00
for basis in available_basis:
time = basis[0].strftime("%Y-%m-%d %H:%M:%S")
amount = round(basis[1], 8)
fmv = round(basis[2], 2)
total = round(amount * fmv, 2)
row = [coin, time, amount, fmv, total]
if harvest:
cumulative_gain_or_loss += (spot_prices[coin] * amount) - (fmv * amount)
row.append(round(cumulative_gain_or_loss, 2))
table.add_row(row)
coin_usd_total += total
coin_amount_total += amount
row = ["", "TOTAL", round(coin_amount_total, 8), "", round(coin_usd_total, 2)]
if harvest:
row.append("")
table.add_row(row)
table.add_row([" "] * len(table.field_names))
print(table)
@cli.command()
@click.option(
"--aggregated",
help="Aggregate holdings irrespective of location (exchange)",
is_flag=True,
default=False,
)
def holdings(aggregated):
"""List all coins held with USD values. Also list holdings by exchange."""
totals = {}
events = get_events(loaders.all)
# Get raw accounting-style entries for each event e.g., (coinbase, LTC, +1.00)
all_entries = [[], [], []] # location, coin, amount (will be zipped)
for ev in events:
entries = ev.entries()
# Try-catch block needed to deal with single vs multiple entries per event
try:
locations, coins, amounts = zip(*entries)
all_entries[0].extend(locations)
all_entries[1].extend(coins)
all_entries[2].extend(amounts)
except TypeError:
location, coin, amount = entries
all_entries[0].append(location)
all_entries[1].append(coin)
all_entries[2].append(amount)
# Process the accounting-style entries into a nested dict of
# location -> coin -> amount
for location, coin, amount in zip(*all_entries):
totals.setdefault(location, {}).setdefault(coin, 0)
totals[location][coin] += amount
# Get set of coin symbols to prepare to poll coinmarketcap API
my_coins = set(all_entries[1])
my_coins.remove("USD")
# Poll coinmarketcap API for spot prices of all coins and store them in a dict
coin_spotprices = get_coin_spot_prices(my_coins)
total_usd = 0
location_usd = {}
total_bycoin = {}
for location in totals:
if "USD" in totals[location]:
del totals[location]["USD"]
location_usd[location] = 0
for coin, amount in totals[location].items():
if round(amount, 9) != 0:
total_bycoin[coin] = total_bycoin.get(coin, 0) + amount
location_usd[location] += amount * coin_spotprices[coin]
# print('Total (in USD) at {}: ${:.2f}\n'.format(location, location_usd))
total_usd += location_usd[location]
# If the --aggregated option is passed
if aggregated:
# Sort total_bycoin by USD value
coins_sorted_usd = []
for coin, amount in total_bycoin.items():
usd_value = amount * coin_spotprices[coin]
coins_sorted_usd.append((coin, amount, usd_value))
coins_sorted_usd.sort(key=lambda x: x[2], reverse=True)
# Print out the total coin values sorted by value
for coin in coins_sorted_usd:
print(
"{} {:.8f} (USD {:.2f} @ USD {:.2f} per {})".format(
coin[0], coin[1], coin[2], coin_spotprices[coin[0]], coin[0]
)
)
# If the --aggregated option is not passed
else:
# Sort locations by USD value
locations = list(totals.keys())
locations.sort(key=lambda x: location_usd[x], reverse=True)
for location in locations:
print("\n{} (USD {:.2f})".format(location, location_usd[location]))
# Sort coins within a location by USD value
coins_sorted_usd = []
for coin, amount in totals[location].items():
usd_value = amount * coin_spotprices[coin]
coins_sorted_usd.append((coin, amount, usd_value))
coins_sorted_usd.sort(key=lambda x: x[2], reverse=True)
# Print out the total coin values sorted by value
for coin in coins_sorted_usd:
if round(coin[1], 9) != 0:
print(
" {} {:.8f} (USD {:.2f} @ USD {:.2f} per {})".format(
coin[0], coin[1], coin[2], coin_spotprices[coin[0]], coin[0]
)
)
print("-----------------")
print("Total Portfolio Value: USD {:.2f}".format(total_usd))
@cli.command()
@click.argument("exchange")
def remoteupdate(exchange):
"""Fetch updated coinbase information from remote"""
if exchange == "coinbase":
loaders.coinbase.update_from_remote()
elif exchange == "gdax":
loaders.gdax.update_from_remote()
elif exchange == "binance":
loaders.binance.update_from_remote()
else:
print("Bad exchange specified.")
if __name__ == "__main__":
cli()