forked from KapJI/capital-gains-calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.py
executable file
·690 lines (651 loc) · 27.6 KB
/
calc.py
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
#!/usr/bin/env python3
import datetime
import decimal
import sys
from collections import defaultdict
from decimal import Decimal
from typing import Dict, List, Tuple
import render_latex
from dates import date_from_index, date_to_index, internal_start_date, is_date
from exceptions import (
AmountMissingError,
CalculatedAmountDiscrepancy,
CalculationError,
ExchangeRateMissingError,
InvalidTransactionError,
PriceMissingError,
QuantityNotPositiveError,
SymbolMissingError,
)
from misc import round_decimal
from model import (
ActionType,
BrokerTransaction,
CalculationEntry,
CalculationLog,
DateIndex,
RuleType,
)
from parsers import (
read_broker_transactions,
read_gbp_prices_history,
read_initial_prices,
)
# First year of tax year
tax_year = 2020
# Allowances
# https://www.gov.uk/guidance/capital-gains-tax-rates-and-allowances#tax-free-allowances-for-capital-gains-tax
capital_gain_allowances = {
2014: 11000,
2015: 11100,
2016: 11100,
2017: 11300,
2018: 11700,
2019: 12000,
2020: 12300,
}
# Schwab transactions
transaction_files = ["sample_transactions.csv"]
schwab_transactions_file = "schwab_transactions.csv"
# Montly GBP/USD history from
# https://www.gov.uk/government/collections/exchange-rates-for-customs-and-vat
gbp_history_file = "GBP_USD_monthly_history.csv"
# Initial vesting and spin-off prices
initial_prices_file = "initial_prices.csv"
# 6 April
tax_year_start_date = datetime.date(tax_year, 4, 6)
# 5 April
tax_year_end_date = datetime.date(tax_year + 1, 4, 5)
# For mapping of dates to int
HmrcTransactionLog = Dict[int, Dict[str, Tuple[Decimal, Decimal, Decimal]]]
gbp_history: Dict[int, Decimal] = {}
initial_prices: Dict[DateIndex, Dict[str, Decimal]] = {}
def gbp_price(date: datetime.date) -> Decimal:
assert is_date(date)
# Set day to 1 to get monthly price
index = date_to_index(date.replace(day=1))
if index in gbp_history:
return gbp_history[index]
else:
raise ExchangeRateMissingError("USD", date)
def get_initial_price(date: datetime.date, symbol: str) -> Decimal:
assert is_date(date)
date_index = date_to_index(date)
if date_index in initial_prices and symbol in initial_prices[date_index]:
return initial_prices[date_index][symbol]
else:
raise ExchangeRateMissingError(symbol, date)
def convert_to_gbp(amount: Decimal, currency: str, date: datetime.date) -> Decimal:
if currency == "USD":
return amount / gbp_price(date)
elif currency == "GBP":
return amount
else:
raise ExchangeRateMissingError(currency, date)
def date_in_tax_year(date: datetime.date) -> bool:
assert is_date(date)
return tax_year_start_date <= date and date <= tax_year_end_date
def has_key(transactions: HmrcTransactionLog, date_index: int, symbol: str) -> bool:
return date_index in transactions and symbol in transactions[date_index]
def add_to_list(
current_list: HmrcTransactionLog,
date_index: int,
symbol: str,
quantity: Decimal,
amount: Decimal,
fees: Decimal,
) -> None:
# assert quantity is not None
if date_index not in current_list:
current_list[date_index] = {}
if symbol not in current_list[date_index]:
current_list[date_index][symbol] = (Decimal(0), Decimal(0), Decimal(0))
current_quantity, current_amount, current_fees = current_list[date_index][symbol]
current_list[date_index][symbol] = (
current_quantity + quantity,
current_amount + amount,
current_fees + fees,
)
def add_acquisition(
portfolio: Dict[str, Decimal],
acquisition_list: HmrcTransactionLog,
transaction: BrokerTransaction,
) -> None:
symbol = transaction.symbol
quantity = transaction.quantity
if symbol is None:
raise SymbolMissingError(transaction)
if quantity is None or quantity <= 0:
raise QuantityNotPositiveError(transaction)
# This is basically only for data validation
if symbol in portfolio:
portfolio[symbol] += quantity
else:
portfolio[symbol] = quantity
# Add to acquisition_list to apply same day rule
if transaction.action in [ActionType.STOCK_ACTIVITY, ActionType.SPIN_OFF]:
amount = quantity * get_initial_price(transaction.date, symbol)
else:
if transaction.amount is None:
raise AmountMissingError(transaction)
if transaction.price is None:
raise PriceMissingError(transaction)
calculated_amount = round_decimal(
quantity * transaction.price + transaction.fees, 2
)
if transaction.amount != -calculated_amount:
raise CalculatedAmountDiscrepancy(transaction, -calculated_amount)
amount = -transaction.amount
add_to_list(
acquisition_list,
date_to_index(transaction.date),
symbol,
quantity,
convert_to_gbp(amount, transaction.currency, transaction.date),
convert_to_gbp(transaction.fees, transaction.currency, transaction.date),
)
def add_disposal(
portfolio: Dict[str, Decimal],
disposal_list: HmrcTransactionLog,
transaction: BrokerTransaction,
) -> None:
symbol = transaction.symbol
quantity = transaction.quantity
if symbol is None:
raise SymbolMissingError(transaction)
if symbol not in portfolio:
raise InvalidTransactionError(
transaction, "Tried to sell not owned symbol, reversed order?"
)
if quantity is None or quantity <= 0:
raise QuantityNotPositiveError(transaction)
if portfolio[symbol] < quantity:
raise InvalidTransactionError(
transaction,
f"Tried to sell more than the available balance({portfolio[symbol]})",
)
# This is basically only for data validation
portfolio[symbol] -= quantity
if portfolio[symbol] == 0:
del portfolio[symbol]
# Add to disposal_list to apply same day rule
if transaction.amount is None:
raise AmountMissingError(transaction)
if transaction.price is None:
raise PriceMissingError(transaction)
amount = transaction.amount
calculated_amount = round_decimal(
quantity * transaction.price - transaction.fees, 2
)
if amount != calculated_amount:
raise CalculatedAmountDiscrepancy(transaction, calculated_amount)
add_to_list(
disposal_list,
date_to_index(transaction.date),
symbol,
quantity,
convert_to_gbp(amount, transaction.currency, transaction.date),
convert_to_gbp(transaction.fees, transaction.currency, transaction.date),
)
def swift_date(date: datetime.date) -> str:
return date.strftime("%d/%m/%Y")
def convert_to_hmrc_transactions(
transactions: List[BrokerTransaction],
) -> Tuple[HmrcTransactionLog, HmrcTransactionLog]:
# We keep a balance per broker,currency pair
balance: Dict[Tuple[str, str], Decimal] = defaultdict(lambda: Decimal(0))
dividends = Decimal(0)
dividends_tax = Decimal(0)
interest = Decimal(0)
total_sells = Decimal(0)
portfolio: Dict[str, Decimal] = {}
acquisition_list: HmrcTransactionLog = {}
disposal_list: HmrcTransactionLog = {}
for i, transaction in enumerate(transactions):
new_balance = balance[(transaction.broker, transaction.currency)]
if transaction.action is ActionType.TRANSFER:
if transaction.amount is None:
raise AmountMissingError(transaction)
new_balance += transaction.amount
elif transaction.action is ActionType.BUY:
if transaction.amount is None:
raise AmountMissingError(transaction)
new_balance += transaction.amount
add_acquisition(portfolio, acquisition_list, transaction)
elif transaction.action is ActionType.SELL:
if transaction.amount is None:
raise AmountMissingError(transaction)
new_balance += transaction.amount
add_disposal(portfolio, disposal_list, transaction)
# TODO: cleanup
if date_in_tax_year(transaction.date):
total_sells += convert_to_gbp(
transaction.amount, transaction.currency, transaction.date
)
elif transaction.action is ActionType.FEE:
if transaction.amount is None:
raise AmountMissingError(transaction)
new_balance += transaction.amount
transaction.fees = -transaction.amount
transaction.quantity = Decimal(0)
gbp_fees = convert_to_gbp(
transaction.fees, transaction.currency, transaction.date
)
add_to_list(
acquisition_list,
date_to_index(transaction.date),
transaction.symbol,
transaction.quantity,
gbp_fees,
gbp_fees,
)
elif transaction.action in [ActionType.STOCK_ACTIVITY, ActionType.SPIN_OFF]:
add_acquisition(portfolio, acquisition_list, transaction)
elif transaction.action in [ActionType.DIVIDEND, ActionType.CAPITAL_GAIN]:
if transaction.amount is None:
raise AmountMissingError(transaction)
new_balance += transaction.amount
if date_in_tax_year(transaction.date):
dividends += convert_to_gbp(
transaction.amount, transaction.currency, transaction.date
)
elif transaction.action in [ActionType.TAX, ActionType.ADJUSTMENT]:
if transaction.amount is None:
raise AmountMissingError(transaction)
new_balance += transaction.amount
if date_in_tax_year(transaction.date):
dividends_tax += convert_to_gbp(
transaction.amount, transaction.currency, transaction.date
)
elif transaction.action is ActionType.INTEREST:
if transaction.amount is None:
raise AmountMissingError(transaction)
new_balance += transaction.amount
if date_in_tax_year(transaction.date):
interest += convert_to_gbp(
transaction.amount, transaction.currency, transaction.date
)
else:
raise InvalidTransactionError(
transaction, f"Action not processed({transaction.action})"
)
if new_balance < 0:
msg = f"Reached a negative balance({new_balance})"
msg += f" for broker {transaction.broker} ({transaction.currency})"
msg += " after processing the following transactions:\n"
msg += "\n".join(map(str, transactions[: i + 1]))
raise CalculationError(msg)
balance[(transaction.broker, transaction.currency)] = new_balance
print("First pass completed")
print("Final portfolio:")
for stock, quantity in portfolio.items():
print(f" {stock}: {round_decimal(quantity, 2)}")
print("Final balance:")
for (broker, currency), amount in balance.items():
print(f" {broker}: {round_decimal(amount, 2)} ({currency})")
print(f"Dividends: £{round_decimal(dividends, 2)}")
print(f"Dividend taxes: £{round_decimal(-dividends_tax, 2)}")
print(f"Interest: £{round_decimal(interest, 2)}")
print(f"Disposal proceeds: £{round_decimal(total_sells, 2)}")
print("")
return acquisition_list, disposal_list
def process_acquisition(
acquisition_list: HmrcTransactionLog,
bed_and_breakfast_list: HmrcTransactionLog,
portfolio: Dict[str, Tuple[Decimal, Decimal]],
symbol: str,
date_index: int,
) -> List[CalculationEntry]:
acquisition_quantity, acquisition_amount, acquisition_fees = acquisition_list[
date_index
][symbol]
original_acquisition_amount = acquisition_amount
if symbol not in portfolio:
portfolio[symbol] = (Decimal(0), Decimal(0))
current_quantity, current_amount = portfolio[symbol]
calculation_entries = []
# Management fee transaction can have 0 quantity
assert acquisition_quantity >= 0
assert acquisition_amount > 0
bed_and_breakfast_quantity = Decimal(0)
bed_and_breakfast_amount = Decimal(0)
if acquisition_quantity > 0:
acquisition_price = acquisition_amount / acquisition_quantity
if has_key(bed_and_breakfast_list, date_index, symbol):
(
bed_and_breakfast_quantity,
bed_and_breakfast_amount,
bed_and_breakfast_fees,
) = bed_and_breakfast_list[date_index][symbol]
assert bed_and_breakfast_quantity <= acquisition_quantity
acquisition_amount -= bed_and_breakfast_quantity * acquisition_price
acquisition_amount += bed_and_breakfast_amount
assert acquisition_amount > 0
calculation_entries.append(
CalculationEntry(
rule_type=RuleType.BED_AND_BREAKFAST,
quantity=bed_and_breakfast_quantity,
amount=-bed_and_breakfast_amount,
new_quantity=current_quantity + bed_and_breakfast_quantity,
new_pool_cost=current_amount + bed_and_breakfast_amount,
fees=bed_and_breakfast_fees,
allowable_cost=original_acquisition_amount,
)
)
portfolio[symbol] = (
current_quantity + acquisition_quantity,
current_amount + acquisition_amount,
)
if (
acquisition_quantity - bed_and_breakfast_quantity > 0
or bed_and_breakfast_quantity == 0
):
calculation_entries.append(
CalculationEntry(
rule_type=RuleType.SECTION_104,
quantity=acquisition_quantity - bed_and_breakfast_quantity,
amount=-(acquisition_amount - bed_and_breakfast_amount),
new_quantity=current_quantity + acquisition_quantity,
new_pool_cost=current_amount + acquisition_amount,
fees=acquisition_fees,
allowable_cost=original_acquisition_amount,
)
)
return calculation_entries
def process_disposal(
acquisition_list: HmrcTransactionLog,
disposal_list: HmrcTransactionLog,
bed_and_breakfast_list: HmrcTransactionLog,
portfolio: Dict[str, Tuple[Decimal, Decimal]],
symbol: str,
date_index: int,
) -> Tuple[Decimal, List[CalculationEntry]]:
disposal_quantity, proceeds_amount, disposal_fees = disposal_list[date_index][
symbol
]
disposal_price = proceeds_amount / disposal_quantity
current_quantity, current_amount = portfolio[symbol]
assert disposal_quantity <= current_quantity
chargeable_gain = Decimal(0)
calculation_entries = []
# Same day rule is first
if has_key(acquisition_list, date_index, symbol):
same_day_quantity, same_day_amount, same_day_fees = acquisition_list[
date_index
][symbol]
bed_and_breakfast_quantity = Decimal(0)
if has_key(bed_and_breakfast_list, date_index, symbol):
(
bed_and_breakfast_quantity,
_bb_amount,
_bb_fees,
) = bed_and_breakfast_list[date_index][symbol]
assert bed_and_breakfast_quantity <= same_day_quantity
available_quantity = min(
disposal_quantity, same_day_quantity - bed_and_breakfast_quantity
)
if available_quantity > 0:
acquisition_price = same_day_amount / same_day_quantity
same_day_proceeds = available_quantity * disposal_price
same_day_allowable_cost = available_quantity * acquisition_price
same_day_gain = same_day_proceeds - same_day_allowable_cost
chargeable_gain += same_day_gain
# print(
# f"SAME DAY"
# f", quantity {available_quantity}"
# f", gain {same_day_gain}
# f", disposal price {disposal_price}"
# f", acquisition price {acquisition_price}"
# )
disposal_quantity -= available_quantity
proceeds_amount -= available_quantity * disposal_price
current_quantity -= available_quantity
# These shares shouldn't be added to Section 104 holding
current_amount -= available_quantity * acquisition_price
if current_quantity == 0:
assert current_amount == 0, f"current amount {current_amount}"
calculation_entries.append(
CalculationEntry(
rule_type=RuleType.SAME_DAY,
quantity=available_quantity,
amount=same_day_proceeds,
gain=same_day_gain,
allowable_cost=same_day_allowable_cost,
fees=same_day_fees,
new_quantity=current_quantity,
new_pool_cost=current_amount,
)
)
# Bed and breakfast rule next
if disposal_quantity > 0:
for i in range(30):
search_index = date_index + i + 1
if has_key(acquisition_list, search_index, symbol):
(
acquisition_quantity,
acquisition_amount,
acquisition_fees,
) = acquisition_list[search_index][symbol]
bed_and_breakfast_quantity = Decimal(0)
if has_key(bed_and_breakfast_list, search_index, symbol):
(
bed_and_breakfast_quantity,
_bb_amount,
_bb_fees,
) = bed_and_breakfast_list[search_index][symbol]
assert bed_and_breakfast_quantity <= acquisition_quantity
# This can be some management fee entry or already used
# by bed and breakfast rule
if acquisition_quantity - bed_and_breakfast_quantity == 0:
continue
print(
f"WARNING: Bed and breakfasting for {symbol}."
f" Disposed on {date_from_index(date_index)}"
f" and acquired again on {date_from_index(search_index)}"
)
available_quantity = min(
disposal_quantity, acquisition_quantity - bed_and_breakfast_quantity
)
acquisition_price = acquisition_amount / acquisition_quantity
bed_and_breakfast_proceeds = available_quantity * disposal_price
bed_and_breakfast_allowable_cost = (
available_quantity * acquisition_price
)
bed_and_breakfast_gain = (
bed_and_breakfast_proceeds - bed_and_breakfast_allowable_cost
)
chargeable_gain += bed_and_breakfast_gain
# print(
# f"BED & BREAKFAST"
# f", quantity {available_quantity}"
# f", gain {bed_and_breakfast_gain}"
# f", disposal price {disposal_price}"
# f", acquisition price {acquisition_price}"
# )
disposal_quantity -= acquisition_quantity
proceeds_amount -= available_quantity * disposal_price
current_price = current_amount / current_quantity
amount_delta = available_quantity * current_price
current_quantity -= available_quantity
current_amount -= amount_delta
if current_quantity == 0:
assert current_amount == 0, f"current amount {current_amount}"
add_to_list(
bed_and_breakfast_list,
search_index,
symbol,
available_quantity,
amount_delta,
Decimal(0),
)
calculation_entries.append(
CalculationEntry(
rule_type=RuleType.BED_AND_BREAKFAST,
quantity=available_quantity,
amount=bed_and_breakfast_proceeds,
gain=bed_and_breakfast_gain,
allowable_cost=bed_and_breakfast_allowable_cost,
# TODO: support fees
fees=acquisition_fees,
bed_and_breakfast_date_index=search_index,
new_quantity=current_quantity,
new_pool_cost=current_amount,
)
)
if disposal_quantity > 0:
allowable_cost = (
current_amount * Decimal(disposal_quantity) / Decimal(current_quantity)
)
chargeable_gain += proceeds_amount - allowable_cost
# print(
# f"SECTION 104"
# f", quantity {disposal_quantity}"
# f", gain {proceeds_amount - allowable_cost}"
# f", proceeds amount {proceeds_amount}"
# f", allowable cost {allowable_cost}"
# )
current_quantity -= disposal_quantity
current_amount -= allowable_cost
if current_quantity == 0:
assert (
round_decimal(current_amount, 10) == 0
), f"current amount {current_amount}"
calculation_entries.append(
CalculationEntry(
rule_type=RuleType.SECTION_104,
quantity=disposal_quantity,
amount=proceeds_amount,
gain=proceeds_amount - allowable_cost,
allowable_cost=allowable_cost,
# CHECK THIS!
fees=disposal_fees,
new_quantity=current_quantity,
new_pool_cost=current_amount,
)
)
portfolio[symbol] = (current_quantity, current_amount)
chargeable_gain = round_decimal(chargeable_gain, 2)
return chargeable_gain, calculation_entries
def calculate_capital_gain(
acquisition_list: HmrcTransactionLog,
disposal_list: HmrcTransactionLog,
) -> CalculationLog:
begin_index = date_to_index(internal_start_date)
tax_year_start_index = date_to_index(tax_year_start_date)
end_index = date_to_index(tax_year_end_date)
disposal_count = 0
disposal_proceeds = Decimal(0)
allowable_costs = Decimal(0)
capital_gain = Decimal(0)
capital_loss = Decimal(0)
bed_and_breakfast_list: HmrcTransactionLog = {}
portfolio: Dict[str, Tuple[Decimal, Decimal]] = {}
calculation_log: CalculationLog = {}
for date_index in range(begin_index, end_index + 1):
if date_index in acquisition_list:
for symbol in acquisition_list[date_index]:
calculation_entries = process_acquisition(
acquisition_list,
bed_and_breakfast_list,
portfolio,
symbol,
date_index,
)
if date_index >= tax_year_start_index:
if date_index not in calculation_log:
calculation_log[date_index] = {}
calculation_log[date_index][f"buy${symbol}"] = calculation_entries
if date_index in disposal_list:
for symbol in disposal_list[date_index]:
transaction_capital_gain, calculation_entries = process_disposal(
acquisition_list,
disposal_list,
bed_and_breakfast_list,
portfolio,
symbol,
date_index,
)
if date_index >= tax_year_start_index:
disposal_count += 1
transaction_disposal_proceeds = disposal_list[date_index][symbol][1]
disposal_proceeds += transaction_disposal_proceeds
allowable_costs += (
transaction_disposal_proceeds - transaction_capital_gain
)
transaction_quantity = disposal_list[date_index][symbol][0]
# print(
# f"DISPOSAL on {date_from_index(date_index)} of {symbol}"
# f", quantity {transaction_quantity}: "
# f"capital gain: ${round_decimal(transaction_capital_gain, 2)}"
# )
calculated_quantity = Decimal(0)
calculated_proceeds = Decimal(0)
calculated_gain = Decimal(0)
for entry in calculation_entries:
calculated_quantity += entry.quantity
calculated_proceeds += entry.amount
calculated_gain += entry.gain
assert transaction_quantity == calculated_quantity
assert round_decimal(
transaction_disposal_proceeds, 10
) == round_decimal(
calculated_proceeds, 10
), f"{transaction_disposal_proceeds} != {calculated_proceeds}"
assert transaction_capital_gain == round_decimal(calculated_gain, 2)
if date_index not in calculation_log:
calculation_log[date_index] = {}
calculation_log[date_index][f"sell${symbol}"] = calculation_entries
if transaction_capital_gain > 0:
capital_gain += transaction_capital_gain
else:
capital_loss += transaction_capital_gain
print("\nSecond pass completed")
print(f"Portfolio at the end of {tax_year}/{tax_year + 1} tax year:")
for symbol in portfolio:
quantity, amount = portfolio[symbol]
if quantity > 0:
print(
f" {symbol}: {round_decimal(quantity, 2)}, £{round_decimal(amount, 2)}"
)
disposal_proceeds = round_decimal(disposal_proceeds, 2)
allowable_costs = round_decimal(allowable_costs, 2)
capital_gain = round_decimal(capital_gain, 2)
capital_loss = round_decimal(capital_loss, 2)
print(f"For tax year {tax_year}/{tax_year + 1}:")
print(f"Number of disposals: {disposal_count}")
print(f"Disposal proceeds: £{disposal_proceeds}")
print(f"Allowable costs: £{allowable_costs}")
print(f"Capital gain: £{capital_gain}")
print(f"Capital loss: £{-capital_loss}")
print(f"Total capital gain: £{capital_gain + capital_loss}")
if tax_year in capital_gain_allowances:
allowance = capital_gain_allowances[tax_year]
print(
f"Taxable capital gain: £{max(Decimal(0), capital_gain + capital_loss - allowance)}"
)
else:
print("WARNING: Missing allowance for this tax year")
print("")
return calculation_log
def main() -> int:
# Throw exception on accidental float usage
decimal.getcontext().traps[decimal.FloatOperation] = True
# Read data from input files
broker_transactions = read_broker_transactions(schwab_transactions_file)
global gbp_history, initial_prices
gbp_history = read_gbp_prices_history(gbp_history_file)
initial_prices = read_initial_prices(initial_prices_file)
# First pass converts broker transactions to HMRC transactions.
# This means applying same day rule and collapsing all transactions with
# same type in the same day.
# It also converts prices to GBP, validates data and calculates dividends,
# taxes on dividends and interest.
acquisition_list, disposal_list = convert_to_hmrc_transactions(broker_transactions)
# Second pass calculates capital gain tax for the given tax year
calculation_log = calculate_capital_gain(acquisition_list, disposal_list)
render_latex.render_calculations(
calculation_log, tax_year=tax_year, date_from_index=date_from_index
)
print("All done!")
return 0
sys.exit(main())