Add test && docs

This commit is contained in:
2020-01-17 13:14:42 +03:00
parent 3acffe085d
commit 31de7837e1
2 changed files with 92 additions and 7 deletions

27
xirr.go
View File

@@ -1,3 +1,7 @@
/*
Goxirr is a simple implementation of a function for calculating
the Internal Rate of Return for irregular cash flow (XIRR).
*/
package goxirr
import (
@@ -5,16 +9,22 @@ import (
"time"
)
//A Transaction represents a single transaction from a series of irregular payments.
type Transaction struct {
Date time.Time
Cash float64
}
func Xirr(transactions []Transaction) float64 {
//Transactions represents a cash flow consisting of individual transactions
type Transactions []Transaction
//Xirr return the Internal Rate of Return (IRR) for an irregular series of cash flows (XIRR)
func Xirr(transactions Transactions) float64 {
var years []float64
for _, ta := range transactions {
years = append(years, (ta.Date.Sub(transactions[0].Date).Hours()/24)/365)
for _, t := range transactions {
years = append(years, (t.Date.Sub(transactions[0].Date).Hours()/24)/365)
}
residual := 1.0
step := 0.05
guess := 0.05
@@ -22,11 +32,13 @@ func Xirr(transactions []Transaction) float64 {
limit := 10000
for math.Abs(residual) > epsilon && limit > 0 {
limit -= 1
limit--
residual = 0.0
for i, trans := range transactions {
residual += trans.Cash / math.Pow(guess, years[i])
for i, t := range transactions {
residual += t.Cash / math.Pow(guess, years[i])
}
if math.Abs(residual) > epsilon {
if residual > 0 {
guess += step
@@ -36,5 +48,6 @@ func Xirr(transactions []Transaction) float64 {
}
}
}
return (guess - 1) * 100
return math.Round(((guess-1)*100)*100) / 100
}