pythonfinancial

financial python library that has xirr and xnpv function?


numpy has irr and npv function, but I need xirr and xnpv function.

this link points out that xirr and xnpv will be coming soon. http://www.projectdirigible.com/documentation/spreadsheet-functions.html#coming-soon

Is there any python library that has those two functions? tks.


Solution

  • With the help of various implementations I found in the net, I came up with a python implementation:

    def xirr(transactions):
        years = [(ta[0] - transactions[0][0]).days / 365.0 for ta in transactions]
        residual = 1
        step = 0.05
        guess = 0.05
        epsilon = 0.0001
        limit = 10000
        while abs(residual) > epsilon and limit > 0:
            limit -= 1
            residual = 0.0
            for i, ta in enumerate(transactions):
                residual += ta[1] / pow(guess, years[i])
            if abs(residual) > epsilon:
                if residual > 0:
                    guess += step
                else:
                    guess -= step
                    step /= 2.0
        return guess-1
    
    from datetime import date
    tas = [ (date(2010, 12, 29), -10000),
        (date(2012, 1, 25), 20),
        (date(2012, 3, 8), 10100)]
    print xirr(tas) #0.0100612640381