How do I calculate Excel's XIRR
function using C#?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Thanks to contributors of the nuget package located at Excel Financial Functions. It supports many financial methods - AccrInt, Irr, Npv, Pv, XIrr, XNpv, etc.,
Financial.<method_name>
with required parameters.Example:
Result is same as Excel.
I started with 0x69's solution but eventually some new scenarios caused Newton's Method to fail. I created a "smart" version, which uses Bisection Method (slower) when Newton's fails.
Please notice the inline references to multiple sources I used for this solution.
Finally, you are not going to be able to reproduce some of these scenarios in Excel, for Excel itself uses Newton's method. Refer to XIRR, eh? for an interesting discussion about this.
}
According to XIRR function openoffice documentation (formula is same as in excel) you need to solve for XIRR variable in the following f(xirr) equation:
You can calculate xirr value by:
f(xirr)
andf'(xirr)
you can solve for xirr value by using iterative Newton's method - famous formula->EDIT
I've got a bit of time so, here it is - complete C# code for XIRR calculation:
BTW, keep in mind that not all payments will result in valid XIRR because of restrictions of formula and/or Newton method!
cheers!
The other answers show how to implement XIRR in C#, but if only the calculation result is needed you can call Excel's XIRR function directly in the following way:
First add reference to Microsoft.Office.Interop.Excel and then use the following method: