Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support "transfer to spouse" transaction #549

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions cgt_calc/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def __init__(

self.acquisition_list: HmrcTransactionLog = {}
self.disposal_list: HmrcTransactionLog = {}
self.transfer_to_spouse_list: HmrcTransactionLog = {}
self.bnb_list: HmrcTransactionLog = {}

self.portfolio: dict[str, Position] = defaultdict(Position)
Expand Down Expand Up @@ -345,6 +346,32 @@ def convert_to_hmrc_transactions(
elif transaction.action is ActionType.WIRE_FUNDS_RECEIVED:
amount = get_amount_or_fail(transaction)
new_balance += amount
elif transaction.action is ActionType.TRANSFER_TO_SPOUSE:
symbol = transaction.symbol
quantity = transaction.quantity
if symbol is None:
raise SymbolMissingError(transaction)
if quantity is None or quantity <= 0:
raise QuantityNotPositiveError(transaction)
if self.portfolio[symbol].quantity < quantity:
raise InvalidTransactionError(
transaction,
"Tried to give to spouse more than the available "
f"balance({self.portfolio[symbol].quantity})",
)
current_quantity = self.portfolio[symbol].quantity
current_amount = self.portfolio[symbol].amount
amount = current_amount * quantity / current_quantity
self.portfolio[symbol] -= Position(quantity, amount)
new_balance += amount
add_to_list(
self.transfer_to_spouse_list,
transaction.date,
symbol,
quantity,
self.converter.to_gbp_for(amount, transaction),
self.converter.to_gbp_for(transaction.fees, transaction),
)
elif transaction.action is ActionType.REINVEST_DIVIDENDS:
print(f"WARNING: Ignoring unsupported action: {transaction.action}")
else:
Expand Down Expand Up @@ -768,6 +795,25 @@ def calculate_capital_gain(
calculation_log[spin_off.date][
f"spin-off${spin_off.source}"
] = [spin_off_entry]
if date_index in self.transfer_to_spouse_list:
for symbol in self.transfer_to_spouse_list[date_index]:
transaction = self.transfer_to_spouse_list[date_index][symbol]
current_quantity = self.portfolio[symbol].quantity
current_amount = self.portfolio[symbol].amount
amount = current_amount * transaction.quantity / current_quantity
self.portfolio[symbol] -= Position(transaction.quantity, amount)
transfer_entry = CalculationEntry(
rule_type=RuleType.TRANSFER_TO_SPOUSE,
quantity=transaction.quantity,
amount=-amount,
new_quantity=self.portfolio[symbol].quantity,
new_pool_cost=self.portfolio[symbol].amount,
fees=transaction.fees,
allowable_cost=amount,
)
calculation_log[date_index][f"transfer-to-spouse${symbol}"] = [
transfer_entry
]
print("\nSecond pass completed")
allowance = CAPITAL_GAIN_ALLOWANCES.get(self.tax_year)

Expand Down
2 changes: 2 additions & 0 deletions cgt_calc/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class ActionType(Enum):
WIRE_FUNDS_RECEIVED = 14
STOCK_SPLIT = 15
CASH_MERGER = 16
TRANSFER_TO_SPOUSE = 17


@dataclass
Expand All @@ -91,6 +92,7 @@ class RuleType(Enum):
SAME_DAY = 2
BED_AND_BREAKFAST = 3
SPIN_OFF = 4
TRANSFER_TO_SPOUSE = 5


class CalculationEntry: # noqa: SIM119 # this has non-trivial constructor
Expand Down
1 change: 1 addition & 0 deletions cgt_calc/parsers/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class RawTransaction(BrokerTransaction):
Represents a single raw transaction.
Example format:
2023-03-16,TRANSFER_TO_SPOUSE,META,21.5,0.0,0.0,USD
2023-02-09,DIVIDEND,OPRA,4200,0.80,0.0,USD
2022-11-14,SELL,META,19,116.00,0.05,USD
2022-08-15,BUY,META,105,180.50,0.00,USD
Expand Down
3 changes: 3 additions & 0 deletions cgt_calc/resources/template.tex.j2
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
\BLOCK{ elif symbol.startswith("spin-off$") }
\BLOCK{ set symbol = symbol[9:] }
\BLOCK{ set is_spin_off = True }
\BLOCK{ elif symbol.startswith("transfer-to-spouse$") }
\BLOCK{ set symbol = symbol[19:] }
\BLOCK{ set is_transfer_to_spouse = True }
\BLOCK{ endif }
\BLOCK{ set overall_quantity = entries|sum(attribute="quantity") }
\BLOCK{ set overall_fees = round_decimal(entries|sum(attribute="fees"), 2) }
Expand Down
3 changes: 2 additions & 1 deletion tests/test_data/raw/test_data.csv
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
2022-07-26,DIVIDEND,OTGLY,305,0.031737,0.0,USD
2022-06-06,STOCK_SPLIT,AMZN,209,0.00,0.00,USD
2022-05-15,BUY,FB,104,198.62,0.00,USD
2022-05-12,TRANSFER_TO_SPOUSE,OPRA,10,0.0,0.00,USD
2022-05-11,BUY,BABA,10,82.50,0.00,USD
2022-05-09,BUY,OPRA,60,5.35,0.00,USD
2022-05-09,BUY,OPRA,70,5.35,0.00,USD
2022-05-09,BUY,BABA,5,85.50,0.00,USD
2022-05-09,BUY,BABA,15,85.50,0.00,USD
2022-05-09,BUY,OPRA,60,5.35,0.00,USD
Expand Down