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
+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")
}
}