summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pnlcalc.py49
1 files changed, 28 insertions, 21 deletions
diff --git a/pnlcalc.py b/pnlcalc.py
index 51152c2..9412200 100644
--- a/pnlcalc.py
+++ b/pnlcalc.py
@@ -3,7 +3,7 @@ from datetime import datetime
from decimal import Decimal
from itertools import groupby
import logging
-from typing import Dict, List
+from typing import Dict, List, Tuple
from kraken import read_ledger
from ledger_action import LedgerAction
@@ -19,35 +19,34 @@ logging.basicConfig(
logger = logging.getLogger("pnlcalc")
-def generate_report(sale_entries, proceeds: Decimal, crypto_asset, date_sold):
+def generate_report(currency: str, trades: List[Tuple[Trade, Trade]]):
report = []
- sell_date = datetime.strptime(date_sold, "%Y-%m-%d").strftime("%d.%m.%Y")
- proceeds = Decimal(proceeds)
- trade: Trade
- for trade in sale_entries:
- buy_date_formatted = datetime.strptime(trade.date, "%Y-%m-%d").strftime(
- "%d.%m.%Y"
- )
- holding_period = (
- datetime.strptime(date_sold, "%Y-%m-%d")
- - datetime.strptime(trade.date, "%Y-%m-%d")
- ).days
+ for trade in trades:
+ sell_date = datetime.strptime(trade[1].date, "%Y-%m-%d")
+ sell_date_formatted = sell_date.strftime("%d.%m.%Y")
+ buy_date = datetime.strptime(trade[0].date, "%Y-%m-%d")
+ buy_date_formatted = buy_date.strftime("%d.%m.%Y")
+
+ holding_period = (sell_date - buy_date).days
+
+ sell_proceeds = trade[1].total_cost
short_or_long = "Short" if holding_period < 365 else "Long"
- total_cost = trade.total_cost
- gain_or_loss = proceeds - total_cost
+ buy_cost = trade[0].total_cost
+ gain_or_loss = sell_proceeds - buy_cost
+ assert trade[0].amount == trade[1].amount
report.append(
{
- "Amount": f"{trade.amount:.8f}",
- "Currency": crypto_asset,
- "Date Sold": sell_date,
+ "Amount": f"{trade[0].amount:.8f}",
+ "Currency": currency,
+ "Date Sold": sell_date_formatted,
"Date Acquired": buy_date_formatted,
"Short/Long": short_or_long,
"Buy/Input at": "Kraken",
"Sell/Output at": "Kraken",
- "Proceeds": f"{proceeds:.2f}",
- "Cost Basis": f"{total_cost:.2f}",
+ "Proceeds": f"{sell_proceeds:.2f}",
+ "Cost Basis": f"{buy_cost:.2f}",
"Gain/Loss": f"{gain_or_loss:.2f}",
}
)
@@ -84,5 +83,13 @@ output_path = "tax_report.csv" # Replace with your desired output file path
actions = read_ledger(ledger_path)
lp = LedgerProcess()
lp.process_ledger(actions)
+
+all_trade_pairs: Dict[str, List[Tuple[Trade, Trade]]] = {}
+for curr, queue in lp.fifo_queues.items():
+ all_trade_pairs[curr] = []
+ trades_for_curr = queue.match_trades()
+ all_trade_pairs[curr].extend(trades_for_curr)
+ report.extend(generate_report(curr, trades_for_curr))
+
+write_report(output_path)
pass
-# write_report(output_path)