Import Trade Republic exports, whose conventions invert Scalable's
A second broker export is recognized locally, by its full column set, and read through the same pipeline: detection and parsing now dispatch on the format, so the upload path, the review dialog, deduplication, the journal and the Wealth report are unchanged. Its nine row types cover cash transfers, interest, dividends, tax settlements and trades in funds, shares and crypto; none of them moves a position without moving cash, so the cash-neutral class that Scalable's corporate actions belong to does not arise here. Three of its conventions are the opposite of the export already supported, and reading any of them the other way round moves money. Fee and tax are the signed adjustments it made to the cash rather than deductions from a gross, so a one euro order fee arrives as -1.00 and is negated at import; the journal keeps one convention and the domain never learns that two exist. A cash row's amount is the gross, not the net, so interest of 16.46 with -4.33 of tax credits 12.13 - where the other export states its cash already net and its tax is recorded and never applied. Whether a cash row carries a gross now decides which of those it was, which also makes the first kind's settlement checkable and stops the Wealth report from claiming a figure was left unapplied when it was not. And a TAX_OPTIMIZATION row puts zero in the amount column and its money in the tax column, signed both ways: read as cash, all six in a real export move nothing. Two more rows lie about their own columns. A dividend fills the share column with the holding the dividend was paid on, not with a position change, so adding it would double the holding. Crypto carries a bare ticker in the symbol column and its ISIN-shaped identifier only in the description, so the identifier is taken from the symbol when that is an ISIN and otherwise from the one the description names; a position row resolving to neither is refused rather than attached to a guess. The shares-times-price check now holds a gross to the precision the export stated it at rather than to four places. This export prints the notional rounded to cents, and 29 of 59 real trades do not land on a whole cent: demanding exactness rejected half a portfolio. One unit of the stated precision is still four orders of magnitude tighter than the misplaced separator the check exists to catch, and where an export prints the full product the check stays exact. A unit price moves from money to the eight-place quantity type, because a crypto price is quoted to six and rounding it would break the check the amount is verified against. Trailing zeros are dropped before any precision test: this export pads a six-place price to ten, and the padding would otherwise exhaust the precision the value needs. A transfer's counterparty comes from the export's own IBAN column when it has one, from the IBAN the description names in parentheses when it does not, and from the account's configured settlement IBAN when neither names anything. Free text contributes only a value shaped like an IBAN. Without this, 108 transfers stay unpaired and their bank-side counterparts read as spending and income. Verified end to end against a real export: 26 rows import to a cash balance of 32187.02 matching the figure computed by hand from the source rows, all four positions close at exactly zero, and every trade satisfies its own arithmetic.
This commit is contained in:
+62
-17
@@ -246,6 +246,9 @@ func validText(values ...string) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
func validHint(s string) bool {
|
||||
return utf8.ValidString(s) && utf8.RuneCountInString(s) <= 200
|
||||
}
|
||||
|
||||
// ValidISIN reports a syntactically valid ISIN: two country letters, nine
|
||||
// alphanumerics and a check digit.
|
||||
@@ -289,8 +292,8 @@ func Validate(d Dataset) error {
|
||||
if err := register(c.ID, "category"); err != nil {
|
||||
return err
|
||||
}
|
||||
if !nonblank(c.Name) || (c.Kind != "expense" && c.Kind != "income") {
|
||||
return fmt.Errorf("category %q: invalid name or kind", c.ID)
|
||||
if !nonblank(c.Name) || !validHint(c.Hint) || (c.Kind != "expense" && c.Kind != "income") {
|
||||
return fmt.Errorf("category %q: invalid name, hint or kind", c.ID)
|
||||
}
|
||||
categories[c.ID] = c
|
||||
if c.ParentID != "" {
|
||||
@@ -327,8 +330,8 @@ func Validate(d Dataset) error {
|
||||
if err := register(t.ID, "tag"); err != nil {
|
||||
return err
|
||||
}
|
||||
if !nonblank(t.Name) {
|
||||
return fmt.Errorf("tag %q: name required", t.ID)
|
||||
if !nonblank(t.Name) || !validHint(t.Hint) {
|
||||
return fmt.Errorf("tag %q: name or hint invalid", t.ID)
|
||||
}
|
||||
tags[t.ID] = true
|
||||
}
|
||||
@@ -463,9 +466,12 @@ func (index enrichmentIndex) validate(f Facts, e Enrichment) error {
|
||||
if e.MerchantID != "" && !index.merchants[e.MerchantID] {
|
||||
return fmt.Errorf("unknown merchant %q", e.MerchantID)
|
||||
}
|
||||
if !validText(e.Classification.Source, e.Classification.Model, e.Classification.Error) {
|
||||
if !validText(e.Classification.Source, e.Classification.Model, e.Classification.Confidence, e.Classification.Error) {
|
||||
return fmt.Errorf("classification metadata must be valid UTF-8")
|
||||
}
|
||||
if e.Classification.Confidence != "" && e.Classification.Confidence != "high" && e.Classification.Confidence != "medium" && e.Classification.Confidence != "low" {
|
||||
return fmt.Errorf("invalid classification confidence")
|
||||
}
|
||||
if e.Classification.Timestamp != "" {
|
||||
if _, err := time.Parse(time.RFC3339Nano, e.Classification.Timestamp); err != nil {
|
||||
return fmt.Errorf("invalid classification timestamp")
|
||||
@@ -538,18 +544,18 @@ func optionalQuantity(q Quantity) (int64, error) {
|
||||
return q.Units()
|
||||
}
|
||||
|
||||
// RoundedProduct multiplies an exact share count by an exact price and rounds
|
||||
// to money's four places, half away from zero. Quantity is 1e-8 units and
|
||||
// price is 1e-4 units, so the product is 1e-12 and needs 128-bit width.
|
||||
// RoundedProduct multiplies an exact share count by an exact unit price and
|
||||
// rounds to money's four places, half away from zero. Both operands are 1e-8
|
||||
// units, so the product is 1e-16 and needs 128-bit width.
|
||||
func RoundedProduct(quantity, price int64) (int64, bool) {
|
||||
product := new(big.Int).Mul(big.NewInt(quantity), big.NewInt(price))
|
||||
half := big.NewInt(50_000_000)
|
||||
half := big.NewInt(500_000_000_000)
|
||||
if product.Sign() < 0 {
|
||||
product.Sub(product, half)
|
||||
} else {
|
||||
product.Add(product, half)
|
||||
}
|
||||
rounded := product.Quo(product, big.NewInt(100_000_000))
|
||||
rounded := product.Quo(product, big.NewInt(1_000_000_000_000))
|
||||
if !rounded.IsInt64() {
|
||||
return 0, false
|
||||
}
|
||||
@@ -592,7 +598,7 @@ func validateInvestment(f Facts, a Account, instruments map[string]Instrument) e
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
price, err := optionalMoney(inv.Price)
|
||||
price, err := optionalQuantity(inv.Price)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -613,10 +619,18 @@ func validateInvestment(f Facts, a Account, instruments map[string]Instrument) e
|
||||
return err
|
||||
}
|
||||
if inv.CashOnly() {
|
||||
if quantity != 0 || inv.Price != "" || inv.Gross != "" {
|
||||
return fmt.Errorf("%s moves cash only: it carries no quantity, price or gross", inv.Event)
|
||||
if quantity != 0 || inv.Price != "" {
|
||||
return fmt.Errorf("%s moves cash only: it carries no quantity or price", inv.Event)
|
||||
}
|
||||
return nil
|
||||
// The gross is optional here. One broker states a cash row already net
|
||||
// of the tax it withheld, and then only the net is knowable, so the
|
||||
// tax is recorded and never applied. Another states the gross and the
|
||||
// deductions separately, and then the settlement is checkable like any
|
||||
// trade's. Which one is a fact about the source, decided at import.
|
||||
if inv.Gross == "" {
|
||||
return nil
|
||||
}
|
||||
return settles(inv, gross, fee, tax, amount)
|
||||
}
|
||||
if inv.InstrumentID == "" {
|
||||
return fmt.Errorf("%s requires an instrument", inv.Event)
|
||||
@@ -633,8 +647,19 @@ func validateInvestment(f Facts, a Account, instruments map[string]Instrument) e
|
||||
if inv.Settling() {
|
||||
expected = -expected
|
||||
}
|
||||
if gross != expected {
|
||||
return fmt.Errorf("%s gross %s does not equal quantity %s times price %s", inv.Event, Money(formatScaled(gross, moneyScale, 2)), inv.Quantity.String(), inv.Price.String())
|
||||
// The gross is checked to the precision the broker stated it at, and no
|
||||
// further. One broker prints the exact product to nine places, and the
|
||||
// check is then exact. Another prints the notional rounded to cents, where
|
||||
// demanding exactness rejects every trade whose product does not land on a
|
||||
// whole cent - measured on a real export, 29 of 59 of them. One unit of
|
||||
// the stated precision is still four orders of magnitude tighter than the
|
||||
// misplaced decimal separator this check exists to catch.
|
||||
difference := expected - gross
|
||||
if difference < 0 {
|
||||
difference = -difference
|
||||
}
|
||||
if difference >= statedUnit(inv.Gross) {
|
||||
return fmt.Errorf("%s gross %s does not equal quantity %s times price %s, which is %s", inv.Event, inv.Gross.String(), inv.Quantity.String(), inv.Price.String(), Money(formatScaled(expected, moneyScale, 2)))
|
||||
}
|
||||
if inv.PositionOnly() {
|
||||
if amount != 0 {
|
||||
@@ -648,10 +673,30 @@ func validateInvestment(f Facts, a Account, instruments map[string]Instrument) e
|
||||
if (inv.Event == EventSell) != (quantity < 0) {
|
||||
return fmt.Errorf("%s must %s the position", inv.Event, map[bool]string{true: "reduce", false: "increase"}[inv.Event == EventSell])
|
||||
}
|
||||
return settles(inv, gross, fee, tax, amount)
|
||||
}
|
||||
|
||||
// settles enforces that the cash a fact moved is its gross less the fee and
|
||||
// the tax deducted from it. Fee and tax are stored as deductions whichever sign
|
||||
// the source printed, so a refunded tax is a negative deduction and a broker
|
||||
// that writes its fee as a negative adjustment is normalized at import.
|
||||
func settles(inv *Investment, gross, fee, tax, amount int64) error {
|
||||
settled := new(big.Int).Sub(big.NewInt(gross), big.NewInt(fee))
|
||||
settled.Sub(settled, big.NewInt(tax))
|
||||
if !settled.IsInt64() || settled.Int64() != amount {
|
||||
return fmt.Errorf("%s cash %s does not equal gross %s minus fee %s minus tax %s", inv.Event, f.Amount.String(), inv.Gross.String(), inv.Fee.String(), inv.Tax.String())
|
||||
return fmt.Errorf("%s cash %s does not equal gross %s minus fee %s minus tax %s", inv.Event, Money(formatScaled(amount, moneyScale, 2)), inv.Gross.String(), inv.Fee.String(), inv.Tax.String())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// statedUnit is one unit of the last decimal place a money figure was written
|
||||
// with, in exact ten-thousandths. Money always renders at least two places, so
|
||||
// a whole-euro figure counts as stated to the cent.
|
||||
func statedUnit(m Money) int64 {
|
||||
_, fraction, _ := strings.Cut(string(m), ".")
|
||||
unit := int64(1)
|
||||
for range moneyScale - len(strings.TrimRight(fraction, "0")) {
|
||||
unit *= 10
|
||||
}
|
||||
return unit
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user