fix(trades): count fees in gains and treat a missing rate as unknown
Refs #4022. Follow-up to #3758, as agreed with @jaysbeekay in its review (points 5 to 9).
Problem
#3758 put an acquisition fee into the calculated cost basis. Four places in the same area still disagreed with it:
- Realized gain compared
price * qtywith the average cost, so a disposal's fee was never deducted from what the sale brought in. - Unrealized gain per trade measured a lot against
price * qty, without the fee that lot cost. - The SQL fallback in
Holding#calculate_avg_cost, used when no basis was materialized, summed price times quantity only. The fallback and the calculators disagreed on what a position cost. - A missing exchange rate counted as 1:1.
convert_to_account_currencyfell back to the raw amount, and the SQL fallback usedCOALESCE(rate, 1).Trade#calculate_realized_gain_lossalready treats a missing rate as an unknown gain; the basis was the one place that still guessed.
Fix
- A disposal's fee comes off its proceeds before they are converted to the basis currency.
- A purchase's unrealized gain is measured against its price plus its fee.
- The SQL fallback includes each purchase's fee.
- A conversion with no rate returns nil.
Holding::CostBasisTrackerthen marks the average unknown until the position is fully closed, rather than skipping the buy, which would average the known buys over fewer units than are held. The SQL fallback returns nil for the same case, as it already does for an internal movement. A stored rate of zero or less counts as missing there, as it does inMoney#exchange_to.
Behaviour change
A holding with a foreign-currency purchase that has no rate for its day now shows no cost basis instead of one computed at 1:1. That is deliberate: the old figure looked measured and was not. It shows again once the rate exists.
Known limitation, not new
The SQL fallback averages every purchase the account ever made, including those of a position that was fully sold and bought again later. The calculators reset at a full close; the fallback never did. Its existing internal-movement check has the same scope, and so does the missing-rate check added here: a rateless purchase in a long-closed position still leaves today's fallback basis unknown. Scoping the fallback to the purchases since the last full close is its own change, and the materialized basis that normally takes precedence does not have this problem.
Not in this PR
Whether IBKR and Questrade should record commissions in trades.fee instead of as separate cash transactions. That changes providers, not calculations, and both book the commission correctly as cash today. Happy to do it separately if you want it.
Tests
Seven new tests, each checked to fail when its change is undone: realized gain net of a disposal's fee (cross-currency), unrealized gain including the fee, the fallback average including fees, the fallback and the forward calculator each returning no basis when a rate is missing, the fallback treating a stored rate of zero as missing, and the tracker staying unknown until a full close. HoldingTest's cross-currency average-cost test relied on the 1:1 fallback, so it now creates the rates its expected values assume.
bin/rails test test/models: 6,879 runs, 0 failures, on current main. Rubocop clean.
Summary by CodeRabbit
- Bug Fixes
- Purchase fees are included in average cost and unrealized gain calculations; sale fees are deducted from proceeds before currency conversion.
- Trade-provided exchange rates are used to convert foreign-currency trades when available. Without a valid rate or purchase price, cost basis remains unknown rather than using an unconverted amount.
- Average cost remains unknown while held purchases have unknown prices. After a position is fully closed, new purchases establish a fresh cost basis.