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
+19 -2
View File
@@ -375,8 +375,25 @@ func Validate(d Dataset) error {
if other, ok := isins[v.ISIN]; ok {
return fmt.Errorf("instrument %q: ISIN %s already held by %q", v.ID, v.ISIN, other)
}
if !nonblank(v.Name) || !currencyPattern.MatchString(v.Currency) {
return fmt.Errorf("instrument %q: valid UTF-8 name and three-letter uppercase currency required", v.ID)
if !nonblank(v.Name) || !currencyPattern.MatchString(v.Currency) || !validText(v.Symbol) {
return fmt.Errorf("instrument %q: valid UTF-8 name and symbol and three-letter uppercase currency required", v.ID)
}
// A quote without its day cannot be judged stale, and a day without a
// quote values nothing, so neither stands alone.
if (v.Quote == "") != (v.QuotedAt == "") {
return fmt.Errorf("instrument %q: a quote and the day it is from are recorded together", v.ID)
}
if v.Quote != "" {
units, err := v.Quote.Units()
if err != nil {
return fmt.Errorf("instrument %q: %w", v.ID, err)
}
if units < 0 {
return fmt.Errorf("instrument %q: a quote cannot be negative", v.ID)
}
if !validDate(v.QuotedAt) {
return fmt.Errorf("instrument %q: invalid quote date %q", v.ID, v.QuotedAt)
}
}
isins[v.ISIN] = v.ID
instruments[v.ID] = v
+10
View File
@@ -104,6 +104,16 @@ type Instrument struct {
ISIN string `json:"isin"`
Name string `json:"name"`
Currency string `json:"currency"`
// Symbol is the market listing this security is quoted under. One ISIN maps
// to several listings in different currencies, and taking the wrong one
// silently misstates wealth, so it is chosen once by hand and never
// guessed. Without it the holding stays unpriced.
Symbol string `json:"symbol,omitempty"`
// Quote is the last known unit price and QuotedAt the day it is from, both
// filled by the daily price job and hand-editable. A quote is a rate, not
// money: a crypto unit price needs more than money's four places.
Quote Quantity `json:"quote,omitempty"`
QuotedAt string `json:"quoted_at,omitempty"`
}
type Facts struct {