Book Trade Republic trades on the linked cash account
Merged
NI
2mo ago+283+283+012345678901234567890123456789-40-40-01234567890123456789
Problem
Follow-up to #3669, found while running it in production with split portfolio and cash accounts.
- Buys and sells never reached the cash account. Trade Republic settles trades directly against the cash balance, but only the portfolio trade was imported. Deposits showed on the cash account; the purchases they paid for did not.
- Some trades stayed on the offline ISIN security. When a fund's trades were split across the ISIN security and the exchange ticker security (e.g.
IE00B5BMR087vsSXR8), calculated holdings for the ticker went negative. The validated holding update in the ISIN rematch then raised, so the first trade per fund never moved to the ticker and was missing from its holding on every sync. - Deposits counted as spending. Cash-account deposits carried the
Contributionactivity label, whichAccount::ProviderImportAdapterturns intoinvestment_contribution, a kind budgets count as an expense.
Change
ISIN rematch (DataHelpers)
- Trades are moved to the exchange security before holdings are touched.
- Derived holding rows are rewritten with
update_columns: while trades are split they can hold negative quantities that fail validation, and the next materialization replaces them anyway. - The collision debug log is one entry per rematch instead of one per holding row (thousands per sync before).
Cash settlement (ActivitiesProcessor)
- The cash account reads order executions from the portfolio payload (the importer keeps them there only) and books the timeline list amount, which already includes fees and taxes.
- Direction follows the sign of the timeline amount, falling back to the traded quantity. An order execution with neither signal is skipped rather than booked as a buy.
- Buys and Round Up:
investment_contribution. Sale proceeds:funds_movement. - None of them get a category, on purpose: they stay uncategorized until the user assigns one, manually or with a rule.
- Deleted or cancelled trades also remove their cash settlement through the existing lifecycle reconciliation.
- Saveback stays portfolio-only, since it is funded by Trade Republic.
- There is no transfer to the portfolio account on purpose: Trade Republic has no intermediate balance, the money leaves the cash balance directly.
Deposits
- Cash-account deposits no longer carry the
Contributionlabel. - Apple Pay, Google Pay and SEPA direct-debit top-ups are
funds_movement, like the other inbound transfers.BANK_TRANSACTION_INCOMINGstays standard, so income received on the Trade Republic account still counts as income. - Untouched deposits stored as
investment_contributionby earlier syncs are reset, and their auto-assigned Investment Contributions category andContributionlabel are cleared. User-modified entries and matched transfers are left alone.
Resulting behavior
- The first sync after this change backfills the cash account with the full trade history. Its balance history and budgets change accordingly.
- Out of scope: the portfolio account's historical cash still goes negative, since trades draw on cash it never receives. That was already the case before this PR.
- Independent of #3760, which fixes the current cash balance while orders are open. The two PRs touch different files.
Validation
- New Minitest coverage: the rematch with negative exchange holdings (fails without the fix), buy and sell settlement including kinds and categories, removal of a deleted trade's settlement, skipping a settlement without a direction, deposit kinds, and the legacy deposit reset including its label. Existing savings-plan, Round Up and Google Pay tests were updated to the new cash behavior.
- Dry run against a real Trade Republic connection, rolled back:
- All 186 trades import without errors (4 failed before). The 28-03-2024 buys now sit on
SXR8andRHM. - All 132 order executions get a cash settlement at the Trade Republic timeline amount: 95 buys as
investment_contribution, 37 sales asfunds_movement, none categorized. The amount sign never contradicts the traded quantity. - No cash-account deposits remain
investment_contribution.
- All 186 trades import without errors (4 failed before). The 28-03-2024 buys now sit on
- Trade Republic model tests: 190 runs, 0 failures. The full
bin/rails testand system tests run green in CI. bin/rubocopandbin/brakemanpass with no offenses or warnings. There are no ERB, JavaScript or UI changes, so erb_lint and Biome do not apply.
Summary by CodeRabbit
- New Features
- Cash-account activity linked to a portfolio now includes buy and sell settlements with amounts and transaction types that reflect trade direction.
- Savings-plan invoices appear as cash outflows alongside portfolio trades.
- SEPA direct debits, Apple Pay, and Google Pay inbound payments are recognized as transfers.
- Round Up cash transactions are labeled as investment contributions.
- Bug Fixes
- Cash deposits and inbound payments are less likely to be incorrectly labeled as investment contributions.
- ISIN trades and holdings are matched more reliably, including when split history results in negative holdings.