Skip to content

Commit

Permalink
Fix spin-offs calculations (#524)
Browse files Browse the repository at this point in the history
* Fix spin-offs calculations

* Fix linter errors.

* Address review feedback.

* * Add spin-offs caching in the filesystem
* Fix calculations for case where there were multiple buy and sell transactions before spin-off - make spin-off a CalculationEntry
* Adjust pdf formating
* Add test for spin-offs.

* Add missing file.

* Fix typing in spin_off_handler.py.
  • Loading branch information
nihn committed Jun 18, 2024
1 parent a77066e commit e7355c8
Show file tree
Hide file tree
Showing 13 changed files with 764 additions and 220 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ env
dist
calculations.pdf
exchange_rates.csv
spin_offs.csv
13 changes: 12 additions & 1 deletion cgt_calc/args_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import argparse
import datetime

from .const import DEFAULT_EXCHANGE_RATES_FILE, DEFAULT_REPORT_PATH
from .const import (
DEFAULT_EXCHANGE_RATES_FILE,
DEFAULT_REPORT_PATH,
DEFAULT_SPIN_OFF_FILE,
)


def get_last_elapsed_tax_year() -> int:
Expand Down Expand Up @@ -78,6 +82,13 @@ def create_parser() -> argparse.ArgumentParser:
nargs="?",
help="output file for monthly exchange rates from HMRC",
)
parser.add_argument(
"--spin-offs-file",
type=str,
default=DEFAULT_SPIN_OFF_FILE,
nargs="?",
help="output file for spin offs data",
)
parser.add_argument(
"--initial-prices",
type=str,
Expand Down
2 changes: 2 additions & 0 deletions cgt_calc/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
# Initial vesting and spin-off prices
DEFAULT_INITIAL_PRICES_FILE: Final = "initial_prices.csv"

DEFAULT_SPIN_OFF_FILE: Final = "spin_offs.csv"

# Latex template for calculations report
TEMPLATE_NAME: Final = "template.tex.j2"

Expand Down
20 changes: 20 additions & 0 deletions cgt_calc/current_price_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

from contextlib import suppress
import datetime
from decimal import Decimal

Expand All @@ -17,9 +18,11 @@ def __init__(
self,
converter: CurrencyConverter,
current_prices_data: dict[str, Decimal | None] | None = None,
historical_prices_data: dict[str, dict[datetime.date, Decimal]] | None = None,
):
"""Load data from exchange_rates_file and optionally from initial_data."""
self.current_prices_data = current_prices_data
self.historical_prices_data = historical_prices_data or {}
self.converter = converter

def get_current_market_price(self, symbol: str) -> Decimal | None:
Expand All @@ -35,3 +38,20 @@ def get_current_market_price(self, symbol: str) -> Decimal | None:
return self.converter.to_gbp(
market_price_usd, "USD", datetime.datetime.now().date()
)

def get_closing_price(self, symbol: str, date: datetime.date) -> Decimal:
"""Get the price of the share on closing time."""
with suppress(KeyError):
return self.historical_prices_data[symbol][date]

prices = yf.Ticker(symbol).history(
period="1d",
interval="1d",
start=date.strftime("%Y-%m-%d"),
end=(date + datetime.timedelta(days=1)).strftime("%Y-%m-%d"),
)
closing_price = prices.iloc[0]["Close"]
market_price_usd = Decimal(format(closing_price, ".15g"))
return self.converter.to_gbp(
market_price_usd, "USD", datetime.datetime.now().date()
)
Loading

0 comments on commit e7355c8

Please sign in to comment.