Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
1836b6bcf9
|
|||
|
83edcc7c62
|
|||
|
bd6c0a4d25
|
|||
|
f10c652292
|
|||
|
8ac353fa81
|
|||
|
3482a8cb44
|
|||
|
11e1c226ee
|
|||
|
17598720eb
|
|||
|
b1c253f250
|
|||
|
a46edc6d2b
|
|||
|
31de7837e1
|
32
.github/workflows/go.yml
vendored
Normal file
32
.github/workflows/go.yml
vendored
Normal 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
3
.golangci.yml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
linters:
|
||||||
|
enable:
|
||||||
|
- wsl
|
||||||
28
README.md
28
README.md
@@ -1 +1,29 @@
|
|||||||
# goxirr
|
# goxirr
|
||||||
|
[](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
28
xirr.go
@@ -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
72
xirr_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user