The personal finance app for everyone (by everyone)
Ruby
fix: add Schwab to SimpleFIN total-basis cost_basis allowlist (#2986)
Falloutmanfc130a1957d2
diff --git a/app/models/simplefin_account/investments/holdings_processor.rb b/app/models/simplefin_account/investments/holdings_processor.rbindex 29bf621550..ed23a8352d 100644--- a/app/models/simplefin_account/investments/holdings_processor.rb+++ b/app/models/simplefin_account/investments/holdings_processor.rb@@ -132,9 +132,10 @@ class SimplefinAccount::Investments::HoldingsProcessor# - cost_basis / basis: the spec calls this per-share, and most# brokerages comply. Keep these values unchanged by default.#- # Exception: a small allowlist of brokerages (Vanguard, Fidelity) is- # known to populate cost_basis with the total position cost in violation- # of the spec (#1718, #1182). For those connections only, divide by qty.+ # Exception: a small allowlist of brokerages (Vanguard, Fidelity, Schwab)+ # is known to populate cost_basis with the total position cost in+ # violation of the spec (#1718, #1182, #2626). For those connections+ # only, divide by qty.## An earlier revision of this fix used a magnitude heuristic# (share_price × √qty midpoint). It was withdrawn because a legitimate@@ -158,7 +159,7 @@ class SimplefinAccount::Investments::HoldingsProcessor# field with the total position cost rather than the per-share value the# spec requires. Matched as case-insensitive substrings against the# account's stored org name and domain.- TOTAL_BASIS_INSTITUTIONS = %w[vanguard fidelity].freeze+ TOTAL_BASIS_INSTITUTIONS = %w[vanguard fidelity schwab].freezedef institution_reports_total_basis?org = simplefin_account.respond_to?(:org_data) ? simplefin_account.org_data : nildiff --git a/test/models/simplefin_account/investments/holdings_processor_test.rb b/test/models/simplefin_account/investments/holdings_processor_test.rbindex a3842ac6a5..759f0965b1 100644--- a/test/models/simplefin_account/investments/holdings_processor_test.rb+++ b/test/models/simplefin_account/investments/holdings_processor_test.rb@@ -122,14 +122,14 @@ class SimplefinAccount::Investments::HoldingsProcessorTest < ActiveSupport::Testassert_equal BigDecimal("100.00"), cost_basisend- test "institution_reports_total_basis? matches Vanguard and Fidelity org metadata" do+ test "institution_reports_total_basis? matches Vanguard, Fidelity, and Schwab org metadata" docases = {{ "name" => "Vanguard" } => true,{ "name" => "VANGUARD BROKERAGE" } => true,{ "name" => "Fidelity Investments" } => true,{ "domain" => "vanguard.com" } => true,{ "domain" => "401k.fidelity.com" } => true,- { "name" => "Charles Schwab", "domain" => "schwab.com" } => false,+ { "name" => "Charles Schwab", "domain" => "schwab.com" } => true,{ "name" => "Chase" } => false,{} => false}@@ -143,6 +143,28 @@ class SimplefinAccount::Investments::HoldingsProcessorTest < ActiveSupport::Testendend+ test "cost_basis from Charles Schwab is divided by qty (#2626)" do+ # Schwab reports `cost_basis` as the total position cost, not per-share,+ # in violation of the SimpleFIN spec — same failure mode as Vanguard+ # (#1182) and Fidelity (#1718). Observed on two independent Sure+ # instances via raw SimpleFIN payloads, e.g.:+ # { "shares" => "651.00", "cost_basis" => "30162.36", "purchase_price" => "46.33235" }+ # Left uncorrected, Holding#calculate_trend later multiplies this+ # (mislabeled-as-per-share) total by qty again when reconstructing the+ # position's original cost, inflating a holding's unrealized loss by+ # roughly qty× — e.g. a $46,950 position showing a -99.8% / -$19.6M+ # "return" instead of its true +55.7% gain.+ cost_basis = @processor.send(+ :normalize_cost_basis,+ BigDecimal("30162.36"),+ BigDecimal("651"),+ "cost_basis",+ true # institution_reports_total_basis?+ )++ assert_in_delta 46.33, cost_basis.to_f, 0.01+ end+test "missing cost basis fields return nil" dopayload = {"market_value" => "10108.16"