Git Product home page Git Product logo

xirr's Introduction

Xirr

To calculate the internal rate of return for a schedule of cash flows that is not necessarily periodic.

Installation

  • If you are using Gradle just add the following dependency to your build.gradle.
implementation "com.github.joutvhu:xirr:1.0.5"
  • Or add the following dependency to your pom.xml if you are using Maven.
<dependency>
    <groupId>com.github.joutvhu</groupId>
    <artifactId>xirr</artifactId>
    <version>1.0.5</version>
</dependency>

Using

To use the xirr function you need to create a Xirr object.

You can use the Xirr.instance() method to create a Xirr object. Xirr cycles through the calculation until the result is accurate within 0.000001 percent. If Xirr can't find a result that works after 100 tries, you'll get a XirrException.

You can also change the accuracy and tries using the Xirr.of(double accurate, double tries) method.

// Xirr.instance() -> precision = 0.000001, tries = 100;
Xirr xirr = Xirr.instance();

// Xirr.of(double precision, double tries);
Xirr xirr1 = Xirr.of(0.000001, 1000);

The Syntax of the xirr function:

  • Xirr.xirr(Transaction[] transactions)

  • Xirr.xirr(Transaction[] transactions, double guess)

  • Xirr.xirr(double[] values, long[] days)

  • Xirr.xirr(double[] values, long[] days, double guess)

The arguments of the xirr function:

  • values required. A series of cash flows that corresponds to a schedule of payments in dates. The first payment is optional and corresponds to a cost or payment that occurs at the beginning of the investment. If the first value is a cost or payment, it must be a negative value. All succeeding payments are discounted based on a 365-day year. The series of values must contain at least one positive and one negative value.
  • days required. A schedule of payment dates that corresponds to the cash flow payments. Dates may occur in any order.
  • transactions required. A series of pairs of value and day.
  • guess optional. A number that you guess is close to the result of Xirr. If omitted, guess is assumed to be 0.1 (10 percent).
// rate ~ 0.35899244 ~ 35.90%
double rate = Xirr.instance().xirr(
        new Transaction(-10000,"2008-01-01"),
        new Transaction(2750,"2008-03-01"),
        new Transaction(4250,"2008-11-30"),
        new Transaction(3250,"2009-02-15"),
        new Transaction(2750,"2009-04-01")
);

xirr's People

Contributors

joutvhu avatar

Stargazers

 avatar  avatar

Watchers

 avatar

xirr's Issues

It does not work with negative XIRR

It doesn't get correct value for below data.
@test
public void xirr_tc_1() {
Xirr xirr = Xirr.instance();
List transactions = Arrays.asList(
new Transaction(-6565763.490000000000000, "2018-10-02"),
new Transaction( -91577.340000000000000, "2018-10-02"),
new Transaction(317505.630000000000000, "2023-09-30"),
new Transaction( 4428.480000000000000, "2023-09-30")
);
double r = xirr.xirr(transactions);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.