summaryrefslogtreecommitdiff
path: root/bla.py
diff options
context:
space:
mode:
authoruvok2025-04-13 15:26:11 +0200
committeruvok2025-04-13 15:26:11 +0200
commit87065de467bd5cfc70e2b9a1d895fda76f308758 (patch)
treefd44abb7d21992c1ed9560be7c1bfee78a81b44b /bla.py
initial
Diffstat (limited to 'bla.py')
-rw-r--r--bla.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/bla.py b/bla.py
new file mode 100644
index 0000000..c73c234
--- /dev/null
+++ b/bla.py
@@ -0,0 +1,55 @@
+import csv
+from collections import deque
+
+class FIFOQueue:
+ def __init__(self):
+ self.queue = deque()
+
+ def add(self, amount, price):
+ self.queue.append((amount, price))
+
+ def remove(self, amount):
+ remaining = amount
+ cost_basis = 0
+ while remaining > 0:
+ quantity, price = self.queue[0]
+ if quantity > remaining:
+ cost_basis += remaining * price
+ self.queue[0] = (quantity - remaining, price)
+ remaining = 0
+ else:
+ cost_basis += quantity * price
+ remaining -= quantity
+ self.queue.popleft()
+ return cost_basis
+
+def process_ledger(file_path):
+ fifo_queues = {} # Separate FIFO queue per currency
+ report = []
+
+ with open(file_path, 'r') as file:
+ reader = csv.DictReader(file)
+ for row in reader:
+ currency = row["asset"]
+ # Use setdefault() for streamlined dictionary handling
+ fifo_queues.setdefault(currency, FIFOQueue())
+
+ if row["type"] == "trade" and row["amount"].startswith('-'): # Sale
+ amount = -float(row["amount"])
+ cost_basis = fifo_queues[currency].remove(amount)
+ sale_proceeds = amount * float(row["fee"]) # Adjust fee as needed
+ profit_or_loss = sale_proceeds - cost_basis
+ report.append((row["time"], currency, profit_or_loss))
+
+ elif row["type"] == "trade" and not row["amount"].startswith('-'): # Purchase
+ amount = float(row["amount"])
+ price = float(row["fee"]) # Adjust fee as needed
+ fifo_queues[currency].add(amount, price)
+
+ return report
+
+# Usage
+ledger_path = "kraken_ledger.csv" # Replace with your file path
+profit_and_loss_report = process_ledger(ledger_path)
+for entry in profit_and_loss_report:
+ print(entry)