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