UGuntupalli
UGuntupalli

Reputation: 859

XIRR function from TVM package in R

I am trying to leverage the tvm package in R to calculate XIRR for a set of cash flows and dates.

I have a moving window, where I start off with i = 1 , CF = CF[1], d = d[1] and as I progress forward , the rest of the cash flow gets involved.

I understand the XIRR function will throw an error in the absence of a sign change in the Cash Flow input.

So, in order to handle that I put it in a tryCatch.

For the reproducible example I am providing below, I intend when to see NA until a positive cash flow value is encountered - but once a positive cash flow value is encountered, I expect the function to return a valid value as Excel does.

 # Reprex
    # Attach desired packages 
    suppressPackageStartupMessages(library(tvm))

    # Provide input data 
    CF <- c(-78662, -32491, -32492, -32492, -32493,
            -32494, 7651, 40300, 10003, 9868,
            7530, 7639, 9939, 9804, 7475)
    d <- as.Date(c("2019-06-30", "2019-09-30", "2019-12-31", "2020-03-31", "2020-06-30",
                   "2020-09-30", "2020-12-31", "2021-03-31", "2021-06-30", "2021-09-30",
                   "2021-12-31", "2022-3-31", "2022-06-30", "2022-09-30", "2022-12-31"))
    test <- xirr(cf = CF, d = d)

    print(test)

Any guidance to fix is appreciated

Upvotes: 0

Views: 814

Answers (0)

Related Questions