diff options
| -rw-r--r-- | bla.py | 31 | 
1 files changed, 23 insertions, 8 deletions
@@ -56,7 +56,7 @@ def generate_report(sale_entries, proceeds, crypto_asset, date_sold):          buy_date_formatted = datetime.strptime(buy_date, "%Y-%m-%d").strftime("%d.%m.%Y")          holding_period = (datetime.strptime(date_sold, "%Y-%m-%d") - datetime.strptime(buy_date, "%Y-%m-%d")).days          short_or_long = "Short" if holding_period < 365 else "Long" -        cost_basis = amount * cost +        cost_basis = cost          gain_or_loss = proceeds - cost_basis          report.append({ @@ -74,7 +74,7 @@ def generate_report(sale_entries, proceeds, crypto_asset, date_sold):      return report -def process_ledger(file_path): +def process_ledger(file_path, output_path):      fifo_queues = {}  # Separate FIFO queue per cryptocurrency      trades_by_refid = defaultdict(list)      report = [] @@ -117,17 +117,32 @@ def process_ledger(file_path):                          fifo_queues[crypto_asset].add(crypto_amount, stake_amount, date_sold)                      elif eur_amount > 0:  # Sale of cryptocurrency                          proceeds = eur_amount - eur_fee  # Account for EUR fees -                        cost_basis, sale_entries = fifo_queues[crypto_asset].remove(-crypto_amount) +                        _, sale_entries = fifo_queues[crypto_asset].remove(-crypto_amount)                          report.extend(generate_report(sale_entries, proceeds, crypto_asset, date_sold))                  else:                      raise ValueError(f"Unexpected trade grouping for refid {refid}")              else:                  raise ValueError(f"Unexpected number of trades for refid {refid}") -    return report +    # Write report to CSV +    with open(output_path, 'w', newline='') as csvfile: +        fieldnames = [ +            "Amount", +            "Currency", +            "Date Sold", +            "Date Acquired", +            "Short/Long", +            "Buy/Input at", +            "Sell/Output at", +            "Proceeds", +            "Cost Basis", +            "Gain/Loss" +        ] +        writer = csv.DictWriter(csvfile, fieldnames=fieldnames) +        writer.writeheader() +        writer.writerows(report)  # Usage -ledger_path = "kraken_ledger.csv"  # Replace with your file path -tax_report = process_ledger(ledger_path) -for entry in tax_report: -    print(",".join(f"{key}={value}" for key, value in entry.items())) +ledger_path = "kraken_ledger.csv"  # Replace with your ledger file path +output_path = "tax_report.csv"  # Replace with your desired output file path +process_ledger(ledger_path, output_path)  | 
