-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
1046 lines (899 loc) · 47.4 KB
/
Copy pathparser.py
File metadata and controls
1046 lines (899 loc) · 47.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pdfplumber
import re
import os
import sys
import json
from collections import defaultdict
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.gridspec as gridspec
import numpy as np
MONTH_ABBREVS = "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec"
MONTH_NAMES = {
"01": "Jan", "02": "Feb", "03": "Mar", "04": "Apr",
"05": "May", "06": "Jun", "07": "Jul", "08": "Aug",
"09": "Sep", "10": "Oct", "11": "Nov", "12": "Dec",
}
def load_config():
"""Load multipliers from config.json next to this script."""
config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.json")
with open(config_path) as f:
return json.load(f)
def find_pdf():
"""Find a PDF file to parse — auto-detect in current dir or ask the user."""
search_dir = os.path.dirname(os.path.abspath(__file__)) or os.getcwd()
while True:
pdfs = sorted([f for f in os.listdir(search_dir) if f.lower().endswith(".pdf")])
if len(pdfs) == 1:
path = os.path.join(search_dir, pdfs[0])
print(f"Found PDF: {pdfs[0]}")
return path
if len(pdfs) > 1:
print(f"\nFound {len(pdfs)} PDF files in {search_dir}:\n")
for i, name in enumerate(pdfs, 1):
print(f" {i}. {name}")
print()
while True:
choice = input(f"Select a file (1-{len(pdfs)}): ").strip()
if choice.isdigit() and 1 <= int(choice) <= len(pdfs):
path = os.path.join(search_dir, pdfs[int(choice) - 1])
return path
print("Invalid choice, try again.")
# No PDFs found
print(f"No PDF files found in {search_dir}")
user_path = input("Enter path to a PDF file or directory: ").strip().strip('"')
if os.path.isfile(user_path) and user_path.lower().endswith(".pdf"):
return user_path
if os.path.isdir(user_path):
search_dir = user_path
continue
print(f"Invalid path: {user_path}")
def detect_years(pdf_path):
"""Scan the PDF for all years that appear in RUN DATE fields."""
years = set()
with pdfplumber.open(pdf_path) as pdf:
for page in pdf.pages:
text = page.extract_text()
if not text:
continue
m = re.search(r"RUN DATE\s*:\s*\d{2}/\d{2}/(\d{4})", text)
if m:
years.add(m.group(1))
return sorted(years)
def pick_year(pdf_path):
"""Detect years in the PDF and let the user choose if there are multiple."""
years = detect_years(pdf_path)
if not years:
year = input("Could not detect any year in the PDF. Enter year (e.g. 2025): ").strip()
return year
if len(years) == 1:
print(f"Detected year: {years[0]}")
return years[0]
print(f"\nMultiple years found in the PDF:\n")
for i, y in enumerate(years, 1):
print(f" {i}. {y}")
print()
while True:
choice = input(f"Select year (1-{len(years)}): ").strip()
if choice.isdigit() and 1 <= int(choice) <= len(years):
return years[int(choice) - 1]
print("Invalid choice, try again.")
def derive_eur_usd_rate(pdf_path, year):
"""Derive EUR/USD rate from WIRE RECEIVED + Adjustments USDE pairs on same dates."""
# Collect per-date: EUR deposits and USDE adjustments
date_eur = defaultdict(float)
date_usde = defaultdict(float)
with pdfplumber.open(pdf_path) as pdf:
current_month_prefix = None
for page in pdf.pages:
text = page.extract_text()
if not text:
continue
m = re.search(rf"RUN DATE\s*:\s*(\d{{2}})/\d{{2}}/{year}", text)
if m:
current_month_prefix = m.group(1)
if not current_month_prefix:
continue
for line in text.split("\n"):
# WIRE RECEIVED: EUR column (3rd number)
wr = re.match(
rf"(\d{{2}}/\d{{2}}/{year})\s+WIRE RECEIVED\s+[\d,.]+\s+([\d,.]+)\s+[\d,.]+",
line,
)
if wr:
date_eur[wr.group(1)] += float(wr.group(2).replace(",", ""))
continue
# Adjustments USDE: Combined column (1st number, positive only = deposit)
adj = re.match(
rf"(\d{{2}}/\d{{2}}/{year})\s+Adjustme[nt]{{1,3}}s?\s+USDE\s+([\d,.]+)\s",
line,
)
if adj:
val = float(adj.group(2).replace(",", ""))
date_usde[adj.group(1)] += val
# Match dates where both a deposit and a positive adjustment exist
rates = []
for date in date_eur:
if date in date_usde and date_eur[date] > 0 and date_usde[date] > 0:
rate = date_usde[date] / date_eur[date]
if 0.8 < rate < 1.5: # sanity check
rates.append(rate)
if rates:
return sum(rates) / len(rates)
return None
def make_month_data():
"""Create a fresh month data dict."""
return {
"pnl": 0.0, "commission": 0.0,
"clearing_fee": 0.0, "nfa_fee": 0.0,
"deposits_eur": 0.0, "withdrawals_eur": 0.0,
"wire_fees_usd": 0.0,
"contracts": defaultdict(lambda: {"buys": 0.0, "sells": 0.0, "buy_qty": 0, "sell_qty": 0}),
}
def parse_trades(pdf_path, year, multipliers):
"""Parse trades from a PhillipCapital futures PDF export.
Returns dict per month with keys:
pnl, commission, clearing_fee, nfa_fee,
deposits_eur, withdrawals_eur, wire_fees_usd,
contracts: {symbol: {buys, sells, buy_qty, sell_qty}}
"""
data = defaultdict(make_month_data)
current_month = None
yy = year[2:] # e.g. "25" from "2025"
unknown_symbols = set()
with pdfplumber.open(pdf_path) as pdf:
for page in pdf.pages:
text = page.extract_text()
if not text:
continue
# Detect month from RUN DATE : MM/DD/YYYY To MM/DD/YYYY
m = re.search(rf"RUN DATE\s*:\s*(\d{{2}})/\d{{2}}/{year}", text)
if m:
current_month = m.group(1) + year # e.g. "022025"
if not current_month:
continue
for line in text.split("\n"):
# ── Cash flow: daily Realised P&L ────────────────────────
pnl_match = re.match(
rf"\d{{2}}/\d{{2}}/{year}\s+Realised P&L\s+([-\d,]+\.\d{{2}})",
line,
)
if pnl_match:
data[current_month]["pnl"] += float(pnl_match.group(1).replace(",", ""))
continue
# ── Cash flow: daily Commission ──────────────────────────
comm_match = re.match(
rf"\d{{2}}/\d{{2}}/{year}\s+Commission\s+([-\d,]+\.\d{{2}})",
line,
)
if comm_match:
data[current_month]["commission"] += float(comm_match.group(1).replace(",", ""))
continue
# ── Cash flow: daily Clearing Fee ────────────────────────
clear_match = re.match(
rf"\d{{2}}/\d{{2}}/{year}\s+Clearing Fee\s+([-\d,]+\.\d{{2}})",
line,
)
if clear_match:
data[current_month]["clearing_fee"] += float(clear_match.group(1).replace(",", ""))
continue
# ── Cash flow: daily NFA ─────────────────────────────────
nfa_match = re.match(
rf"\d{{2}}/\d{{2}}/{year}\s+NFA\s+([-\d,]+\.\d{{2}})",
line,
)
if nfa_match:
data[current_month]["nfa_fee"] += float(nfa_match.group(1).replace(",", ""))
continue
# ── Deposits: WIRE RECEIVED (EUR column) ─────────────────
dep_match = re.match(
rf"\d{{2}}/\d{{2}}/{year}\s+WIRE RECEIVED\s+[\d,.]+\s+([\d,.]+)\s+[\d,.]+",
line,
)
if dep_match:
data[current_month]["deposits_eur"] += float(dep_match.group(1).replace(",", ""))
continue
# ── Withdrawals: WIRE SENT (EUR column, negative) ────────
wd_match = re.match(
rf"\d{{2}}/\d{{2}}/{year}\s+WIRE SENT.*?\s+([-\d,.]+)\s+[\d,.]+\s*$",
line,
)
if wd_match:
data[current_month]["withdrawals_eur"] += float(wd_match.group(1).replace(",", ""))
continue
# ── Wire fees (USD column) ───────────────────────────────
wf_match = re.match(
rf"\d{{2}}/\d{{2}}/{year}\s+WIRE FEE\s+[\d,.]+\s+[\d,.]+\s+([-\d,.]+)",
line,
)
if wf_match:
data[current_month]["wire_fees_usd"] += float(wf_match.group(1).replace(",", ""))
continue
# ── Trade lines: buy (positive qty) ──────────────────────
# Format: "02/28/25 3 CME MICRO MINI NQ(MNQ) Mar 25 20557.50 USD"
# Also handles spaced date: "0 2 / 2 8 / 2 5 3 CME ..."
buy_match = re.search(
rf"(\d+)\s+CME\s+.*?\((\w+)\)\s+(?:{MONTH_ABBREVS})\s+{yy}\s+([\d.]+)\s+\w{{3}}$",
line,
)
if buy_match and "-" not in line.split("CME")[0]:
qty = int(buy_match.group(1))
symbol = buy_match.group(2)
price = float(buy_match.group(3))
data[current_month]["contracts"][symbol]["buys"] += price * qty
data[current_month]["contracts"][symbol]["buy_qty"] += qty
if symbol not in multipliers:
unknown_symbols.add(symbol)
continue
# ── Trade lines: sell (negative qty) ─────────────────────
# Format: "02/28/25 -3 CME ... (MNQ) Mar 25 20567.75 61.50 USD"
sell_match = re.search(
rf"-(\d+)\s+CME\s+.*?\((\w+)\)\s+(?:{MONTH_ABBREVS})\s+{yy}\s+([\d.]+)\s+[-\d,.]+\s+\w{{3}}$",
line,
)
if sell_match:
qty = int(sell_match.group(1))
symbol = sell_match.group(2)
price = float(sell_match.group(3))
data[current_month]["contracts"][symbol]["sells"] += price * qty
data[current_month]["contracts"][symbol]["sell_qty"] += qty
if symbol not in multipliers:
unknown_symbols.add(symbol)
continue
if unknown_symbols:
print(f"\nWARNING: Unknown contract symbols (not in config.json): {', '.join(sorted(unknown_symbols))}")
print("Add them to config.json with the correct multiplier.\n")
return data
def calc_month_totals(data, month, multipliers):
"""Calculate aggregated buys, sells, and calculated P&L for a month across all contracts.
Buys and sells are multiplied by the contract multiplier to give USD values."""
d = data[month]
total_buys = 0.0
total_sells = 0.0
calc_pnl = 0.0
for sym, c in d["contracts"].items():
mult = multipliers.get(sym, 1)
total_buys += c["buys"] * mult
total_sells += c["sells"] * mult
calc_pnl += (c["sells"] - c["buys"]) * mult
return total_buys, total_sells, calc_pnl
def print_table(data, year, eur_rate, multipliers):
"""Print a text summary table to the console."""
months = sorted(data.keys())
if not months:
print("No trade data found.")
return
# ── Per-contract breakdown ───────────────────────────────────────────────
all_symbols = set()
for month in months:
all_symbols.update(data[month]["contracts"].keys())
if all_symbols:
print(f"\n{'':=^100}")
print(f"{'PER-CONTRACT BREAKDOWN':^100}")
print(f"{'':=^100}")
print(f"\n{'MONTH':>8} | {'SYM':>5} | {'MULT':>5} | {'BUY QTY':>8} | {'SELL QTY':>8} | {'BUYS (USD)':>16} | {'SELLS (USD)':>16} | {'CALC P&L':>14}")
print("-" * 100)
for month in months:
label = f"{MONTH_NAMES.get(month[:2], month[:2])} {year}"
for sym in sorted(data[month]["contracts"].keys()):
c = data[month]["contracts"][sym]
mult = multipliers.get(sym, 1)
buys_usd = c["buys"] * mult
sells_usd = c["sells"] * mult
pnl = sells_usd - buys_usd
print(f"{label:>8} | {sym:>5} | {mult:>5} | {c['buy_qty']:>8} | {c['sell_qty']:>8} | {buys_usd:>16,.2f} | {sells_usd:>16,.2f} | {pnl:>+14,.2f}")
# ── USD Table ────────────────────────────────────────────────────────────
print(f"\n{'':=^140}")
print(f"{'USD SUMMARY':^140}")
print(f"{'':=^140}")
print(f"\n{'MONTH':>8} | {'BUYS':>14} | {'SELLS':>14} | {'CALC P&L':>12} | {'PDF P&L':>12} | {'DIFF':>10} | {'COMMISSION':>12} | {'CLEARING':>10} | {'NFA':>8} | {'NET P&L':>14}")
print("-" * 140)
totals = defaultdict(float)
for month in months:
d = data[month]
buys, sells, calc_pnl = calc_month_totals(data, month, multipliers)
net = d["pnl"] + d["commission"] + d["clearing_fee"] + d["nfa_fee"]
diff = round(calc_pnl - d["pnl"], 2)
label = f"{MONTH_NAMES.get(month[:2], month[:2])} {year}"
print(f"{label:>8} | {buys:>14,.2f} | {sells:>14,.2f} | {calc_pnl:>+12,.2f} | {d['pnl']:>+12,.2f} | {diff:>+10,.2f} | {d['commission']:>12,.2f} | {d['clearing_fee']:>10,.2f} | {d['nfa_fee']:>8,.2f} | {net:>+14,.2f}")
totals["buys"] += buys
totals["sells"] += sells
totals["calc_pnl"] += calc_pnl
for k in ("pnl", "commission", "clearing_fee", "nfa_fee", "deposits_eur", "withdrawals_eur", "wire_fees_usd"):
totals[k] += d[k]
print("-" * 140)
net_total = totals["pnl"] + totals["commission"] + totals["clearing_fee"] + totals["nfa_fee"]
total_diff = round(totals["calc_pnl"] - totals["pnl"], 2)
print(f"{'TOTAL':>8} | {totals['buys']:>14,.2f} | {totals['sells']:>14,.2f} | {totals['calc_pnl']:>+12,.2f} | {totals['pnl']:>+12,.2f} | {total_diff:>+10,.2f} | {totals['commission']:>12,.2f} | {totals['clearing_fee']:>10,.2f} | {totals['nfa_fee']:>8,.2f} | {net_total:>+14,.2f}")
# ── Deposits & Withdrawals ───────────────────────────────────────────────
print(f"\n{'MONTH':>8} | {'DEPOSITS (EUR)':>16} | {'WITHDRAWALS (EUR)':>18} | {'WIRE FEES (USD)':>16} | {'NET FLOW (EUR)':>16}")
print("-" * 90)
for month in months:
d = data[month]
if d["deposits_eur"] == 0 and d["withdrawals_eur"] == 0 and d["wire_fees_usd"] == 0:
continue
label = f"{MONTH_NAMES.get(month[:2], month[:2])} {year}"
net_flow = d["deposits_eur"] + d["withdrawals_eur"]
print(f"{label:>8} | {d['deposits_eur']:>16,.2f} | {d['withdrawals_eur']:>+18,.2f} | {d['wire_fees_usd']:>16,.2f} | {net_flow:>+16,.2f}")
print("-" * 90)
net_flow_total = totals["deposits_eur"] + totals["withdrawals_eur"]
print(f"{'TOTAL':>8} | {totals['deposits_eur']:>16,.2f} | {totals['withdrawals_eur']:>+18,.2f} | {totals['wire_fees_usd']:>16,.2f} | {net_flow_total:>+16,.2f}")
# ── EUR Table ────────────────────────────────────────────────────────────
print(f"\n{'':=^160}")
print(f"{'EUR SUMMARY (EUR/USD rate: ' + f'{eur_rate:.4f})':^160}")
print(f"{'':=^160}")
to_eur = 1.0 / eur_rate
print(f"\n{'MONTH':>8} | {'BUYS':>16} | {'SELLS':>16} | {'REALISED P&L':>14} | {'COMMISSION':>12} | {'CLEARING':>10} | {'NFA':>8} | {'WIRE FEES':>10} | {'NET P&L':>14} | {'DEPOSITS':>12} | {'WITHDRAWALS':>14}")
print("-" * 160)
for month in months:
d = data[month]
buys, sells, _ = calc_month_totals(data, month, multipliers)
net_usd = d["pnl"] + d["commission"] + d["clearing_fee"] + d["nfa_fee"] + d["wire_fees_usd"]
label = f"{MONTH_NAMES.get(month[:2], month[:2])} {year}"
print(f"{label:>8} | {buys*to_eur:>16,.2f} | {sells*to_eur:>16,.2f} | {d['pnl']*to_eur:>+14,.2f} | {d['commission']*to_eur:>12,.2f} | {d['clearing_fee']*to_eur:>10,.2f} | {d['nfa_fee']*to_eur:>8,.2f} | {d['wire_fees_usd']*to_eur:>10,.2f} | {net_usd*to_eur:>+14,.2f} | {d['deposits_eur']:>12,.2f} | {d['withdrawals_eur']:>+14,.2f}")
print("-" * 160)
total_net_usd = net_total + totals["wire_fees_usd"]
print(f"{'TOTAL':>8} | {totals['buys']*to_eur:>16,.2f} | {totals['sells']*to_eur:>16,.2f} | {totals['pnl']*to_eur:>+14,.2f} | {totals['commission']*to_eur:>12,.2f} | {totals['clearing_fee']*to_eur:>10,.2f} | {totals['nfa_fee']*to_eur:>8,.2f} | {totals['wire_fees_usd']*to_eur:>10,.2f} | {total_net_usd*to_eur:>+14,.2f} | {totals['deposits_eur']:>12,.2f} | {totals['withdrawals_eur']:>+14,.2f}")
def calc_finnish_tax(data, year, eur_rate, multipliers):
"""Calculate Finnish tax figures (veroilmoitus) per disposal.
Each symbol/month combination with both buys and sells is one disposal (luovutus).
Fees (commission, clearing, NFA, wire) are allocated proportionally by contract
count and added to hankintameno (acquisition cost), per Finnish tax rules.
Returns a list of disposal dicts and the three summary totals.
"""
to_eur = 1.0 / eur_rate
months = sorted(data.keys())
disposals = []
for month in months:
d = data[month]
label = f"{MONTH_NAMES.get(month[:2], month[:2])} {year}"
# Month's total fees in EUR
month_fees_eur = abs(d["commission"] + d["clearing_fee"] + d["nfa_fee"] + d["wire_fees_usd"]) * to_eur
# Total contracts traded this month (for proportional fee allocation)
total_contracts = sum(c["buy_qty"] + c["sell_qty"] for c in d["contracts"].values())
for sym in sorted(d["contracts"].keys()):
c = d["contracts"][sym]
mult = multipliers.get(sym, 1)
buys_usd = c["buys"] * mult
sells_usd = c["sells"] * mult
if c["sell_qty"] == 0:
continue # no disposal if nothing sold
# Allocate fees proportionally by contract count
sym_contracts = c["buy_qty"] + c["sell_qty"]
kulut_eur = month_fees_eur * (sym_contracts / total_contracts) if total_contracts > 0 else 0.0
# Luovutushinta = sale proceeds
# Hankintameno = acquisition cost + allocated fees (kulut)
luovutushinta_eur = sells_usd * to_eur
hankintameno_eur = buys_usd * to_eur + kulut_eur
tulos_eur = luovutushinta_eur - hankintameno_eur
disposals.append({
"month": label,
"symbol": sym,
"buy_qty": c["buy_qty"],
"sell_qty": c["sell_qty"],
"luovutushinta_eur": luovutushinta_eur,
"hankintameno_eur": hankintameno_eur,
"kulut_eur": kulut_eur,
"tulos_eur": tulos_eur,
})
luovutushinnat = sum(d["luovutushinta_eur"] for d in disposals)
luovutusvoitot = sum(d["tulos_eur"] for d in disposals if d["tulos_eur"] > 0)
luovutustappiot = sum(d["tulos_eur"] for d in disposals if d["tulos_eur"] < 0)
return disposals, luovutushinnat, luovutusvoitot, luovutustappiot
def _fi(v, sign=False):
"""Format a number Finnish style: no thousands separator, comma as decimal."""
if sign:
s = f"{v:+.2f}"
else:
s = f"{v:.2f}"
return s.replace(".", ",")
def print_finnish_tax(data, year, eur_rate, multipliers):
"""Print Finnish tax summary (veroilmoitus) to the console."""
disposals, luovutushinnat, luovutusvoitot, luovutustappiot = \
calc_finnish_tax(data, year, eur_rate, multipliers)
if not disposals:
print("\nNo disposals found for Finnish tax calculation.")
return
total_kulut = sum(d["kulut_eur"] for d in disposals)
print(f"\n{'':=^140}")
print(f"{'FINNISH TAX SUMMARY / VEROILMOITUS (EUR)':^140}")
print(f"{'':=^140}")
print(f"\n EUR/USD rate: {eur_rate:.4f}")
print(f" Kulut (fees) are included in hankintameno, allocated proportionally by contract count.")
print(f"\n{'MONTH':>8} | {'SYM':>5} | {'SELL QTY':>8} | {'BUY QTY':>8} | {'Luovutushinta':>16} | {'Kulut':>12} | {'Hankintameno':>16} | {'Voitto/Tappio':>16}")
print("-" * 140)
for d in disposals:
print(f"{d['month']:>8} | {d['symbol']:>5} | {d['sell_qty']:>8} | {d['buy_qty']:>8} | {_fi(d['luovutushinta_eur']):>16} | {_fi(d['kulut_eur']):>12} | {_fi(d['hankintameno_eur']):>16} | {_fi(d['tulos_eur'], sign=True):>16}")
print("-" * 140)
total_hankintameno = sum(d["hankintameno_eur"] for d in disposals)
print(f"\n Luovutushinnat yhteensä (Total sale prices) : {_fi(luovutushinnat):>16} EUR")
print(f" Hankintamenot yhteensä (Total acquisition cost): {_fi(total_hankintameno):>16} EUR (incl. kulut {_fi(total_kulut)})")
print(f" Luovutusvoitot yhteensä (Total capital gains) : {_fi(luovutusvoitot, sign=True):>16} EUR")
print(f" Luovutustappiot yhteensä (Total capital losses) : {_fi(luovutustappiot, sign=True):>16} EUR")
print(f" Netto (voitot + tappiot) (Net gain/loss) : {_fi(luovutusvoitot + luovutustappiot, sign=True):>16} EUR")
print()
def generate_report(data, year, pdf_path, eur_rate, multipliers):
"""Generate a PNG dashboard report."""
months = sorted(data.keys())
if not months:
print("No trade data found — skipping report generation.")
return
labels = [MONTH_NAMES.get(m[:2], m[:2]) for m in months]
to_eur = 1.0 / eur_rate
# Per-month values
pnl_vals = [data[m]["pnl"] for m in months]
comm_vals = [data[m]["commission"] for m in months]
clear_vals = [data[m]["clearing_fee"] for m in months]
nfa_vals = [data[m]["nfa_fee"] for m in months]
wire_fees = [data[m]["wire_fees_usd"] for m in months]
net_vals = [pnl_vals[i] + comm_vals[i] + clear_vals[i] + nfa_vals[i] for i in range(len(months))]
month_totals = [calc_month_totals(data, m, multipliers) for m in months]
buy_vals = [t[0] for t in month_totals]
sell_vals = [t[1] for t in month_totals]
calc_pnl_vals = [t[2] for t in month_totals]
dep_vals = [data[m]["deposits_eur"] for m in months]
wd_vals = [data[m]["withdrawals_eur"] for m in months]
cumulative = list(np.cumsum(net_vals))
total_pnl = sum(pnl_vals)
total_comm = sum(comm_vals)
total_clear = sum(clear_vals)
total_nfa = sum(nfa_vals)
total_fees = total_comm + total_clear + total_nfa
total_net = total_pnl + total_fees
total_buys = sum(buy_vals)
total_sells = sum(sell_vals)
total_deps = sum(dep_vals)
total_wds = sum(wd_vals)
total_wire_fees = sum(wire_fees)
# ── Colours ──────────────────────────────────────────────────────────────
pos_color = "#4CAF50"
neg_color = "#F44336"
PANEL_BG = "#16213e"
TICK_COL = "#c0c0c0"
GRID_COL = "#2a2a4a"
def money(v, sym="$"):
return f"{sym}{v:+,.0f}" if v != 0 else f"{sym}0"
def fmt_k(x, _):
return f"${x/1000:+.1f}k" if abs(x) >= 1000 else f"${x:+.0f}"
# ── Layout ───────────────────────────────────────────────────────────────
fig = plt.figure(figsize=(20, 22), facecolor="#1a1a2e")
fig.suptitle(
f"PhillipCapital Futures Report — {year}",
fontsize=18, fontweight="bold", color="white", y=0.99,
)
gs = gridspec.GridSpec(
5, 2, figure=fig,
hspace=0.45, wspace=0.3,
left=0.06, right=0.97,
top=0.96, bottom=0.03,
height_ratios=[1, 1, 1.2, 1.0, 1.0],
)
ax_bar = fig.add_subplot(gs[0, 0]) # Monthly P&L bars
ax_cum = fig.add_subplot(gs[0, 1]) # Cumulative P&L line
ax_wf = fig.add_subplot(gs[1, :]) # Net P&L after fees waterfall
ax_sum = fig.add_subplot(gs[2, :]) # USD Summary table
ax_eur = fig.add_subplot(gs[3, :]) # EUR Summary table
ax_tax = fig.add_subplot(gs[4, :]) # Finnish tax summary
for ax in (ax_bar, ax_cum, ax_wf, ax_sum, ax_eur, ax_tax):
ax.set_facecolor(PANEL_BG)
ax.tick_params(colors=TICK_COL, labelsize=9)
for spine in ax.spines.values():
spine.set_edgecolor("#3a3a5a")
# ── 1. Monthly Gross P&L (bar chart) ─────────────────────────────────────
ax_bar.set_title("Monthly Realised P&L (before fees)", color="white", fontsize=11, pad=8)
x_pos = np.arange(len(months))
bar_colors = [pos_color if v >= 0 else neg_color for v in pnl_vals]
bars = ax_bar.bar(x_pos, pnl_vals, color=bar_colors, width=0.6, zorder=3)
for bar, val in zip(bars, pnl_vals):
ax_bar.text(
bar.get_x() + bar.get_width() / 2, val,
money(val),
ha="center", va="bottom" if val >= 0 else "top",
color="white", fontsize=9, fontweight="bold",
)
ax_bar.axhline(0, color="#555577", linewidth=0.8)
ax_bar.set_xticks(x_pos)
ax_bar.set_xticklabels(labels, color="white", fontsize=10)
ax_bar.yaxis.set_major_formatter(mticker.FuncFormatter(fmt_k))
ax_bar.set_ylabel("USD", color=TICK_COL, fontsize=9)
ax_bar.grid(axis="y", color=GRID_COL, linewidth=0.7, zorder=0)
ax_bar.text(
0.97, 0.05,
f"Total: {money(total_pnl)}",
transform=ax_bar.transAxes, ha="right", va="bottom",
color="white", fontsize=10, fontweight="bold",
bbox=dict(facecolor="#0f3460", edgecolor="#4a90d9", boxstyle="round,pad=0.3"),
)
# ── 2. Cumulative P&L (line chart) ───────────────────────────────────────
ax_cum.set_title("Cumulative P&L (after all fees)", color="white", fontsize=11, pad=8)
ax_cum.plot(x_pos, cumulative, color="#00bcd4", linewidth=2, zorder=3)
ax_cum.fill_between(x_pos, cumulative, alpha=0.15, color="#00bcd4", zorder=2)
ax_cum.axhline(0, color="#555577", linewidth=0.8)
ax_cum.set_xticks(x_pos)
ax_cum.set_xticklabels(labels, rotation=45, ha="right", color=TICK_COL, fontsize=8)
ax_cum.yaxis.set_major_formatter(mticker.FuncFormatter(fmt_k))
ax_cum.set_ylabel("USD", color=TICK_COL, fontsize=9)
ax_cum.grid(color=GRID_COL, linewidth=0.7, zorder=0)
if cumulative:
ax_cum.annotate(
money(cumulative[-1]),
xy=(x_pos[-1], cumulative[-1]),
xytext=(-45, 10), textcoords="offset points",
color="white", fontsize=9, fontweight="bold",
arrowprops=dict(arrowstyle="->", color="#aaaacc", lw=1),
)
# ── 3. Net P&L after fees (waterfall) ────────────────────────────────────
ax_wf.set_title("Monthly Net P&L (after all fees)", color="white", fontsize=11, pad=8)
wf_colors = [pos_color if v >= 0 else neg_color for v in net_vals]
bars_wf = ax_wf.bar(x_pos, net_vals, color=wf_colors, width=0.6, zorder=3)
for bar, val in zip(bars_wf, net_vals):
ax_wf.text(
bar.get_x() + bar.get_width() / 2, val,
money(val),
ha="center", va="bottom" if val >= 0 else "top",
color="white", fontsize=8,
)
total_fees_per_month = [comm_vals[i] + clear_vals[i] + nfa_vals[i] for i in range(len(months))]
ax_wf.scatter(x_pos, total_fees_per_month, color="#FF9800", s=30, zorder=5, label="Total Fees")
ax_wf.axhline(0, color="#555577", linewidth=0.8)
ax_wf.set_xticks(x_pos)
ax_wf.set_xticklabels(labels, rotation=30, ha="right", color=TICK_COL, fontsize=9)
ax_wf.yaxis.set_major_formatter(mticker.FuncFormatter(fmt_k))
ax_wf.set_ylabel("USD", color=TICK_COL, fontsize=9)
ax_wf.grid(axis="y", color=GRID_COL, linewidth=0.7, zorder=0)
ax_wf.legend(loc="upper left", facecolor=PANEL_BG, edgecolor="#3a3a5a",
labelcolor="white", fontsize=8)
# ── 4. USD Summary table ─────────────────────────────────────────────────
ax_sum.axis("off")
ax_sum.set_title("USD Summary (trades + deposits/withdrawals)", color="white", fontsize=11, pad=8)
col_labels = ["Month", "Buys", "Sells", "P&L", "Commiss.", "Clearing", "NFA", "Net P&L", "Dep EUR", "Wdraw EUR", "Wire Fee"]
col_x = [0.01, 0.08, 0.18, 0.28, 0.38, 0.48, 0.56, 0.64, 0.75, 0.85, 0.94]
row_h = 0.065
y_start = 0.95
for ci, label in enumerate(col_labels):
ax_sum.text(col_x[ci], y_start, label,
transform=ax_sum.transAxes,
color="#aaddff", fontsize=8, fontweight="bold",
va="top", family="monospace")
ax_sum.plot([0.01, 0.99], [y_start - 0.03, y_start - 0.03],
color="#3a3a5a", linewidth=0.8, transform=ax_sum.transAxes)
for ri, month in enumerate(months):
y = y_start - 0.06 - ri * row_h
d = data[month]
buys_m, sells_m, calc_m = month_totals[ri]
net = d["pnl"] + d["commission"] + d["clearing_fee"] + d["nfa_fee"]
m_label = f"{MONTH_NAMES.get(month[:2], month[:2])} {year}"
pnl_color = pos_color if d["pnl"] >= 0 else neg_color
net_color = pos_color if net >= 0 else neg_color
vals = [
(m_label, "white"),
(f"${buys_m:,.0f}", TICK_COL),
(f"${sells_m:,.0f}", TICK_COL),
(f"${d['pnl']:+,.2f}", pnl_color),
(f"${d['commission']:,.2f}", "#FF9800"),
(f"${d['clearing_fee']:,.2f}", "#FF9800"),
(f"${d['nfa_fee']:,.2f}", "#FF9800"),
(f"${net:+,.2f}", net_color),
(f"{d['deposits_eur']:,.0f}" if d['deposits_eur'] else "", "#2196F3"),
(f"{d['withdrawals_eur']:+,.0f}" if d['withdrawals_eur'] else "", neg_color),
(f"${d['wire_fees_usd']:,.2f}" if d['wire_fees_usd'] else "", "#FF9800"),
]
for ci, (txt, color) in enumerate(vals):
ax_sum.text(col_x[ci], y, txt,
transform=ax_sum.transAxes,
color=color, fontsize=8, va="top", family="monospace")
# Totals row
y_total = y_start - 0.06 - len(months) * row_h
ax_sum.plot([0.01, 0.99], [y_total + row_h * 0.45, y_total + row_h * 0.45],
color="#3a3a5a", linewidth=0.8, transform=ax_sum.transAxes)
totals_row = [
("TOTAL", "white"),
(f"${total_buys:,.0f}", TICK_COL),
(f"${total_sells:,.0f}", TICK_COL),
(f"${total_pnl:+,.2f}", pos_color if total_pnl >= 0 else neg_color),
(f"${total_comm:,.2f}", "#FF9800"),
(f"${total_clear:,.2f}", "#FF9800"),
(f"${total_nfa:,.2f}", "#FF9800"),
(f"${total_net:+,.2f}", pos_color if total_net >= 0 else neg_color),
(f"{total_deps:,.0f}", "#2196F3"),
(f"{total_wds:+,.0f}", neg_color),
(f"${total_wire_fees:,.2f}", "#FF9800"),
]
for ci, (txt, color) in enumerate(totals_row):
ax_sum.text(col_x[ci], y_total, txt,
transform=ax_sum.transAxes,
color=color, fontsize=9, va="top", fontweight="bold",
family="monospace")
# ── 5. EUR Summary table ─────────────────────────────────────────────────
ax_eur.axis("off")
ax_eur.set_title(f"EUR Summary (EUR/USD rate: {eur_rate:.4f})", color="white", fontsize=11, pad=8)
eur_cols = ["Month", "Buys", "Sells", "P&L", "Comm.", "Clear.", "NFA", "Wire", "Net P&L", "Dep", "Wdraw"]
eur_x = [0.01, 0.07, 0.18, 0.29, 0.39, 0.48, 0.56, 0.63, 0.71, 0.82, 0.91]
y_start_e = 0.95
for ci, label in enumerate(eur_cols):
ax_eur.text(eur_x[ci], y_start_e, label,
transform=ax_eur.transAxes,
color="#aaddff", fontsize=8, fontweight="bold",
va="top", family="monospace")
ax_eur.plot([0.01, 0.99], [y_start_e - 0.03, y_start_e - 0.03],
color="#3a3a5a", linewidth=0.8, transform=ax_eur.transAxes)
for ri, month in enumerate(months):
y = y_start_e - 0.06 - ri * row_h
d = data[month]
buys_m, sells_m, _ = month_totals[ri]
net_usd = d["pnl"] + d["commission"] + d["clearing_fee"] + d["nfa_fee"] + d["wire_fees_usd"]
m_label = f"{MONTH_NAMES.get(month[:2], month[:2])} {year}"
pnl_e = d["pnl"] * to_eur
net_e = net_usd * to_eur
pnl_color = pos_color if pnl_e >= 0 else neg_color
net_color = pos_color if net_e >= 0 else neg_color
vals = [
(m_label, "white"),
(f"{buys_m*to_eur:,.0f}", TICK_COL),
(f"{sells_m*to_eur:,.0f}", TICK_COL),
(f"{pnl_e:+,.2f}", pnl_color),
(f"{d['commission']*to_eur:,.2f}", "#FF9800"),
(f"{d['clearing_fee']*to_eur:,.2f}", "#FF9800"),
(f"{d['nfa_fee']*to_eur:,.2f}", "#FF9800"),
(f"{d['wire_fees_usd']*to_eur:,.2f}" if d['wire_fees_usd'] else "", "#FF9800"),
(f"{net_e:+,.2f}", net_color),
(f"{d['deposits_eur']:,.0f}" if d['deposits_eur'] else "", "#2196F3"),
(f"{d['withdrawals_eur']:+,.0f}" if d['withdrawals_eur'] else "", neg_color),
]
for ci, (txt, color) in enumerate(vals):
ax_eur.text(eur_x[ci], y, txt,
transform=ax_eur.transAxes,
color=color, fontsize=8, va="top", family="monospace")
# EUR totals
y_total_e = y_start_e - 0.06 - len(months) * row_h
ax_eur.plot([0.01, 0.99], [y_total_e + row_h * 0.45, y_total_e + row_h * 0.45],
color="#3a3a5a", linewidth=0.8, transform=ax_eur.transAxes)
total_net_usd_all = total_net + total_wire_fees
net_flow_total = total_deps + total_wds
eur_totals = [
("TOTAL", "white"),
(f"{total_buys*to_eur:,.0f}", TICK_COL),
(f"{total_sells*to_eur:,.0f}", TICK_COL),
(f"{total_pnl*to_eur:+,.2f}", pos_color if total_pnl >= 0 else neg_color),
(f"{total_comm*to_eur:,.2f}", "#FF9800"),
(f"{total_clear*to_eur:,.2f}", "#FF9800"),
(f"{total_nfa*to_eur:,.2f}", "#FF9800"),
(f"{total_wire_fees*to_eur:,.2f}", "#FF9800"),
(f"{total_net_usd_all*to_eur:+,.2f}", pos_color if total_net_usd_all >= 0 else neg_color),
(f"{total_deps:,.0f}", "#2196F3"),
(f"{total_wds:+,.0f}", neg_color),
]
for ci, (txt, color) in enumerate(eur_totals):
ax_eur.text(eur_x[ci], y_total_e, txt,
transform=ax_eur.transAxes,
color=color, fontsize=9, va="top", fontweight="bold",
family="monospace")
# ── 6. Finnish Tax Summary (Veroilmoitus) ──────────────────────────────
ax_tax.axis("off")
ax_tax.set_title("Veroilmoitus — Finnish Tax Summary (EUR)", color="white", fontsize=11, pad=8)
disposals, luovutushinnat, luovutusvoitot, luovutustappiot = \
calc_finnish_tax(data, year, eur_rate, multipliers)
total_kulut = sum(d["kulut_eur"] for d in disposals)
total_hankintameno = sum(d["hankintameno_eur"] for d in disposals)
tax_cols = ["Month", "Symbol", "Sell Qty", "Buy Qty", "Luovutushinta", "Kulut", "Hankintameno", "Voitto/Tappio"]
tax_x = [0.01, 0.10, 0.18, 0.27, 0.36, 0.50, 0.61, 0.78]
y_start_t = 0.95
# Note about fees
ax_tax.text(0.01, y_start_t + 0.03, "Kulut (fees) included in hankintameno, allocated by contract count",
transform=ax_tax.transAxes, color="#888888", fontsize=7, va="top", family="monospace")
for ci, label in enumerate(tax_cols):
ax_tax.text(tax_x[ci], y_start_t, label,
transform=ax_tax.transAxes,
color="#aaddff", fontsize=8, fontweight="bold",
va="top", family="monospace")
ax_tax.plot([0.01, 0.99], [y_start_t - 0.03, y_start_t - 0.03],
color="#3a3a5a", linewidth=0.8, transform=ax_tax.transAxes)
tax_row_h = 0.055
for ri, disp in enumerate(disposals):
y = y_start_t - 0.06 - ri * tax_row_h
result_color = pos_color if disp["tulos_eur"] >= 0 else neg_color
vals = [
(disp["month"], "white"),
(disp["symbol"], TICK_COL),
(str(disp["sell_qty"]), TICK_COL),
(str(disp["buy_qty"]), TICK_COL),
(f"\u20ac{disp['luovutushinta_eur']:,.2f}", TICK_COL),
(f"\u20ac{disp['kulut_eur']:,.2f}", "#FF9800"),
(f"\u20ac{disp['hankintameno_eur']:,.2f}", TICK_COL),
(f"\u20ac{disp['tulos_eur']:+,.2f}", result_color),
]
for ci, (txt, color) in enumerate(vals):
ax_tax.text(tax_x[ci], y, txt,
transform=ax_tax.transAxes,
color=color, fontsize=8, va="top", family="monospace")
# Summary totals
y_totals = y_start_t - 0.06 - len(disposals) * tax_row_h
ax_tax.plot([0.01, 0.99], [y_totals + tax_row_h * 0.45, y_totals + tax_row_h * 0.45],
color="#3a3a5a", linewidth=0.8, transform=ax_tax.transAxes)
summary_lines = [
(f"Luovutushinnat yht. (Total sale prices): \u20ac{luovutushinnat:,.2f}", TICK_COL),
(f"Hankintamenot yht. (Total acquisition cost): \u20ac{total_hankintameno:,.2f} (incl. kulut \u20ac{total_kulut:,.2f})", TICK_COL),
(f"Luovutusvoitot yht. (Total capital gains): \u20ac{luovutusvoitot:+,.2f}", pos_color),
(f"Luovutustappiot yht. (Total capital losses): \u20ac{luovutustappiot:+,.2f}", neg_color),
(f"Netto (Net gain/loss): \u20ac{luovutusvoitot + luovutustappiot:+,.2f}",
pos_color if (luovutusvoitot + luovutustappiot) >= 0 else neg_color),
]
for si, (txt, color) in enumerate(summary_lines):
ax_tax.text(0.01, y_totals - si * tax_row_h, txt,
transform=ax_tax.transAxes,
color=color, fontsize=9, va="top", fontweight="bold",
family="monospace")
# ── Save ─────────────────────────────────────────────────────────────────
script_dir = os.path.dirname(os.path.abspath(__file__)) or os.getcwd()
out_path = os.path.join(script_dir, f"phillipcapital_report_{year}.png")
fig.savefig(out_path, dpi=150, bbox_inches="tight", facecolor=fig.get_facecolor())
plt.close(fig)
print(f"\nReport saved: {out_path}")
# ── Main ─────────────────────────────────────────────────────────────────────
config = load_config()
multipliers = config["multipliers"]
print(f"Loaded {len(multipliers)} contract multipliers from config.json")
pdf_file = find_pdf()
print(f"\nParsing: {os.path.basename(pdf_file)}\n")
year = pick_year(pdf_file)
print()
# Derive EUR/USD rate from deposit data in the PDF
avg_rate = derive_eur_usd_rate(pdf_file, year)
if avg_rate:
print(f"Derived avg EUR/USD rate from deposits: {avg_rate:.4f}")
ans = input(f"Use this rate? (Y/n, or enter custom rate): ").strip()
if ans.lower() == "n":
eur_rate = float(input("Enter EUR/USD rate: ").strip())
elif ans and ans.replace(".", "").replace(",", "").isdigit():
eur_rate = float(ans.replace(",", "."))
else:
eur_rate = avg_rate
else:
eur_rate = float(input("Enter EUR/USD rate (e.g. 1.08): ").strip())
print()
data = parse_trades(pdf_file, year, multipliers)
# ── Debug dump ───────────────────────────────────────────────────────────────
debug_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "debug_output.txt")
with open(debug_path, "w") as dbg:
to_eur = 1.0 / eur_rate
months = sorted(data.keys())
for month in months:
d = data[month]
label = f"{MONTH_NAMES.get(month[:2], month[:2])} {year}"
dbg.write(f"\n{'='*80}\n{label}\n{'='*80}\n")
# Per-contract raw data
for sym in sorted(d["contracts"].keys()):
c = d["contracts"][sym]
mult = multipliers.get(sym, 1)
buys_raw = c["buys"]
sells_raw = c["sells"]
buys_usd = buys_raw * mult
sells_usd = sells_raw * mult
pnl_calc = sells_usd - buys_usd
dbg.write(f" {sym} (mult={mult}):\n")
dbg.write(f" buy_qty={c['buy_qty']} sell_qty={c['sell_qty']}\n")
dbg.write(f" buys_raw (price*qty) = {buys_raw:.2f}\n")
dbg.write(f" sells_raw (price*qty) = {sells_raw:.2f}\n")
dbg.write(f" buys_usd (raw*mult) = {buys_usd:.2f}\n")
dbg.write(f" sells_usd (raw*mult) = {sells_usd:.2f}\n")
dbg.write(f" calc_pnl (sells-buys) = {pnl_calc:.2f}\n")
# Aggregated month totals
buys_tot, sells_tot, calc_pnl = calc_month_totals(data, month, multipliers)
net = d["pnl"] + d["commission"] + d["clearing_fee"] + d["nfa_fee"]
diff = round(calc_pnl - d["pnl"], 2)
dbg.write(f"\n AGGREGATED:\n")
dbg.write(f" total_buys_usd = {buys_tot:.2f}\n")
dbg.write(f" total_sells_usd = {sells_tot:.2f}\n")
dbg.write(f" sells - buys = {sells_tot - buys_tot:.2f}\n")
dbg.write(f" calc_pnl = {calc_pnl:.2f}\n")
dbg.write(f" pdf_pnl = {d['pnl']:.2f}\n")
dbg.write(f" diff = {diff:.2f}\n")
dbg.write(f" commission = {d['commission']:.2f}\n")
dbg.write(f" clearing_fee = {d['clearing_fee']:.2f}\n")
dbg.write(f" nfa_fee = {d['nfa_fee']:.2f}\n")
dbg.write(f" net_pnl (pdf) = {net:.2f}\n")
dbg.write(f" deposits_eur = {d['deposits_eur']:.2f}\n")
dbg.write(f" withdrawals_eur = {d['withdrawals_eur']:.2f}\n")
dbg.write(f" wire_fees_usd = {d['wire_fees_usd']:.2f}\n")
# EUR conversions
dbg.write(f"\n EUR (rate={eur_rate:.4f}, 1/rate={to_eur:.6f}):\n")
dbg.write(f" buys_eur = {buys_tot * to_eur:.2f}\n")
dbg.write(f" sells_eur = {sells_tot * to_eur:.2f}\n")
dbg.write(f" sells-buys_eur = {(sells_tot - buys_tot) * to_eur:.2f}\n")
dbg.write(f" pnl_eur = {d['pnl'] * to_eur:.2f}\n")
net_usd_all = net + d["wire_fees_usd"]
dbg.write(f" net_pnl_eur = {net_usd_all * to_eur:.2f}\n")
# Grand totals across all months
dbg.write(f"\n\n{'='*80}\n")
dbg.write(f"GRAND TOTALS (all months)\n")
dbg.write(f"{'='*80}\n")
g_buys = 0.0
g_sells = 0.0
g_pnl = 0.0
g_comm = 0.0
g_clear = 0.0
g_nfa = 0.0
g_wire = 0.0
g_deps = 0.0
g_wds = 0.0
for month in months:
d = data[month]
b, s, cp = calc_month_totals(data, month, multipliers)
g_buys += b
g_sells += s
g_pnl += d["pnl"]
g_comm += d["commission"]
g_clear += d["clearing_fee"]
g_nfa += d["nfa_fee"]
g_wire += d["wire_fees_usd"]
g_deps += d["deposits_eur"]
g_wds += d["withdrawals_eur"]
g_net = g_pnl + g_comm + g_clear + g_nfa
g_net_all = g_net + g_wire
dbg.write(f" total_buys_usd = {g_buys:,.2f}\n")
dbg.write(f" total_sells_usd = {g_sells:,.2f}\n")
dbg.write(f" total_pnl = {g_pnl:,.2f}\n")
dbg.write(f" total_commission = {g_comm:,.2f}\n")
dbg.write(f" total_clearing = {g_clear:,.2f}\n")
dbg.write(f" total_nfa = {g_nfa:,.2f}\n")
dbg.write(f" total_wire_fees = {g_wire:,.2f}\n")
dbg.write(f" net_pnl (no wire) = {g_net:,.2f}\n")
dbg.write(f" net_pnl (w/ wire) = {g_net_all:,.2f}\n")
dbg.write(f" deposits_eur = {g_deps:,.2f}\n")
dbg.write(f" withdrawals_eur = {g_wds:,.2f}\n")
dbg.write(f"\n EUR (rate={eur_rate:.4f}):\n")
dbg.write(f" total_buys_eur = {g_buys * to_eur:,.2f}\n")
dbg.write(f" total_sells_eur = {g_sells * to_eur:,.2f}\n")
dbg.write(f" total_pnl_eur = {g_pnl * to_eur:,.2f}\n")
dbg.write(f" net_pnl_eur = {g_net_all * to_eur:,.2f}\n")
# Also dump raw PDF lines that matched our parsing regexes
with open(debug_path, "a") as dbg:
dbg.write(f"\n\n{'#'*80}\n")
dbg.write(f"RAW PDF LINES MATCHED\n")
dbg.write(f"{'#'*80}\n")
yy = year[2:]