Skip to content

Commit

Permalink
Be more defensive against zero prices
Browse files Browse the repository at this point in the history
  • Loading branch information
Silvio Böhler committed Oct 7, 2024
1 parent 582e928 commit 26c30f0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
3 changes: 1 addition & 2 deletions lib/journal/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ func ComputePrices(v *model.Commodity) *Processor {
prc := make(price.Prices)
return &Processor{
Price: func(p *model.Price) error {
prc.Insert(p.Commodity, p.Price, p.Target)
return nil
return prc.Insert(p.Commodity, p.Price, p.Target)
},
DayEnd: func(d *Day) error {
if len(d.Prices) > 0 {
Expand Down
6 changes: 5 additions & 1 deletion lib/model/price/prices.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ type Prices map[*commodity.Commodity]NormalizedPrices
var one = decimal.NewFromInt(1)

// Insert inserts a new price.
func (ps Prices) Insert(commodity *commodity.Commodity, price decimal.Decimal, target *commodity.Commodity) {
func (ps Prices) Insert(commodity *commodity.Commodity, price decimal.Decimal, target *commodity.Commodity) error {
if price.IsZero() {
return fmt.Errorf("invalid price %s for commodity %s in %s", price.String(), commodity.Name(), target.Name())
}
ps.addPrice(target, commodity, price)
ps.addPrice(commodity, target, one.Div(price).Truncate(8))
return nil
}

func (ps Prices) addPrice(target, commodity *commodity.Commodity, price decimal.Decimal) {
Expand Down
4 changes: 3 additions & 1 deletion lib/quotes/yahoo2/yahoo2.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ func decodeResponse(r io.Reader) ([]Quote, error) {
AdjClose: result.Indicators.Adjclose[0].Adjclose[i],
Volume: result.Indicators.Quote[0].Volume[i],
}
res = append(res, q)
if q.Close > 0 {
res = append(res, q)
}
}
return res, nil
}
Expand Down

0 comments on commit 26c30f0

Please sign in to comment.