XIRR stands for Extended Internal Rate of Return and is used to calculate return on investments made at different time periods which need not be periodic.
XIRR gives the annualized return of a portfolio that include all cash flows.
XIRR is a financial function available in Excel. The drawback with Excel's XIRR is that it will show the 0% XIRR as a default for negative returns.
For use in Python, I have written this code which calculates XIRR for both positive and negative returns. For the same XIRR calculation using R, please check my other repository: XIRR_in_R
I hope you find my xirr function useful.
Data should be in a csv file and should have two columns: 1. Date of transaction The date can be any one or a mix of these formats. I set it to consider dayfirst = True: 23/03/1990 19 December 1991 1992-04-10 27/05/92 26.10.92 3/9/93 datetime.date(1990, 3, 23) 2. Transaction amount where purchases (debit) should be a negative number and the redemptions (credit) should be a positive number. dates amount 23/03/1990 -1350 23/07/1990 600 31/10/1990 -1250 13/11/1990 3550 30/01/1991 -850 03/05/1991 3600
Note: Please find the sample data provided in the repository
- Python (version 3.8.2)
- Libraries: Pandas, Numpy
I am using git terminal (CLI) to execute the script.
Clone the repository onto your system from github.
View the list of files and folders.
Pass the location of Sample_Positive_Returns.csv
That is, the return of our portfolio is 17.429% per annum.
Pass the location of Sample_Negative_Returns.csv
That is, the return of our portfolio is -79.848% per annum.