Value positions from a daily price feed

A position was a share count. An instrument now carries a market symbol and
the last close fetched for it, so Wealth and the dashboard report cash plus
market value instead of cash alone.

The symbol is chosen by hand and never derived: one ISIN lists on several
exchanges in different currencies, and a price from the wrong listing misstates
wealth without failing any check. The refresh refuses a quote whose currency
differs from the instrument's, keeps the previous quote when a symbol cannot be
priced, and counts an instrument with no symbol as unpriced - naming it in a
check and leaving it out of every total, because cost is not value. The quote
belongs to the job: saving an instrument can neither set nor erase it, and
changing the symbol discards it.

Two things the provider forced. It answers HTTP 429 to every request whose
User-Agent names a programming language, so the client identifies as a browser;
without that header the first call of the day fails. Its closes are 32-bit
floats widened to 64 - 165.26 arrives as 165.25999450683594 - so a figure is
rounded to seven significant digits, which is what 24 mantissa bits carry;
eight would have stored 165.25999 as a price.

Accepted quotes are written in one commit against a revision re-read after the
fetches, and nothing is committed when no quote changed. The automatic run
starts shortly after launch and repeats daily on its own timer, so a sync
backoff cannot delay it and prices arrive with no bank connected.

Verified against live quotes end to end: 80 shares at 125.45 and 40 at 165.26
on 6000.00 cash report 22646.40 with one holding named as unpriced; giving that
holding a symbol through the UI moves the figure to 23530.50, and a second
refresh leaves the revision untouched.
This commit is contained in:
Lars Nolden
2026-09-12 18:42:07 +02:00
parent 2373790be3
commit 588c16ad19
19 changed files with 1580 additions and 55 deletions
+5 -1
View File
@@ -19,6 +19,7 @@ import (
"finance-duck/internal/classification"
"finance-duck/internal/domain"
"finance-duck/internal/journal"
"finance-duck/internal/quotes"
)
// Settings holds preferences only, never credentials. ClassifyOnImport controls
@@ -77,7 +78,10 @@ type App struct {
authStates map[string]authorization
callbackURL string
bankingSettings bankingSettings
syncRequested chan struct{}
// quotes needs no configuration: it reads a public endpoint, so its zero
// value is the working client and tests replace it with a stub.
quotes quotes.Client
syncRequested chan struct{}
}
// Settings this application has retired. They are read and discarded: a
+9
View File
@@ -77,6 +77,12 @@ func SaveInstrument(d *domain.Dataset, v domain.Instrument) error {
v.Name = strings.TrimSpace(v.Name)
v.ISIN = strings.ToUpper(strings.Join(strings.Fields(v.ISIN), ""))
v.Currency = strings.ToUpper(strings.TrimSpace(v.Currency))
v.Symbol = strings.TrimSpace(v.Symbol)
// A quote belongs to the price job: this endpoint can neither set one nor
// erase one. Changing the symbol does discard it, because a price from the
// previous listing values the holding on the wrong market, and sometimes in
// the wrong currency.
v.Quote, v.QuotedAt = "", ""
if v.ID == "" {
if !domain.ValidISIN(v.ISIN) {
return errors.New("an instrument needs a valid ISIN")
@@ -88,6 +94,9 @@ func SaveInstrument(d *domain.Dataset, v domain.Instrument) error {
if x.ISIN != v.ISIN {
return errors.New("an instrument's ISIN is its identity; register the other security separately")
}
if x.Symbol == v.Symbol {
v.Quote, v.QuotedAt = x.Quote, x.QuotedAt
}
d.Instruments[i] = v
return nil
}
+151
View File
@@ -0,0 +1,151 @@
package app
import (
"context"
"fmt"
"strings"
"time"
"finance-duck/internal/domain"
"finance-duck/internal/quotes"
)
// QuoteFailure names one instrument the price job could not value, with the
// provider's already sanitized reason. It carries the ISIN as well as the ID
// because the person reading a failed refresh recognises the security by its
// ISIN, not by a registry identifier.
type QuoteFailure struct {
InstrumentID string `json:"instrument_id"`
ISIN string `json:"isin"`
Symbol string `json:"symbol"`
Error string `json:"error"`
}
// QuoteResult is the outcome of one refresh. Every instrument is accounted for
// exactly once, so Updated, Unchanged, Skipped and the failures add up to the
// number of instruments in the journal and a partial run is visibly partial.
type QuoteResult struct {
Updated int `json:"updated"`
Unchanged int `json:"unchanged"`
Skipped int `json:"skipped"`
Failures []QuoteFailure `json:"failures"`
State State `json:"state"`
}
// quoteInterval is how often prices refresh on their own. The provider
// publishes one close per day, so asking more often only spends requests.
const quoteInterval = 24 * time.Hour
// quotePace spaces provider calls. The chart endpoint is public and
// unauthenticated, and a household portfolio of a few dozen symbols still
// finishes in seconds at this rate while staying far below the burst at which
// the provider starts refusing.
const quotePace = 250 * time.Millisecond
// quoteStartup delays the first automatic refresh past start, so a restart
// never fetches while the journal is still being read and a rebuild is running.
const quoteStartup = 30 * time.Second
// RefreshQuotes fetches the latest close for every instrument that names a
// market symbol and writes the accepted ones to the journal in a single
// commit. One instrument's failure is recorded and the run continues: a
// delisted or mistyped symbol must not stop the rest of the portfolio from
// being valued.
func (a *App) RefreshQuotes(ctx context.Context) (QuoteResult, error) {
s, err := a.Snapshot(ctx)
if err != nil {
return QuoteResult{}, err
}
result := QuoteResult{Failures: []QuoteFailure{}}
accepted := make(map[string]quotes.Quote)
fetched := 0
for _, instrument := range s.Data.Instruments {
if instrument.Symbol == "" {
result.Skipped++
continue
}
if err = paceQuote(ctx, fetched); err != nil {
return QuoteResult{}, err
}
fetched++
fail := func(reason string) {
result.Failures = append(result.Failures, QuoteFailure{InstrumentID: instrument.ID, ISIN: instrument.ISIN, Symbol: instrument.Symbol, Error: reason})
}
quote, e := a.quotes.Latest(ctx, instrument.Symbol)
if e != nil {
// A shutdown cancels the fetch too, and recording that as this
// instrument's fault would fill the report with failures that say
// nothing about the symbols.
if ctx.Err() != nil {
return QuoteResult{}, ctx.Err()
}
fail(e.Error())
continue
}
// One ISIN is listed on several exchanges in different currencies, and
// a symbol can be resolved to the wrong listing. Storing a price in a
// currency the holding is not denominated in would misstate wealth
// silently, so a disagreement is a failure and never a write.
if !strings.EqualFold(quote.Currency, instrument.Currency) {
fail(fmt.Sprintf("quoted in %s but the instrument is held in %s", quote.Currency, instrument.Currency))
continue
}
units, e := quote.Price.Units()
if e != nil {
fail(e.Error())
continue
}
if units <= 0 {
fail("quoted price is not positive")
continue
}
// An empty stored quote fails to parse, which is exactly the "not the
// same value" answer wanted here.
if current, e := instrument.Quote.Units(); e == nil && current == units && instrument.QuotedAt == quote.Day {
result.Unchanged++
continue
}
accepted[instrument.ID] = quote
}
if len(accepted) == 0 {
result.State = s
return result, nil
}
// The fetches took time, so the journal may have moved on underneath this
// run; re-read it and match by instrument ID rather than by position.
if s, err = a.Snapshot(ctx); err != nil {
return QuoteResult{}, err
}
s, err = a.Mutate(ctx, s.Revision, func(d *domain.Dataset) error {
for i := range d.Instruments {
quote, ok := accepted[d.Instruments[i].ID]
if !ok {
continue
}
d.Instruments[i].Quote = quote.Price
d.Instruments[i].QuotedAt = quote.Day
result.Updated++
}
return nil
})
if err != nil {
return QuoteResult{}, err
}
result.State = s
return result, nil
}
// paceQuote waits out the spacing between provider calls and is where a run
// notices that it has been canceled: nothing has been written yet at this
// point, so abandoning the run here costs only the fetches already made.
func paceQuote(ctx context.Context, fetched int) error {
if fetched == 0 {
return ctx.Err()
}
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(quotePace):
return nil
}
}
+152
View File
@@ -0,0 +1,152 @@
package app
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"path"
"strings"
"testing"
"finance-duck/internal/domain"
"finance-duck/internal/quotes"
)
// chartResponse is the provider's payload for one symbol. The trailing null
// close is what the endpoint really returns for a day that has not settled
// yet, so the price below belongs to the first timestamp, 2025-09-09.
func chartResponse(currency string, price float64) string {
return fmt.Sprintf(`{"chart":{"result":[{"meta":{"currency":%q},"timestamp":[1757376000,1757462400],"indicators":{"quote":[{"close":[%g,null]}]}}],"error":null}}`, currency, price)
}
func quoteStub(t *testing.T) *httptest.Server {
t.Helper()
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch path.Base(r.URL.Path) {
case "VWCE.DE":
fmt.Fprint(w, chartResponse("EUR", 128.42))
case "VUSA.AS":
// The same fund also lists in dollars; resolving a symbol to that
// listing must not value a euro holding.
fmt.Fprint(w, chartResponse("USD", 95.5))
case "BROKEN.DE":
w.WriteHeader(http.StatusInternalServerError)
case "SAP.DE":
fmt.Fprint(w, chartResponse("EUR", 210.5))
default:
t.Errorf("unexpected request for %q", r.URL.Path)
w.WriteHeader(http.StatusNotFound)
}
}))
}
func seedInstruments(t *testing.T, a *App, s State) State {
t.Helper()
s, err := a.Mutate(context.Background(), s.Revision, func(d *domain.Dataset) error {
for _, v := range []struct{ isin, name, symbol string }{
{"IE00BK5BQT80", "FTSE All-World", "VWCE.DE"},
{"IE00B3XXRP09", "S&P 500", "VUSA.AS"},
{"US0378331005", "Apple", ""},
{"LU0908500753", "Stoxx 600", "BROKEN.DE"},
{"DE0007164600", "SAP", "SAP.DE"},
} {
instrument := domain.Instrument{ID: domain.InstrumentID(v.isin), ISIN: v.isin, Name: v.name, Currency: "EUR", Symbol: v.symbol}
if v.symbol == "BROKEN.DE" {
instrument.Quote, instrument.QuotedAt = "42.5", "2025-09-01"
}
d.Instruments = append(d.Instruments, instrument)
}
return nil
})
if err != nil {
t.Fatal(err)
}
return s
}
// A refresh values what it can and reports the rest: a wrong-currency listing
// is the dangerous case, because writing it would misstate wealth without any
// visible error.
func TestRefreshQuotesWritesOnlyMatchingCurrenciesAndOutlivesOneFailure(t *testing.T) {
a, s := testApp(t)
stub := quoteStub(t)
defer stub.Close()
a.quotes = quotes.Client{BaseURL: stub.URL}
s = seedInstruments(t, a, s)
result, err := a.RefreshQuotes(context.Background())
if err != nil {
t.Fatal(err)
}
if result.Updated != 2 || result.Unchanged != 0 || result.Skipped != 1 || len(result.Failures) != 2 {
t.Fatalf("unexpected tally: updated %d unchanged %d skipped %d failures %+v", result.Updated, result.Unchanged, result.Skipped, result.Failures)
}
fresh, err := a.Snapshot(context.Background())
if err != nil {
t.Fatal(err)
}
held := map[string]domain.Instrument{}
for _, v := range fresh.Data.Instruments {
held[v.ISIN] = v
}
if got := held["IE00BK5BQT80"]; got.Quote != "128.42" || got.QuotedAt != "2025-09-09" {
t.Fatalf("accepted quote not journaled: %+v", got)
}
if got := held["IE00B3XXRP09"]; got.Quote != "" || got.QuotedAt != "" {
t.Fatalf("a dollar quote was written onto a euro holding: %+v", got)
}
if got := held["LU0908500753"]; got.Quote != "42.5" || got.QuotedAt != "2025-09-01" {
t.Fatalf("a failed fetch overwrote a good quote: %+v", got)
}
if got := held["DE0007164600"]; got.Quote != "210.5" || got.QuotedAt != "2025-09-09" {
t.Fatalf("an earlier failure stopped a later instrument: %+v", got)
}
failures := map[string]QuoteFailure{}
for _, f := range result.Failures {
failures[f.ISIN] = f
}
mismatch, ok := failures["IE00B3XXRP09"]
if !ok || mismatch.Symbol != "VUSA.AS" || !strings.Contains(mismatch.Error, "USD") || !strings.Contains(mismatch.Error, "EUR") {
t.Fatalf("currency mismatch not reported usefully: %+v", result.Failures)
}
if _, ok = failures["LU0908500753"]; !ok {
t.Fatalf("a provider failure went unreported: %+v", result.Failures)
}
if _, ok = failures["US0378331005"]; ok {
t.Fatalf("an instrument without a symbol must be skipped, not failed: %+v", result.Failures)
}
// A second run finds the same closes and must leave the journal alone: a
// commit per refresh would grow the journal by a revision a day for nothing.
again, err := a.RefreshQuotes(context.Background())
if err != nil {
t.Fatal(err)
}
if again.Updated != 0 || again.Unchanged != 2 {
t.Fatalf("repeated refresh rewrote unchanged quotes: updated %d unchanged %d", again.Updated, again.Unchanged)
}
if again.State.Revision != fresh.Revision {
t.Fatalf("repeated refresh committed a new revision %q after %q", again.State.Revision, fresh.Revision)
}
}
// Cancellation must be observed between instruments so a shutdown mid-refresh
// leaves the journal exactly as it was.
func TestRefreshQuotesStopsOnCanceledContextWithoutWriting(t *testing.T) {
a, s := testApp(t)
stub := quoteStub(t)
defer stub.Close()
a.quotes = quotes.Client{BaseURL: stub.URL}
s = seedInstruments(t, a, s)
ctx, cancel := context.WithCancel(context.Background())
cancel()
if _, err := a.RefreshQuotes(ctx); err == nil {
t.Fatal("a canceled refresh must report the cancellation")
}
fresh, err := a.Snapshot(context.Background())
if err != nil {
t.Fatal(err)
}
if fresh.Revision != s.Revision {
t.Fatalf("a canceled refresh committed %q over %q", fresh.Revision, s.Revision)
}
}
+153 -13
View File
@@ -15,13 +15,21 @@ import (
// same journal derives.
type Wealth struct {
Accounts []WealthAccount `json:"accounts"`
// Totals is cash summed per currency across every account.
// Totals is cash, position value and their sum per currency, across every
// account.
Totals []WealthTotal `json:"totals"`
}
type WealthTotal struct {
Currency string `json:"currency"`
Cash domain.Money `json:"cash"`
// Positions is the market value of every priced holding, and Wealth the
// two together. Holdings with no quote are excluded from both and counted
// in Unpriced, because valuing them at cost would report a number the
// journal cannot support.
Positions domain.Money `json:"positions"`
Wealth domain.Money `json:"wealth"`
Unpriced int `json:"unpriced"`
}
// WealthAccount is one account's position as the journal records it.
@@ -38,11 +46,47 @@ type WealthAccount struct {
// Cash is every recorded movement summed. It equals the account's real
// balance only when the journal holds that account's complete history,
// which a broker export does and a date-windowed bank statement does not.
Cash domain.Money `json:"cash"`
Cash domain.Money `json:"cash"`
// Positions is the market value of every priced holding, and Wealth the two
// together: the number this page exists to show. Unpriced counts the
// holdings left out because no quote is known for them.
Positions domain.Money `json:"positions"`
Wealth domain.Money `json:"wealth"`
Unpriced int `json:"unpriced"`
// Flows is that balance grouped by what moved it, so a total that
// disagrees with a broker's own figure localises to one class of row
// instead of to the whole history.
Flows []WealthFlow `json:"flows"`
Holdings []WealthHolding `json:"holdings"`
Checks []WealthCheck `json:"checks"`
}
// WealthFlow is the cash one kind of record moved, and how many of them there
// were. The sum of every flow is the account's balance.
type WealthFlow struct {
Event string `json:"event"`
Label string `json:"label"`
Cash domain.Money `json:"cash"`
Records int `json:"records"`
}
// flowLabels names each kind of movement in the order a statement reads, so
// the breakdown is comparable line by line against a broker's own screen.
var flowLabels = []struct{ event, label string }{
{domain.EventDeposit, "Deposits"},
{domain.EventWithdrawal, "Withdrawals"},
{domain.EventFee, "Broker fees"},
{domain.EventInterest, "Interest"},
{domain.EventTaxSettlement, "Tax settlements"},
{domain.EventDistribution, "Distributions"},
{domain.EventBuy, "Purchases"},
{domain.EventSell, "Sales"},
{domain.EventReinvest, "Reinvestments"},
{domain.EventCorporateAction, "Corporate actions"},
{domain.EventPositionTransfer, "Depot transfers"},
{"bank", "Rows from other sources"},
}
// WealthHolding is one instrument's position in one account.
type WealthHolding struct {
InstrumentID string `json:"instrument_id"`
@@ -56,7 +100,17 @@ type WealthHolding struct {
// Received is cash this instrument paid out without moving the position:
// distributions, and the cash side of a corporate action.
Received domain.Money `json:"received"`
Records int `json:"records"`
// Quote is the last known unit price and QuotedAt the day it is from.
// Value is the holding at that price. Priced is false when no quote is
// known, and then Value is absent rather than guessed from cost.
Quote domain.Quantity `json:"quote,omitempty"`
QuotedAt string `json:"quoted_at,omitempty"`
Value domain.Money `json:"value,omitempty"`
Priced bool `json:"priced"`
// Result is the value now plus every euro this position returned, less
// every euro put into it: the total outcome to date, realised and not.
Result domain.Money `json:"result,omitempty"`
Records int `json:"records"`
}
// WealthCheck is one named verification with its evidence. Failed marks a
@@ -98,6 +152,10 @@ func WealthOf(data domain.Dataset) Wealth {
return strings.Compare(x.Facts.ID, y.Facts.ID)
})
type flowState struct {
cash int64
records int
}
type holdingState struct {
units, invested, received int64
records int
@@ -107,10 +165,12 @@ func WealthOf(data domain.Dataset) Wealth {
type accountState struct {
cash, lowestCash int64
lowestCashDate string
day string
records int
first, last string
holdings map[string]*holdingState
order []string
flows map[string]*flowState
broken []string
unappliedFee, unappliedTax int64
unappliedRows int
@@ -119,14 +179,34 @@ func WealthOf(data domain.Dataset) Wealth {
states := map[string]*accountState{}
state := func(id string) *accountState {
if states[id] == nil {
states[id] = &accountState{holdings: map[string]*holdingState{}}
states[id] = &accountState{holdings: map[string]*holdingState{}, flows: map[string]*flowState{}}
}
return states[id]
}
// A day's rows are applied together before any low-water mark is taken.
// Order within a day is not knowable: a broker export states a booking date
// and a clock time, the time is local and crosses midnight, so only the
// date is imported. A purchase funded by a sale nine seconds earlier then
// arrives in an arbitrary order, and checking row by row reports a dip
// that never happened.
closeDay := func(st *accountState) {
if st.cash < st.lowestCash {
st.lowestCash, st.lowestCashDate = st.cash, st.day
}
for _, held := range st.holdings {
if held.units < held.lowest {
held.lowest, held.lowestDate = held.units, st.day
}
}
}
for _, t := range ordered {
f := t.Facts
account := accounts[f.AccountID]
st := state(f.AccountID)
if st.day != "" && st.day != f.BookingDate {
closeDay(st)
}
st.day = f.BookingDate
st.records++
if st.first == "" {
st.first = f.BookingDate
@@ -138,10 +218,16 @@ func WealthOf(data domain.Dataset) Wealth {
continue
}
st.cash += minor
if st.cash < st.lowestCash {
st.lowestCash, st.lowestCashDate = st.cash, f.BookingDate
}
inv := f.Investment
flow := "bank"
if inv != nil {
flow = inv.Event
}
if st.flows[flow] == nil {
st.flows[flow] = &flowState{}
}
st.flows[flow].cash += minor
st.flows[flow].records++
if inv == nil {
continue
}
@@ -189,13 +275,17 @@ func WealthOf(data domain.Dataset) Wealth {
continue
}
held.units += units
if held.units < held.lowest {
held.lowest, held.lowestDate = held.units, f.BookingDate
}
for _, st := range states {
if st.day != "" {
closeDay(st)
}
}
report := Wealth{Accounts: []WealthAccount{}, Totals: []WealthTotal{}}
totals := map[string]int64{}
positionTotals := map[string]int64{}
unpricedTotals := map[string]int{}
currencies := []string{}
for _, account := range data.Accounts {
st := state(account.ID)
@@ -207,22 +297,62 @@ func WealthOf(data domain.Dataset) Wealth {
AccountID: account.ID, DisplayName: account.DisplayName, Institution: account.Institution,
Currency: account.Currency, Kind: kind, Active: account.Active,
Records: st.records, FirstBooking: st.first, LastBooking: st.last,
Cash: domain.FormatMoney(st.cash), Holdings: []WealthHolding{}, Checks: []WealthCheck{},
Cash: domain.FormatMoney(st.cash), Flows: []WealthFlow{},
Holdings: []WealthHolding{}, Checks: []WealthCheck{},
}
for _, flow := range flowLabels {
if moved := st.flows[flow.event]; moved != nil {
entry.Flows = append(entry.Flows, WealthFlow{
Event: flow.event, Label: flow.label,
Cash: domain.FormatMoney(moved.cash), Records: moved.records,
})
}
}
if _, seen := totals[account.Currency]; !seen {
currencies = append(currencies, account.Currency)
}
totals[account.Currency] += st.cash
positions, unpriced, stale := int64(0), 0, []string{}
for _, id := range st.order {
held := st.holdings[id]
instrument := instruments[id]
entry.Holdings = append(entry.Holdings, WealthHolding{
holding := WealthHolding{
InstrumentID: id, ISIN: instrument.ISIN, Name: instrument.Name,
Quantity: domain.FormatQuantity(held.units), Invested: domain.FormatMoney(held.invested),
Received: domain.FormatMoney(held.received), Records: held.records,
})
}
// A closed position needs no quote: nothing multiplied by any price
// is nothing, and its result is already settled in cash.
quote, err := instrument.Quote.Units()
switch {
case held.units == 0:
holding.Priced, holding.Value = true, domain.FormatMoney(0)
case instrument.Quote == "" || err != nil:
unpriced++
stale = append(stale, instrument.ISIN)
default:
value, ok := domain.RoundedProduct(held.units, quote)
if !ok {
unpriced++
stale = append(stale, instrument.ISIN)
break
}
holding.Priced = true
holding.Quote, holding.QuotedAt = instrument.Quote, instrument.QuotedAt
holding.Value = domain.FormatMoney(value)
positions += value
}
if holding.Priced {
settled, _ := holding.Value.Minor()
holding.Result = domain.FormatMoney(settled - held.invested + held.received)
}
entry.Holdings = append(entry.Holdings, holding)
}
slices.SortFunc(entry.Holdings, func(x, y WealthHolding) int { return strings.Compare(x.Name, y.Name) })
entry.Positions, entry.Unpriced = domain.FormatMoney(positions), unpriced
entry.Wealth = domain.FormatMoney(st.cash + positions)
positionTotals[account.Currency] += positions
unpricedTotals[account.Currency] += unpriced
check := func(name, detail string, failed bool) {
entry.Checks = append(entry.Checks, WealthCheck{Name: name, Detail: detail, Failed: failed})
@@ -254,10 +384,20 @@ func WealthOf(data domain.Dataset) Wealth {
if st.unmatchedRows > 0 {
check("Deposits and withdrawals unmatched", fmt.Sprintf("%d transfer(s) totalling %s have no counterpart in another account. They stay out of spending either way; set this account's IBAN and settlement IBAN to pair them", st.unmatchedRows, domain.FormatMoney(st.unmatchedCash)), false)
}
if unpriced > 0 {
check("Holdings priced", fmt.Sprintf("%d holding(s) have no quote and are left out of the wealth above: %s. Set each one's market symbol in Instruments so the daily price job can quote it; valuing them at cost would report a number the journal cannot support", unpriced, strings.Join(stale, ", ")), false)
} else if len(st.order) > 0 {
check("Holdings priced", "every open position has a quote, so the wealth above is complete", false)
}
report.Accounts = append(report.Accounts, entry)
}
for _, currency := range currencies {
report.Totals = append(report.Totals, WealthTotal{Currency: currency, Cash: domain.FormatMoney(totals[currency])})
report.Totals = append(report.Totals, WealthTotal{
Currency: currency, Cash: domain.FormatMoney(totals[currency]),
Positions: domain.FormatMoney(positionTotals[currency]),
Wealth: domain.FormatMoney(totals[currency] + positionTotals[currency]),
Unpriced: unpricedTotals[currency],
})
}
return report
}
+150
View File
@@ -231,3 +231,153 @@ func TestManualTransferLinkRewritesBothPairsAtOnce(t *testing.T) {
}
}
}
// Order within a day is not knowable. A broker states a booking date and a
// local clock time, and only the date is imported, because the time crosses
// midnight for part of the year and would move rows to the wrong day. A
// purchase funded by a sale nine seconds earlier then arrives in an arbitrary
// order, so a balance that never went negative gets reported as if it had.
// The balance is therefore only judged where it is observable: at each day's
// close.
func TestSameDayTradesDoNotReportAnIntradayDip(t *testing.T) {
build := func(funded bool) domain.Dataset {
data := domain.NewDataset()
data.Accounts = []domain.Account{{ID: "broker", DisplayName: "Scalable", Currency: "EUR", Kind: domain.AccountInvestment, Active: true}}
data.Instruments = []domain.Instrument{{ID: "ins_world", ISIN: "IE000BI8OT95", Name: "Amundi Core MSCI World (Acc)", Currency: "EUR"}}
row := func(id, date, amount string, inv domain.Investment) domain.Transaction {
f := domain.Facts{
ID: id, Source: "scalable_csv", AccountID: "broker", BookingDate: date,
Amount: domain.Money(amount), Currency: "EUR", RawDescription: "Amundi Core MSCI World (Acc)",
Fingerprint: id, Investment: &inv,
}
return domain.Transaction{Facts: f, Enrichment: domain.Fallback(f)}
}
if funded {
data.Transactions = append(data.Transactions, row("tx_0", "2025-12-18", "1000.00", domain.Investment{Event: domain.EventDeposit}))
}
// tx_a sorts before tx_b, so the purchase is applied first even though
// the sale that funded it happened nine seconds earlier.
data.Transactions = append(data.Transactions,
row("tx_a", "2025-12-19", "-30911.145", domain.Investment{Event: domain.EventBuy, InstrumentID: "ins_world", Quantity: "223", Price: "138.615", Gross: "-30911.145"}),
row("tx_b", "2025-12-19", "30619.545", domain.Investment{Event: domain.EventSell, InstrumentID: "ins_world", Quantity: "-223", Price: "138.565", Gross: "30899.995", Tax: "280.45"}),
)
if err := domain.Validate(data); err != nil {
t.Fatal(err)
}
return data
}
funded := WealthOf(build(true)).Accounts[0]
for _, check := range funded.Checks {
if check.Failed {
t.Errorf("a day that closed at %s reported %q: %s", funded.Cash, check.Name, check.Detail)
}
}
if funded.Cash != "708.40" {
t.Errorf("balance %s, want 708.40", funded.Cash)
}
// The breakdown accounts for the balance exactly, so a total that
// disagrees with a broker's screen points at one class of row.
total := int64(0)
for _, flow := range funded.Flows {
minor, err := flow.Cash.Minor()
if err != nil {
t.Fatal(err)
}
total += minor
}
if domain.FormatMoney(total) != funded.Cash {
t.Errorf("flows sum to %s, balance is %s", domain.FormatMoney(total), funded.Cash)
}
if len(funded.Flows) != 3 {
t.Errorf("expected a line per kind of movement, got %+v", funded.Flows)
}
// A day that really does close negative is still reported.
unfunded := WealthOf(build(false)).Accounts[0]
found := false
for _, check := range unfunded.Checks {
if check.Failed && check.Name == "Cash never negative" {
found = true
if !strings.Contains(check.Detail, "2025-12-19") {
t.Errorf("negative close not located: %s", check.Detail)
}
}
}
if !found {
t.Errorf("a day closing at %s passed: %+v", unfunded.Cash, unfunded.Checks)
}
}
// A page that reports only cash is not reporting wealth. An open position is
// valued at its own quote; a closed one needs none; an open one without a quote
// is named and left out, because valuing it at cost would report a number the
// journal cannot support.
func TestWealthValuesHoldingsAtTheirQuote(t *testing.T) {
data := domain.NewDataset()
data.Accounts = []domain.Account{{ID: "broker", DisplayName: "Scalable", Currency: "EUR", Kind: domain.AccountInvestment, Active: true}}
data.Instruments = []domain.Instrument{
{ID: "ins_a", ISIN: "IE00B4L5Y983", Name: "Core World", Currency: "EUR", Symbol: "EUNL.DE", Quote: "110.00", QuotedAt: "2026-09-11"},
{ID: "ins_b", ISIN: "IE00B1XNHC34", Name: "Clean Energy", Currency: "EUR"},
{ID: "ins_c", ISIN: "US67066G1040", Name: "NVIDIA", Currency: "EUR", Symbol: "NVD.DE", Quote: "150.00", QuotedAt: "2026-09-11"},
}
row := func(id, date, amount string, inv domain.Investment) domain.Transaction {
f := domain.Facts{
ID: id, Source: "scalable_csv", AccountID: "broker", BookingDate: date,
Amount: domain.Money(amount), Currency: "EUR", RawDescription: "row", Fingerprint: id, Investment: &inv,
}
return domain.Transaction{Facts: f, Enrichment: domain.Fallback(f)}
}
data.Transactions = []domain.Transaction{
row("tx_1", "2026-01-02", "50000.00", domain.Investment{Event: domain.EventDeposit}),
row("tx_2", "2026-01-03", "-10000.00", domain.Investment{Event: domain.EventBuy, InstrumentID: "ins_a", Quantity: "100", Price: "100.00", Gross: "-10000.00"}),
row("tx_3", "2026-01-04", "-500.00", domain.Investment{Event: domain.EventBuy, InstrumentID: "ins_b", Quantity: "10", Price: "50.00", Gross: "-500.00"}),
row("tx_4", "2026-01-05", "-100.00", domain.Investment{Event: domain.EventBuy, InstrumentID: "ins_c", Quantity: "5", Price: "20.00", Gross: "-100.00"}),
row("tx_5", "2026-01-06", "125.00", domain.Investment{Event: domain.EventSell, InstrumentID: "ins_c", Quantity: "-5", Price: "25.00", Gross: "125.00"}),
}
if err := domain.Validate(data); err != nil {
t.Fatal(err)
}
report := WealthOf(data)
account := report.Accounts[0]
if account.Cash != "39525.00" || account.Positions != "11000.00" || account.Wealth != "50525.00" {
t.Fatalf("cash %s, positions %s, wealth %s; want 39525.00, 11000.00, 50525.00", account.Cash, account.Positions, account.Wealth)
}
if account.Unpriced != 1 {
t.Errorf("unpriced holdings %d, want 1", account.Unpriced)
}
byISIN := map[string]WealthHolding{}
for _, h := range account.Holdings {
byISIN[h.ISIN] = h
}
// An open position carries its quote and the day it is from.
if open := byISIN["IE00B4L5Y983"]; !open.Priced || open.Value != "11000.00" || open.Result != "1000.00" || open.QuotedAt != "2026-09-11" {
t.Errorf("open position valued as %+v", open)
}
// A position with no quote contributes nothing and says so.
if none := byISIN["IE00B1XNHC34"]; none.Priced || none.Value != "" || none.Result != "" {
t.Errorf("unquoted position was valued anyway: %+v", none)
}
// A closed position is worth nothing at any price, and its result is the
// cash it settled.
if closed := byISIN["US67066G1040"]; !closed.Priced || closed.Value != "0.00" || closed.Result != "25.00" {
t.Errorf("closed position valued as %+v", closed)
}
if total := report.Totals[0]; total.Wealth != "50525.00" || total.Positions != "11000.00" || total.Unpriced != 1 {
t.Errorf("totals %+v", total)
}
// The gap is named rather than hidden in the number.
named := false
for _, check := range account.Checks {
if check.Name == "Holdings priced" {
named = true
if check.Failed || !strings.Contains(check.Detail, "IE00B1XNHC34") {
t.Errorf("unpriced holding not named: %+v", check)
}
}
}
if !named {
t.Error("no note about the holdings left out of the wealth figure")
}
}