Skip to content

Commit

Permalink
schwab_equity_award_json.py parser: avoid unnecessary quantity recalc…
Browse files Browse the repository at this point in the history
…ulation. (#575)

This can introduce rounding errors.

This fixes #574.

This contribution has been developed in my spare time.
  • Loading branch information
m01 authored Dec 16, 2024
1 parent bb068b9 commit 5ad2d52
Show file tree
Hide file tree
Showing 3 changed files with 545 additions and 2 deletions.
17 changes: 15 additions & 2 deletions cgt_calc/parsers/schwab_equity_award_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ def __init__(self, row: JsonRowType, file: str, field_names: FieldNames) -> None
f"Schwab Equity Award JSON only supports GOOG stock but found {symbol}",
)
quantity = _decimal_from_number_or_str(row, names.quantity)
amount = _decimal_from_number_or_str(row, names.amount)
initially_parsed_amount = _decimal_from_number_or_str(row, names.amount)
amount = initially_parsed_amount
fees = _decimal_from_number_or_str(row, names.fees)
if row[names.action] == "Deposit":
if len(row[names.transac_details]) != 1:
Expand Down Expand Up @@ -279,7 +280,19 @@ def __init__(self, row: JsonRowType, file: str, field_names: FieldNames) -> None
" prices",
)

quantity = (amount + fees) / price
# Check if the transaction was lacking decimals. Sometimes
# we did just sell an integer number of shares.
# If we unconditionally perform the quantity update step
# here, small roundings that were necessary to determine
# the initially_parsed_amount on the broker side can cause
# tiny decimal deviations in the quantity, which can then
# cause errors later on, e.g. triggering assertions about
# selling more than was available.
if (
round_decimal(quantity * price - fees, 2)
!= initially_parsed_amount
):
quantity = (amount + fees) / price

else:
raise ParsingError(
Expand Down
Loading

0 comments on commit 5ad2d52

Please sign in to comment.