Hi,

I am new to R, and as a first exercise, I decided to try to implement an XIRR function using the secant method.  I did a quick search and saw another posting that used the Bisection method but wanted to see if it was possible using the secant method.

I would input a Cash Flow and Date vector as well as an initial guess.  I hardcoded today's initial date so I could do checks in Excel.  This code seems to only converge when my initial guess is very close to the correct IRR.

Maybe I have some basic errors in my coding/logic? Any help would be greatly appreciated.

The Wikipedia article to secant method and IRR: http://en.wikipedia.org/wiki/Internal_rate_of_return#Numerical_solution

Thanks!



ANXIRR <- function (cashFlow, cfDate, guess){
        cfDate<-as.Date(cfDate,format="%m/%d/%Y")
        irrprev <- c(0); irr<- guess


        pvPrev<- sum(cashFlow)
        pv<- sum(cashFlow/((1+irr)^(as.numeric(difftime(cfDate,"2010-08-24",units="days"))/360)))
        print(pv)
        print("Hi")


while (abs(pv) >= 0.001) {
        t<-irrprev; irrprev<- irr;
        irr<-irr-((irr-t)*pv/(pv-pvPrev));
        pvPrev<-pv;
        pv<-sum(cashFlow/((1+irr)^(as.numeric(difftime(cfDate,"2010-08-24",units="days"))/365)))
        print(irr);print(pv)
        }
}





Please consider the environment before printing this e-mail.

	[[alternative HTML version deleted]]

