11 Commits

Author SHA1 Message Date
1836b6bcf9 Add linter actions 2020-01-22 15:43:54 +03:00
83edcc7c62 Add linter actions 2020-01-22 15:42:10 +03:00
bd6c0a4d25 Fix typo 2020-01-21 12:50:47 +03:00
f10c652292 Add links to README 2020-01-17 14:29:13 +03:00
8ac353fa81 fix typo 2020-01-17 13:31:16 +03:00
3482a8cb44 Add GitHub Actions 2020-01-17 13:29:26 +03:00
11e1c226ee Add GolangCI-lint config 2020-01-17 13:27:01 +03:00
17598720eb Small update docs 2020-01-17 13:23:33 +03:00
b1c253f250 Small update docs 2020-01-17 13:23:08 +03:00
a46edc6d2b Update README 2020-01-17 13:21:50 +03:00
31de7837e1 Add test && docs 2020-01-17 13:14:42 +03:00
5 changed files with 157 additions and 8 deletions

32
.github/workflows/go.yml vendored Normal file
View File

@@ -0,0 +1,32 @@
name: Go
on: [push]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.13
uses: actions/setup-go@v1
with:
go-version: 1.13
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: Get dependencies
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: GolangCI-Lint
uses: Mushus/golangci-linter@v1.1.2
- name: Test
run: go test -v .
- name: Build
run: go build -v .

3
.golangci.yml Normal file
View File

@@ -0,0 +1,3 @@
linters:
enable:
- wsl

View File

@@ -1 +1,29 @@
# goxirr # goxirr
[![GoDoc](https://godoc.org/github.com/maksim77/goxirr?status.svg)](https://godoc.org/github.com/maksim77/goxirr)
Goxirr is a simple implementation of a function for calculating the Internal Rate of Return for irregular cash flow (XIRR).
## Links
- [Wikipedia](https://en.wikipedia.org/wiki/Internal_rate_of_return)
- [Excel support](https://support.office.com/en-us/article/XIRR-function-DE1242EC-6477-445B-B11B-A303AD9ADC9D)
## Example
```go
import "time"
import "github.com/maksim77/goxirr"
firstDate := time.Date(2019, time.January, 1, 0, 0, 0, 0, time.UTC)
t1 := goxirr.Transaction{
Date: firstDate,
Cash: -100,
}
t2 := goxirr.Transaction{
Date: firstDate.Add(time.Hour * 24 * 365),
Cash: 112,
}
tas := goxirr.Transactions{t1, t2}
fmt.Println(goxirr.Xirr(tas))
```

28
xirr.go
View File

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

72
xirr_test.go Normal file
View File

@@ -0,0 +1,72 @@
package goxirr
import (
"fmt"
"testing"
"time"
)
func ExampleXirr() {
firstDate := time.Date(2019, time.January, 1, 0, 0, 0, 0, time.UTC)
t1 := Transaction{
Date: firstDate,
Cash: -100,
}
t2 := Transaction{
Date: firstDate.Add(time.Hour * 24 * 365),
Cash: 112,
}
tas := Transactions{t1, t2}
fmt.Println(Xirr(tas))
// Output: 12
}
func TestXirr(t *testing.T) {
type args struct {
transactions []Transaction
}
var case1, case2, case3 args
case1.transactions = append(case1.transactions, Transaction{
Date: time.Date(2019, time.January, 1, 0, 0, 0, 0, time.UTC),
Cash: -100,
}, Transaction{
Date: time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC),
Cash: 200,
})
case2.transactions = append(case2.transactions, Transaction{
Date: time.Date(2019, time.January, 1, 0, 0, 0, 0, time.UTC),
Cash: -100,
}, Transaction{
Date: time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC),
Cash: 100,
})
case3.transactions = append(case3.transactions, Transaction{
Date: time.Date(2019, time.January, 1, 0, 0, 0, 0, time.UTC),
Cash: -100,
}, Transaction{
Date: time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC),
Cash: 112,
})
tests := []struct {
name string
args args
want float64
}{
{name: "100%", args: case1, want: 100},
{name: "0%", args: case2, want: 0.0},
{name: "12%", args: case3, want: 12},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Xirr(tt.args.transactions); got != tt.want {
t.Errorf("Xirr() = %v, want %v", got, tt.want)
}
})
}
}