0

I'm importing a data frame from a csv file that looks like the below, however with hundreds of strategies and thousands of cash flows for each strategy:

TRADE_STRATEGY  EVENT_DATE  BOOK_VALUE_USD
A               1/1/2021    -1.5
A               2/28/2021   -2
A               3/31/2021   4
B               2/1/2021    -1.75
B               3/31/2021   -1.25
B               4/30/2021   5.75

How can I loop through the XIRR function that would save results in a data frame? I.e.

TRADE_STRATEGY  IRR
A               1.357539928
B               2.48654511
DoRemy95
  • 614
  • 3
  • 19

1 Answers1

0

I can suggest using the group_by() function of the dplyr library.

With this, you may group your "data.frame" into different trade strategies (TRADE_STRATEGY) and them compute the IRR on the cash flow (BOOK_VALUE_USD)

Note that I did not know which IRR function to use in R so I decided to use the one suggested by @dmi3kno in this post.

Here is the code:

df <- data.frame(list(
  TRADE_STRATEGY = c("A", "A", "A", "B", "B", "B"),
  EVENT_DATE = c("1/1/2021", "2/28/2021", "3/31/2021", "2/1/2021", "3/31/2021", "4/30/2021"),
  BOOK_VALUE_USD = c(-1.5, -2, 4, -1.75, -1.25, 5.75)
  ))

df %>% 
  group_by(TRADE_STRATEGY) %>% 
  summarise(IRR_value = irr(BOOK_VALUE_USD)$IRR) # The irr() function from @dmi3kno
DoRemy95
  • 614
  • 3
  • 19