** Internal Return Rate ** Calculates interest rate, based on Cash Flow LOCAL lyInvestment, lyPayment, lnPayments, lyEstimated, lnInterestRate lyInvestment = 1000.00 lyPayment = 87.92 lnPayments = 12 dimension laFlow (lnPayments) FOR i = 1 TO lnPayments laFlow(i) = lyPayment ENDFOR lyEstimated = 0.01 && Assumes any seed value (guess) i.e 1.00 % lnInterestRate = IRR (lyInvestment, @laFlow, lyEstimated, lnPayments ) *************************************************************************** FUNCTION IRR LPARAMETERS tyInvestment, taFlow, tyEstimated, tnPayments LOCAL x1, x2, x3, fx1, fx2, nTries, j, k ** This is an algorithm to solve equations ** Assumes first couple of values based on the estimate x1 = tyEstimated * 0.9 && A value lower than estimate x2 = tyEstimated * 1.1 && A value greater than estiamte x3 = 0 fx1 = 0 && Calculated function using X1 value fx2 = 0 && Calculated function using X2 value nTries = 0 do while .T. nTries = nTries + 1 If nTries = 50 Wait window 'Could not calculate values' RETURN 0.00 Endif ** Calculate Function FX1 . Net Present value of the cash flow FX1 = tyInvestment * (-1) && Initial investment FOR j = 1 TO tnPayments FX1 = FX1 + taFlow(j) / ( 1 + x1 ) ^ j && Net present value for each cash flow ENDFOR ** IF FX1 is very close to zero, the solution was found (X1) ** it is not necesary to calculate more iterations. IF ABS(fx1) < 0.01 Exit ENDIF ** Calculate Function FX2 . Net Present value of the cash flow FX2 = tyInvestment * (-1) && Initial investment FOR k = 1 TO tnPayments FX2 = FX2 + taFlow(k) / ( 1 + x2 ) ^ k && Net present value for each cash flow ENDFOR ** Calculates next iteration value (X3) ** Avoids division by 0 IF (fx1 - fx2) <> 0 x3 = round(x2 - (x1-x2) * fx2 / (fx1 - fx2),11) ENDIF ** Re-Assign values and continue with iterations x1 = x2 x2 = x3 enddo ** X1 is the solution X1 = round(X1,4) WAIT WINDOW TRANSFORM(x1) + " found after " + TRANSFORM(nTries) + " tries." RETURN x1