Apply stock splits to holdings
Summary
Nothing in app/models/holding/ accounted for a stock split, so from a split's ex-date a position's share count, value and cost per share were all wrong — and stayed wrong, because the calculators rebuild history from trades. This records splits against the security, beside its prices, and applies them in both holding calculators at the open of the ex-date, before that day's trades.
The design choice worth knowing: the ratio is stored as two integers and every application goes through Security::Split.scale / .unscale, which multiply by the numerator and then divide by the denominator. Multiplying a BigDecimal by a Rational rounds at 32 digits, so 3 shares through a 1-for-3 split came back as 0.999…; this way the answer is exact whenever an exact answer exists.
Fixes #3781.
Draft: under active development. The branch is complete and green as it stands, but it is the first of several slices and I would rather land review comments on the shape before the rest follows. See the last section.
What changed
- 7571a6b76 Record stock splits against a security
- d48c50dc3 Apply stock splits in the holding calculators
- 03df5a73a Document split precision and which splits a portfolio applies
- ef0d8eb59 Pin splits across two accounts and a same-day sale to zero
- 013834a20 Round a split to the share count the column can hold
- 06d72057e Carry a split through a day that has no price
- fed3c55d4 Bring a stale provider snapshot forward before walking it back
- 6a04d5974 Restate a carried cost basis for the post-split share count
- 86cd3ae8c Seed the cost-basis replay from the snapshot brought forward
- 9b7b26c9d Carry a snapshot through splits before the account's history
16 files changed, 817 insertions(+), 19 deletions(-)
This is the model and calculator slice only. Nothing writes a security_splits row yet: provider split activity, split feeds from the price providers, the price convention and manual entry are separate changes.
Demo data
No demo data needed: Demo::Generator writes no split rows, and with none the calculators produce holdings identical to today's — which is the regression guard listed below rather than an omission.
Screenshots
No UI in this change.
Migration notes
One new table, no backfill and no existing row rewritten.
security_splits: uuid primary key; security_id uuid, not null, foreign key with on_delete: :cascade; ex_date date, not null; numerator and denominator integers, not null; source string, not null; timestamps. A unique index on (security_id, ex_date), and two check constraints — numerator > 0 AND denominator > 0, and numerator <> denominator, since a 1:1 split changes nothing.
Nothing to run after deploy. Holdings are recalculated from trades as they already are, and with no split rows the result is unchanged.
Verification
Ruby 3.4.9, PostgreSQL 16, head 9b7b26c9d (rebased onto main eb34c06f0 on 6 Oct).
bin/rails test: 10,595 runs, 45,053 assertions, 0 failures, 0 errors, 33 skips.bin/rails test test/models/holding test/models/security test/models/account: 337 runs, 1,282 assertions, 0 failures.bin/rubocopon the changed models, migration and tests: 23 files, no offenses.bin/brakeman -q: no warnings found.bundle exec erb_lintandnpm run lint: not run — no ERB or JavaScript changed.
Against the acceptance criteria this slice was written to: a forward-calculated position holds N shares the day before a 2:1 ex-date and 2N on it, with total cost basis unchanged and cost per share halved; a reverse-calculated position holds half of today's count the day before; a 1:10 reverse split scales both ways with cost basis unchanged; and a security with no split rows produces holdings identical to today's.
Blast radius
A review round on 2026-09-29 moved three fixes into files this PR had not previously touched — materializer.rb, gapfillable.rb and portfolio_snapshot.rb. Applying splits in the calculators is what makes those paths wrong, so they belong to this slice rather than a later one; the comment thread below has the detail.
Beyond Security::Split and its table:
Holding::ForwardCalculatorandHolding::ReverseCalculator— every holding series is rebuilt through these, so a split applied at the wrong boundary would move every position's history. Both directions are covered, including a position held in two accounts and a same-day sale to zero.Holding::PortfolioCache— gains the split lookup for the securities a portfolio holds; it batches, so no per-security query is added to a series rebuild.Holding::CostBasisTracker— cost per share moves by the inverse of the ratio while total cost basis is untouched, which is what the cost-basis assertions pin.Security#split_factor_betweenis new and has no other caller yet.
The guard for all of them is that with no split rows every path is arithmetically identical to before, and the suite above runs with none.
Still to come
This is the model and calculator slice. The remaining work, each its own change:
- provider split activity (SnapTrade, Plaid, Indexa) becoming a split row rather than a trade, plus the dedupe path so a split is never applied twice, and moving splits with prices in
securities:deduplicate; - split feeds from the price providers, once each API's split data is confirmed;
- one price convention — storing unadjusted prices and converting a provider's adjusted
closeback through the split table. This one touches every price chart and holding value, so it goes last and alone; - manual entry on the security, with a lock a provider cannot overwrite.
Two questions are open and would change the later slices rather than this one: what each provider's close actually is, and whether SPLIT activity carries the delta or the new total. Neither affects the table or the calculators here.
A fork pre-flight runs the same commits through CI and the review bots first: jaysbeekay/sure#260.
Summary by CodeRabbit
- New Features
- Added support for forward and reverse stock splits in portfolio share quantities and per-share cost basis.
- Holdings account for splits when carrying positions and provider-sourced cost basis across dates.
- Historical holdings and cost-basis calculations reflect splits while preserving total cost and applying same-day splits before trades.
- Split-adjusted quantities and cost basis are maintained for fractional shares and across dates without prices.