Skip to content

Commit

Permalink
Line numbers for errors in trades csv-file
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsofwinter committed May 1, 2018
1 parent 94d9956 commit 68c5ae1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 32 deletions.
4 changes: 4 additions & 0 deletions report.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import datetime
import os
import sys
from enum import Enum

from taxdata import PersonalDetails, Fees, Trades, TaxEvent
Expand Down Expand Up @@ -43,6 +44,9 @@ def __str__(self):
exclude_groups=opts.exclude_groups if opts.exclude_groups else [],
coin_report_filename=os.path.join(opts.out, "coin_report.csv") if opts.coin_report else None
)
if not tax_events:
print(f"Aborting tax computation.")
sys.exit(1)

if opts.simplified_k4:
tax_events = tax.aggregate_per_coin(tax_events)
Expand Down
68 changes: 37 additions & 31 deletions tax.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,38 +57,44 @@ def get_sell_coin(trade:Trade):
elif trade.group in exclude_groups:
continue

if trade.type == 'Trade':
buy_coin = get_buy_coin(trade)
sell_coin = get_sell_coin(trade)
try:

if trade.sell_coin == native_currency:
value_sek = trade.sell_value
else:
value_sek = trade.buy_value

if buy_coin:
buy_coin.buy(trade.buy_amount, value_sek)
if sell_coin:
tax_event = sell_coin.sell(trade.sell_amount, value_sek)
if trade.date >= from_date:
tax_events.append(tax_event)

elif trade.type == 'Mining':
buy_coin = get_buy_coin(trade)
if buy_coin:
buy_coin.buy(trade.buy_amount, trade.buy_value)

elif trade.type == 'Gift/Tip':
buy_coin = get_buy_coin(trade)
if buy_coin:
buy_coin.buy(trade.buy_amount, 0.0)

elif trade.type == 'Spend':
sell_coin = get_sell_coin(trade)
if sell_coin:
tax_event = sell_coin.sell(trade.sell_amount, trade.sell_value)
if trade.date >= from_date:
tax_events.append(tax_event)
if trade.type == 'Trade':
buy_coin = get_buy_coin(trade)
sell_coin = get_sell_coin(trade)

if trade.sell_coin == native_currency:
value_sek = trade.sell_value
else:
value_sek = trade.buy_value

if buy_coin:
buy_coin.buy(trade.buy_amount, value_sek)
if sell_coin:
tax_event = sell_coin.sell(trade.sell_amount, value_sek)
if trade.date >= from_date:
tax_events.append(tax_event)

elif trade.type == 'Mining':
buy_coin = get_buy_coin(trade)
if buy_coin:
buy_coin.buy(trade.buy_amount, trade.buy_value)

elif trade.type == 'Gift/Tip':
buy_coin = get_buy_coin(trade)
if buy_coin:
buy_coin.buy(trade.buy_amount, 0.0)

elif trade.type == 'Spend':
sell_coin = get_sell_coin(trade)
if sell_coin:
tax_event = sell_coin.sell(trade.sell_amount, trade.sell_value)
if trade.date >= from_date:
tax_events.append(tax_event)

except Exception as e:
print(f"Exception encountered at line {trade.lineno} in trades csv-file: {e}")
return None

if coin_report_filename:
with open(coin_report_filename, "w") as f:
Expand Down
6 changes: 5 additions & 1 deletion taxdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ def read_from(filename):


class Trade:
def __init__(self, date:datetime, type, group,
def __init__(self, lineno, date:datetime, type, group,
buy_coin, buy_amount, buy_value,
sell_coin, sell_amount, sell_value):
self.lineno = lineno
self.date = date
self.type = type
self.group = group
Expand Down Expand Up @@ -100,8 +101,10 @@ def indices(col_name):
sell_value_index = indices(price_field_name)[1]

trades = []
lineno = 2
for line in lines[1:]:
trade = Trade(
lineno,
datetime.strptime(line[date_index], "%d.%m.%Y %H:%M"),
line[type_index],
None if line[group_index] == '-' else line[group_index],
Expand All @@ -119,6 +122,7 @@ def indices(col_name):
if trade.sell_value:
trade.sell_value *= usdsek_rate
trades.append(trade)
lineno += 1

trades.sort(key=lambda x: x.date)

Expand Down

0 comments on commit 68c5ae1

Please sign in to comment.