Skip to content

fix(wash-sale): block a loss only when FIFO really keeps replacement shares, and name the EEA in the market test - #461

Merged
GeiserX merged 5 commits into
mainfrom
fix/wash-sale-cr459
Nov 18, 2026
Merged

GeiserX merged 5 commits into
mainfrom
fix/wash-sale-cr459

Conversation

@GeiserX

@GeiserX GeiserX commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Problem

CodeRabbit reviewed #459 after it shipped in v0.66.8, and a second review of this fix found more. Four engine paths could block or release a deferred loss in the wrong place:

  1. Two cover buys on one day. When two buys covered a short on the same day, the detector shared out the covered shares in file order. FIFO uses its own order (a C;O buy before a C buy). So a C buy that covered nothing could count as a repurchase and block a loss with no replacement shares.
  2. Intraday flip from short to long. The shares the account keeps were not counted as a repurchase, so a loss the law defers was deducted at once.
  3. Total exit on a cover day. When a sale empties the position and a short is covered the same day, the losses the total exit releases could land on the short cover. Covers release nothing.
  4. TC row with no new security. It is booked as a merger of the ISIN into itself. It stamped its date as the entry date of shares already held, so an earlier sale looked like a total exit.
  5. Statement-only broker. With merged brokers, a broker with a statement but no trades did not count. Its later statement end could hide another broker's incomplete window.

The docs and the in-app hints also described the market test wrongly. They called NYSE- and Nasdaq-only shares a DeclaRenta deviation, and they put crypto under letter g.

Fix

  • The detector follows FIFO's processing order. It splits each day's covered shares by the day the short opened. A C;O buy covers only shorts opened earlier. A C buy covers what is left, then the same-day short, and keeps the rest as long shares. Over 8,000 random runs, each buy day's repurchases match the long shares FIFO keeps.
  • The total-exit remainder goes to the last long sale of the day.
  • The entry date is set only when the ISIN changes.
  • Every broker with a statement end counts in the coverage check.

Market wording, in the docs, CLAUDE.md, the engine comments and all 5 locales:

What users see

  • No fixture changes in any FX method. Only the window-past-data warning text changes.
  • A user with an intraday short-to-long flip, or a same-day C/C;O cover pair, gets the loss deferred as the law requires.
  • The hints say «Espacio Económico Europeo» instead of «UE».
  • If a filed year had such a flip, the loss was deducted too early. The usual correction applies: an autoliquidación rectificativa from 2024 on, a complementaria for 2023 and earlier.

Left on purpose

Follow-up to #459, from the CodeRabbit review of its exact diff on the fork mirror.

…ly brokers from moving deferred losses

Late review of #459 (CodeRabbit on the exact diff):

- Same-day covers now take the short in FIFO's processing order. FIFO runs a
  C;O buy before a C buy, so an orphan C buy listed first in the file was
  indexed as a repurchase and blocked a loss with no replacement shares.
  The comparator is now exported from fifo.ts (compareFifoTradeOrder) and
  used by both.
- The total exit gives the rest of the queue to the day's last long sale,
  never to a same-day short cover.
- A TC row that names no new security is booked as a merger of the ISIN
  into itself; it no longer stamps entryDate, so held shares do not look
  bought on the TC date and an earlier sale is not a total exit.
- The window-past-data coverage index counts a broker with a statement end
  and no trades, so its later statement end cannot hide another broker's
  incomplete window.
- The short-cover unit test flags the cover buy C, as FIFO data does.

No fixture number changes in any FX method.
…e equivalent markets (DGT V0778-25)

DGT V0778-25 (binding, 05/05/2025) reads art. 33.5.f to cover any
third-country market the European Commission declared equivalent to an EU
regulated market; Implementing Decision (EU) 2017/2320 lists NYSE and
Nasdaq. So a share listed only there gets the 2-month window, as the engine
already does. The 1-year window (33.5.g) is only for securities admitted to
neither, e.g. OTC Markets; that is the one case where the ISIN heuristic
deviates (#452).

The FAQ, casillas, modelos-fiscales and design docs, CLAUDE.md, the
anti-churning hints and the window-past-data warning (5 locales, plus the
Spanish text report.ts writes) now say this. guide-consistency pins the new
wording and rejects the EU-only one. No engine change. The design doc and
CLAUDE.md also record the late-review fixes of #459.
… as a repurchase

A C;O buy runs before the day's SELL O rows in FIFO and a C buy runs after
them, so a C;O buy can only cover a short opened on an earlier day. The
detector shared one day budget between them, gave the flip's same-day short
to the C;O buy, and then indexed nothing for the C buy that FIFO keeps: a
loss FIFO-held shares replace was deducted at once. Split the covered shares
by the short's open day (the short disposal's acquireDate).
…Kong as equivalent markets, and keep crypto out of letter g

V0778-25 places letter f's regulated markets in the whole European Economic
Area, so an Oslo-only share read as 1 year in the hints. Only Implementing
Decisions 2017/2318, 2017/2319 and 2017/2320 are in force (Switzerland's
lapsed on 30/06/2019, the UK has none), so a share listed only in London,
SIX or Toronto falls under letter g: the #452 gap is wider than OTC Markets.
Crypto is no «valor o participación»: the law applies art. 33.5.e to it,
which the engine does not (#457). Docs, CLAUDE.md, engine comments and the
hints in all 5 locales now say so; the guide test pins it.
@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@codecov

codecov Bot commented Nov 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.06%. Comparing base (c168b6c) to head (601d2b1).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #461   +/-   ##
=======================================
  Coverage   94.06%   94.06%           
=======================================
  Files          64       64           
  Lines        8338     8347    +9     
  Branches     2888     2892    +4     
=======================================
+ Hits         7843     7852    +9     
  Misses        407      407           
  Partials       88       88           
Files with missing lines Coverage Δ
src/engine/fifo.ts 96.84% <100.00%> (+<0.01%) ⬆️
src/engine/wash-sale.ts 98.78% <100.00%> (+0.02%) ⬆️
src/generators/report.ts 97.95% <ø> (ø)
src/i18n/locales/ca.ts 100.00% <ø> (ø)
src/i18n/locales/en.ts 100.00% <ø> (ø)
src/i18n/locales/es.ts 100.00% <ø> (ø)
src/i18n/locales/eu.ts 100.00% <ø> (ø)
src/i18n/locales/gl.ts 100.00% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@GeiserX
GeiserX merged commit fdf87ef into main Nov 18, 2026
7 of 8 checks passed
@GeiserX
GeiserX deleted the fix/wash-sale-cr459 branch November 18, 2026 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant