perm1ss10n
perm1ss10n

Reputation: 1

Calculation of Internal Rate of Return (IRR) in C#

Good evening, I ran into the problem of calculating IRR in C# for an array of cash flows. On the advice from another topic, I used the ExcelFinancialFunction package, but when calculating it gives an error:

System.Exception: "Not found an interval comprising the root after 60 tries, last tried was (-172638512857238298689536.000000, 280537583393012226981888.000000)"

Is it possible to choose the number of possible iterations to calculate the IRR? Maybe someone has encountered something like this? Unfortunately, my skills are not enough to implement an iterative approach and select the value of IRR
The numbers that I enter into the array through the text field: -30000000, 5000000, 5000000, 5000000, 5000000, 5000000

for (int i = 1; i < Data.FinYears; i++)
    {
     Data.profit[i] = Convert.ToDouble(textBoxes[i].Text);
     }
double IRR = Financial.Irr(Data.profit);

Upvotes: 0

Views: 606

Answers (1)

perm1ss10n
perm1ss10n

Reputation: 1

My friend helped me, as a result we got this:

double VSD = iter(0.0d, 0.1d);
    private double summ(double irr)
    {
        double sum = 0;

        for (int i = 0; i < Data.profit.Length; i++)
        {
            sum = sum + Data.profit[i] / Math.Pow((1 + irr), i);

        }
        return sum;
    }
    private double iter(double irr, double k)
    {
        double summm = summ(irr);
        if (summm < -0.999d)
        {
            irr -= k;
            return iter(irr, k);
        }
        if (summm > 0.001d && k > 0.00000000001d)
        {
            irr += k;
            k /= 10;
            irr -= k;
            return iter(irr, k);
        }
        else
        {
            return irr * 100;
        }
    }

Maybe someone will need something similar too. Data.profit[] is an array with money flows, VSD is IRR as a percentage, VSD is in public DecisionForm()

Upvotes: 0

Related Questions