Cuma, Ağustos 25, 2017

C# – IRR Function

Today I was working on a financial application for a project. One of the spec’s was an IRR (internal rate of return) calculation that had to be done. Since C# doesn’t offer any classes that can calculate the IRR, I decided to use the .Net Framework alternative: the IRR method from the Financial Class (in Microsoft.VisualBasic.dll). So one could call this method in the following way:

double IRRValue = Microsoft.VisualBasic.Financial.IRR(ref irrArray, estimate);


Unfortunately this will only work for simple IRR calculations. My project has an array containing 16 cashflows. I have concluded that the abovementioned IRR method can only calculate arrays that don’t exceed 12 cashflows. If you exceed 12 cashflows the method throws an exception stating “Arguments Are Not Valid". (yes my friend, you are reading this correctly) Furthermore (yes, there is more), if you read the MSDN documentation on this beautiful method you will see that is says: “If IRR cannot find a result after 20 tries, it fails.”. For complex calculations chances are that 20 is to little, and thus you will get the aforementioned exception again.