summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoruvok2025-04-13 15:26:11 +0200
committeruvok2025-04-13 15:26:11 +0200
commit87065de467bd5cfc70e2b9a1d895fda76f308758 (patch)
treefd44abb7d21992c1ed9560be7c1bfee78a81b44b
initial
-rw-r--r--bla.py55
-rw-r--r--kraken_ledger.csv23
2 files changed, 78 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)
diff --git a/kraken_ledger.csv b/kraken_ledger.csv
new file mode 100644
index 0000000..201bc91
--- /dev/null
+++ b/kraken_ledger.csv
@@ -0,0 +1,23 @@
+"txid","refid","time","type","subtype","aclass","asset","wallet","amount","fee","balance"
+"","d1e57f","2024-07-12 16:26:22","trade","tradespot","currency","EUR","spot / main",-130.4204,0,0.0000
+"","d1e57f","2024-07-12 16:26:22","trade","tradespot","currency","DOGE","spot / main",1321.95097670,5.28780391,1316.66317279
+"","","2024-07-12 16:36:49","withdrawal","","currency","DOGE","spot / main",-1312.66317279,4.00000000,0.00000000
+"","","2024-08-02 14:24:30","deposit","","currency","EUR","spot / main",100.0000,0,100.0000
+"","1c80f4","2024-08-03 01:10:06","trade","tradespot","currency","EUR","spot / main",-100.0000,0,0.0000
+"","1c80f4","2024-08-03 01:10:06","trade","tradespot","currency","DOGE","spot / main",1000.00000000,2.50000000,997.50000000
+"","","2024-08-03 14:35:54","withdrawal","","currency","DOGE","spot / main",-993.50000000,4.00000000,0.00000000
+"","","2025-04-09 16:40:43","deposit","","currency","EUR","spot / main",100.0000,0,100.0000
+"","a91df1","2025-04-09 16:44:15","trade","tradespot","currency","EUR","spot / main",-100.0000,0,0.0000
+"","a91df1","2025-04-09 16:44:15","trade","tradespot","currency","DOGE","spot / main",747.94315632,1.86985789,746.07329843
+"","","2025-04-09 17:03:36","withdrawal","","currency","DOGE","spot / main",-742.07329843,4.00000000,0.00000000
+"","","2025-04-11 14:53:45","deposit","","currency","EUR","spot / main",100.0000,0,100.0000
+"","f1dbbd","2025-04-11 16:05:06","trade","tradespot","currency","EUR","spot / main",-100.0000,0,0.0000
+"","f1dbbd","2025-04-11 16:05:06","trade","tradespot","currency","DOGE","spot / main",714.88826296,2.85955305,712.02870991
+"","","2025-04-12 13:59:49","deposit","","currency","EUR","spot / main",100.0000,0,100.0000
+"","","2025-04-12 14:03:25","deposit","","currency","EUR","spot / main",20.0000,0,120.0000
+"","3cba2e","2025-04-12 14:03:36","trade","tradespot","currency","EUR","spot / main",-99.9998,0.4000,19.6002
+"","3cba2e","2025-04-12 14:03:36","trade","tradespot","currency","ADA","spot / main",172.88475502,0,172.88475502
+"","849986","2025-04-13 09:26:39","trade","tradespot","currency","DOGE","spot / main",-712.02870991,0,0.00000000
+"","849986","2025-04-13 09:26:39","trade","tradespot","currency","EUR","spot / main",103.9125,0.4156,123.0971
+"","bde54e","2025-04-13 09:58:22","trade","tradespot","currency","EUR","spot / main",-100.0000,0.4000,22.6971
+"","bde54e","2025-04-13 09:58:22","trade","tradespot","currency","PEPE","spot / main",15360983.10,0,15360983.10