wip
parent
e511da2e70
commit
596c20d76c
|
|
@ -2,6 +2,7 @@ package ana
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"maps"
|
"maps"
|
||||||
|
"math"
|
||||||
"regexp"
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -27,7 +28,8 @@ func NewInterestPredictor(namePattern string, currencyPattern string, apy float6
|
||||||
if !currencyMatcher.MatchString(string(currency)) {
|
if !currencyMatcher.MatchString(string(currency)) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
result[name][currency] *= 1.0 + apy/12.0
|
percentOfYearPassed := float64(delta) / float64(time.Hour*365)
|
||||||
|
result[name][currency] *= math.Pow(1.0+apy/12.0, percentOfYearPassed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
package ana
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gogs.inhome.blapointe.com/ana-ledger/ledger"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewInterestPredictor(t *testing.T) {
|
||||||
|
name := "x"
|
||||||
|
curr := ledger.USD
|
||||||
|
cases := map[string]struct {
|
||||||
|
apy float64
|
||||||
|
given ledger.Balances
|
||||||
|
delta time.Duration
|
||||||
|
want ledger.Balances
|
||||||
|
}{
|
||||||
|
"zero": {
|
||||||
|
apy: 0,
|
||||||
|
given: ledger.Balances{name: ledger.Balance{curr: 100}},
|
||||||
|
delta: time.Hour * 24 * 365,
|
||||||
|
want: ledger.Balances{name: ledger.Balance{curr: 100}},
|
||||||
|
},
|
||||||
|
"100%": {
|
||||||
|
apy: 1,
|
||||||
|
given: ledger.Balances{name: ledger.Balance{curr: 100}},
|
||||||
|
delta: time.Hour * 24 * 365,
|
||||||
|
want: ledger.Balances{name: ledger.Balance{curr: 200}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, d := range cases {
|
||||||
|
c := d
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
predictor := NewInterestPredictor(name, string(curr), c.apy)
|
||||||
|
got := predictor(c.given, c.delta)
|
||||||
|
if got.Debug() != c.want.Debug() {
|
||||||
|
t.Errorf("want\n\t%+v, got\n\t%+v", c.want, got)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue