The problem
When having a continuous buy and sell system (like online trading) then the "principal" is meaningless.
Spreadsheet XIRR is desired.
The solution
Implement a version that accepts a stream of transactions (positive and negative).
An alternative 1
Use composer require phpoffice/phpspreadsheet and the following code:
use \PhpOffice\PhpSpreadsheet\Calculation\Financial;
$xirr = Financial::XIRR($values, $dates, 0.1);
Note, that dates can have many formats. It does not accept epoch, however. But epoch/(24 * 60 * 60) works.
Note, that phpoffice/phpspreadsheet is a fairly large package, and installing it only for XIRR, is a bit overkill.
Note, that this package is maintained, and is well tested.
An alternative 2
There is a manually downloadable package that performs the desired XIRR:
https://www.phpclasses.org/package/892-PHP-Financial-functions-with-the-Excel-function-names-.html
But the downside is that one must register to download it manually, and from there on maintain it oneself.
The problem
When having a continuous buy and sell system (like online trading) then the "principal" is meaningless.
Spreadsheet XIRR is desired.
The solution
Implement a version that accepts a stream of transactions (positive and negative).
An alternative 1
Use
composer require phpoffice/phpspreadsheetand the following code:Note, that dates can have many formats. It does not accept epoch, however. But epoch/(24 * 60 * 60) works.
Note, that
phpoffice/phpspreadsheetis a fairly large package, and installing it only for XIRR, is a bit overkill.Note, that this package is maintained, and is well tested.
An alternative 2
There is a manually downloadable package that performs the desired XIRR:
https://www.phpclasses.org/package/892-PHP-Financial-functions-with-the-Excel-function-names-.html
But the downside is that one must register to download it manually, and from there on maintain it oneself.