Keep Trade Republic cash balance intact for open orders

Merged
NI
2mo ago
+26-6

Problem

The Trade Republic importer took the cash balance from the availableCash topic when it was present. That value subtracts funds reserved for open orders, such as unfilled limit buys. Those orders haven't executed, so no money has left the account. As a result, the cash account in Sure dropped without a matching transaction and no longer matched the balance shown in Trade Republic.

Real-world example: two open limit buys (€211 and €157) made the cash balance €777.19 in Sure, while Trade Republic showed €1,145.19. On the first sync after the orders were placed, a reconciliation valuation absorbed the €368 drop.

Change

  • Provider::TradeRepublicClient#sync_once no longer subscribes to availableCash and only returns the booked cash amount and currency.
  • TradeRepublicItem::Importer#cash_balance no longer reads available_amount.
  • When an order executes, the cash moves through the timeline trade event as before.

Validation

  • New importer test: when the payload has a lower available_amount, the cash balance still equals the booked amount.
  • bin/rails test: 10,032 runs with 1 error, which also happens without this change. RecurringTransaction::PipelineTest#test_run_with_lock!_refuses_to_stack_on_a_held_family_lock reads config[:username], but database.yml sets user:. So when POSTGRES_USER differs from the OS user, the test connects as the wrong user.
  • bin/rubocop, erb_lint, npm run lint and bin/brakeman all pass with no offenses or warnings.
  • No system tests apply (no UI change).

Notes

  • Existing reconciliation valuations recorded while orders were open keep the reduced value. The next sync writes the correct current anchor.

Summary by CodeRabbit

  • Bug Fixes
    • Cash balances now reflect the provider’s booked cash amount rather than the lower amount available to spend. Imported cash accounts use the booked amount for both their current balance and cash balance.
    • Cash balance reporting no longer includes an available-balance amount or uses its currency as a fallback when a separate available-cash currency is missing.