Skip to content

Commit

Permalink
attempt to resolve the split issue
Browse files Browse the repository at this point in the history
  • Loading branch information
sygi committed Jul 2, 2024
1 parent 52189a6 commit 1ec704c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
13 changes: 11 additions & 2 deletions cgt_calc/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
)
from .parsers import read_broker_transactions, read_initial_prices
from .spin_off_handler import SpinOffHandler
from .transaction_log import add_to_list, has_key
from .transaction_log import add_to_list, has_key, multiply_entries
from .util import round_decimal

LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -123,7 +123,16 @@ def add_acquisition(
price, amount = self.handle_spin_off(transaction)
elif transaction.action is ActionType.STOCK_SPLIT:
price = Decimal(0)
amount = Decimal(0)
original = self.portfolio[symbol].quantity
multiple = Decimal((original + quantity) / original)
# Retroactively alter the acquisition / disposal list, instead of
# adding the split as a separate acquisition event.
multiply_entries(
self.acquisition_list, symbol, multiple, transaction.date)
multiply_entries(
self.disposal_list, symbol, multiple, transaction.date)
self.portfolio[symbol] += Position(quantity, Decimal(0))
return
else:
if price is None:
raise PriceMissingError(transaction)
Expand Down
12 changes: 12 additions & 0 deletions cgt_calc/transaction_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,15 @@ def add_to_list(
amount=amount,
fees=fees,
)

def multiply_entries(
current_list: HmrcTransactionLog,
symbol: str,
multiple: Decimal,
multiply_date: datetime.date | None = None
) -> None:
for date, entries in current_list.items():
if multiply_date is not None:
assert multiply_date >= date
entries[symbol].quantity *= multiple

20 changes: 3 additions & 17 deletions tests/test_data/calc_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,10 +1055,10 @@ def split_transaction(
"buy$FOO": [
CalculationEntry(
RuleType.SECTION_104,
quantity=Decimal(12),
quantity=Decimal(24),
amount=Decimal(-60),
allowable_cost=Decimal(60),
new_quantity=Decimal(12),
new_quantity=Decimal(24),
fees=Decimal(0),
new_pool_cost=Decimal(60),
),
Expand All @@ -1068,25 +1068,11 @@ def split_transaction(
"sell$FOO": [
CalculationEntry(
RuleType.SECTION_104,
quantity=Decimal(2),
quantity=Decimal(4),
amount=Decimal(12),
gain=Decimal(2),
allowable_cost=Decimal(10),
fees=Decimal(0),
new_quantity=Decimal(10),
new_pool_cost=Decimal(50),
),
],
},
datetime.date(day=15, month=5, year=2023): {
"split$FOO": [
CalculationEntry(
RuleType.SECTION_104,
quantity=Decimal(10),
amount=Decimal(0),
gain=Decimal(0),
fees=Decimal(0),
allowable_cost=Decimal(0),
new_quantity=Decimal(20),
new_pool_cost=Decimal(50),
),
Expand Down

0 comments on commit 1ec704c

Please sign in to comment.