0

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);
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Honestly, I don't understand your question. Is this about how to calculate the IRR or how to fix a method that calculates the IRR in C#? Your code also isn't helpful, it doesn't provide enough context. What exactly do you need? – Julian Oct 22 '22 at 18:06
  • @ewerspej I need to calculate the IRR for 6 sums that are in the array, as I understood, the number of iterations is not enough to find the number. I'm trying to do everything through iterations now, without using the ExcelFinancialFunction package – perm1ss10n Oct 22 '22 at 18:13

1 Answers1

0

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()