Stop refreshing prices after security positions close

Merged
JA
2mo ago
+475-74

Part of #267 and upstream #3856.

Upstream PR: we-promise/sure#3888 (draft). Different branch, same files: its branch up/3856-closed-security-prices has two commits on 20352e1a0 (head 526a7c34b), and its file tree is byte-identical to this PR's head a7875f6da. Any further change goes to both branches.

Behavior

  • Price windows come from each account's trades and positive holdings. Zero-quantity rows carried after a sale do not extend the window.
  • The daily importer combines the windows for accounts sharing a security and fetches prices only through the latest date any account needs. Account sync uses the same calculation.
  • A new, backdated or edited manual trade, long or short, reopens a position before holdings are rematerialized. A zero holding written after every trade change takes precedence over the raw trade sum, which can disagree after a reverse split. A deleted closing trade leaves nothing to compare, so it is picked up on the next sync.
  • Closed provider-held positions without trades now get a one-time historical price backfill through their last positive holding date. Complete closed windows stay quiet on later syncs.
  • Security details still refresh for every online security. That behavior is separate from price fetching.

Scope and cost

Account status does not close a position. Draft, disabled, and pending-deletion accounts remain in the daily window calculation while their holdings or trades exist. This preserves prices for retained account data; it can also keep an otherwise unused security current until the account or position is removed.

The daily calculation makes roughly five database reads per account with holdings or trades, replacing the former per-security start-date lookup. This trades database work for fewer provider requests. Measured locally: 1,000 accounts holding 20 shared tickers took 5,007 queries and 1.84 s, growing linearly with accounts.

The scheduled security health checker can still request current quotes. Security#current_price can also fetch today's missing price on demand when a page reads it, including an old trade or holding. This PR narrows the two price importers; it does not stop those paths.

Verification

  • Focused importer and window tests: 28 tests, 53 assertions, 0 failures.
  • Full Rails suite: 10,340 tests, 43,503 assertions, 0 failures, 0 errors, 33 skips (Docker devcontainer, PostgreSQL).
  • RuboCop on all changed Ruby files: no offenses. The flattened Codacy RuboCop config first added here (9752435f3) moved to #271 against fork main in 7ee760900, since it's fork tooling unrelated to this fix.
  • On 9752435f3, every check passed: the six GitHub Actions jobs (unit, system, Ruby lint, JS lint, Ruby scan, JS scan), CodeRabbit, and Codacy Static Code Analysis with 0 issues. 7ee760900 only removes the Codacy config; its checks are on the PR.

This PR targets base/267-upstream-main, an exact copy of upstream main at 20352e1a0. The fork's main has diverged, so this base keeps the PR diff confined to the fix.