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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user