XavierA
XavierA

Reputation: 1775

How to calculate Internal Rate of Return (IRR) in .NET Core

I try to calculate an Internal Rate of Return (IRR) in a C# .NET Core 2.2 project.

Is there any built in formula that I could use?

From the MSDN documentation available here, you should be able to import the VisualBasic Namespace and its Financial Formulas into a .NET Framework Project.

But, trying to do so in a .NET Core Project, I don't get any useful method from the Microsoft.VisualBasic package.

Any hint (apart from coding from scratch the whole algorithm) ?

Upvotes: 1

Views: 3858

Answers (2)

cdev
cdev

Reputation: 5411

There is no built in formula unless you using .NET Core 3.0 Preview 9

double[] values = new double[] { -70000, 22000, 25000, 28000, 31000 };

//// values[0] - Business start-up costs.
//// values[1-n] - Positive cash flows reflecting income for four successive years.

//// Guess starts at 10 percent.
double guess = 0.1;

double CalcRetRate = Financial.IRR(values, guess);

Upvotes: 3

XavierA
XavierA

Reputation: 1775

Bottom line is, as found by @cdev, there is no built in formula unless you switch to .NET Core 3.0 Preview.

But, as a workaround, I found a Nuget Package which wraps Excel Financial Functions Library : ExcelFinancialFunctions from Luca Bolognese and contains an IRR Method.

Then, it becomes straightforward :

    private double ComputeIRR()
    {
        double[] cashFlows = new double[]{-1000,100,100,1200}; //should contain at least one negative and one positive value
        double irr = Financial.Irr(cashFlows);
        return irr;
    }

Upvotes: 4

Related Questions