See through a fund to what it actually holds

Merged
JA
2mo ago
+391-6

Part of #122 (slice 3.7, the epic's stretch item). Triage plan: https://github.com/jaysbeekay/sure/issues/122#issuecomment-5741606711

The problem

3.1 deliberately refuses to give an ETF an asset class, and is right to — a bond
ETF and an equity ETF are the same wrapper around different things, so the wrapper
implies nothing. The consequence is that a portfolio held through funds, which is
most index investors, charts as almost entirely Unclassified.

Worse than empty, it is misleading where it does classify. Someone holding a
world ETF and a single US tech stock sees one equity slice and one unclassified
slice, when their real technology exposure is the stock plus roughly a fifth of
the fund
. Concentration is invisible exactly where it matters.

The provider question, answered from the code

You chose a provider feed over CSV upload. The provider is EODHD, and the
reason is that we already call the endpoint: Provider::Eodhd#fetch_security_info
requests /api/fundamentals/{ticker} and reads only the General block. That same
response carries ETF_Data.Holdings — keyed by suffixed ticker, each entry with
Code, Name and Assets_%.

So constituents cost no additional provider call, which is the identical
argument 3.1 made for sector and industry. Nothing new to configure; a deployment
without EODHD gets no look-through rather than an error.

What changed

  • SecurityInfo gains constituents:, defaulted to nil in the initialize
    override — exactly how 3.1 added sector and industry. The Data has eleven
    implementers and every one would raise at construction without the default.
  • Provider::Eodhd maps ETF_Data.Holdings. Other providers return nothing.
  • security_constituents + Security::Constituent, and constituents_fetched_at
    on securities.
  • import_provider_details(include_constituents:), mirroring
    include_classification: so an importer opts in and sync_prices does not
    inherit the cost.
  • Security#look_through_weights — the fund expanded, normalised.

Two decisions worth the reviewer's attention

The gate is keyed on the timestamp, not on presence. Most securities are not
funds, so the provider returns nothing for them — and a presence-keyed gate would
re-ask every one of those on every sync, for ever. constituents_fetched_at is
written whether or not anything came back, which is what lets "never asked" be told
from "asked, nothing there". Same shape and same reasoning as 3.1's gate.

Weights normalise against their actual sum, not against 100. A fund's reported
holdings routinely fall short — cash, rounding, securities lending — so dividing by
100 silently under-reports every constituent and the expansion stops adding up to
the position it replaces. The test uses holdings summing to 99.4 and asserts the
naive answer differs, so it cannot pass against a /100 implementation.

A defect this turned up

Chasing a mutation that wasn't caught found a real bug in my own first cut.
weight validated greater_than: 0. EODHD rounds Assets_% and returns 0 for
a position too small to round up to 0.01 — so that row failed validation, create!
raised inside store_constituents, and the transaction took the entire fund's
holdings down with it
. One negligible position cost the whole look-through.

Widened to greater_than_or_equal_to: 0, with a test importing a fund containing a
0% holding and asserting all three constituents survive. That also made the
zero-sum guard reachable for the first time (a fund whose every weight is 0), which
now has its own test.

Evidence

Written before the implementation and watched to fail — all 12 errored first.

Two gate tests initially failed, and the gate was right, not the tests:
securities(:aapl) has no logo or website, so has_metadata is false and
import_provider_details refetches for metadata reasons regardless. 3.1's tests
set those first for the same reason; mine now do too, with a comment saying why.

Ten mutations, all caught:

MutationCaught by
normalise against 100 instead of the actual sumthe 99.4 test
zero-sum guard removedall-weights-zero
gate keyed on presence instead of the timestamp2 gate tests
timestamp never written, gate never closes2 gate tests
re-import merges instead of replacingre-import replaces
opt-in ignored, every caller paysnot-opted-in
EODHD keys on the suffixed key, not Codebare-Code test
EODHD reads the wrong block name2 provider tests
constituents has no defaulteleven-implementers test
weight refuses zero againthe two new zero tests

Checks:

  • Full suite: 9951 runs, 41546 assertions, 0 failures, 0 errors, 33 skips.
  • bin/rubocop: 2776 files, no offenses. brakeman: 0 warnings, 0 errors.
  • Schema hygiene: db:prepare re-dumped db/schema.rb with ~30 unrelated
    check constraints rewritten into the other pg_get_constraintdef rendering form.
    Restored and hand-applied only the version line, the new table, the new column
    and the foreign key — 14 insertions, 1 deletion. Verified by dropping a
    database, loading the hand-patched schema from scratch and re-running all 14
    tests green.

What this leaves out

The "real exposure" toggle on the allocation views. It needs
ALLOCATION_GROUPINGS and classification_bucket, which are in #194 and not
merged — the same position as 3.5's allocation-by-tag. The model half lands here;
the toggle follows #194 rather than stacking two deep or duplicating that PR's work.