I need help calculating IRR for different investments, and the IRR for those investments at different times.
So a have one dataframe that looks like this:
DATE | Investment | Flow |
---|---|---|
2012-05-12 | 1 | -50 |
2013-09-04 | 1 | 100 |
2014-05-05 | 1 | 300 |
2013-09-04 | 2 | -700 |
2015-05-12 | 2 | 1000 |
2012-04-04 | 3 | 100 |
2013-05-12 | 3 | -50 |
2013-09-04 | 4 | -60 |
And another one that looks like this
DATE | Investment | Stock |
---|---|---|
2012-09-05 | 1 | 400 |
2014-05-05 | 1 | 600 |
2014-05-05 | 2 | 300 |
2013-09-04 | 2 | 800 |
2012-09-14 | 3 | 1000 |
2013-09-05 | 4 | 6000 |
So I want to create multiple dataframes that contains the flow of each investment up until the date that I have information on the stock, with the last row containing the stock for that date. So for example, I have 2 observations on the stock for investment 1 so I should create 2 dataframes por investment 1 that look like this:
DATE | Investment | Flow + Stock(last row) |
---|---|---|
2012-05-12 | 1 | -50 |
2012-09-05 | 1 | 400 |
DATE | Investment | Flow + Stock(last row) |
---|---|---|
2012-05-12 | 1 | -50 |
2013-09-04 | 1 | 100 |
2014-05-05 | 1 | 300 |
2014-05-05 | 1 | 600 |
And for investment 3, give that I only have one observation on stock, there should only be 1 dataframe that look like this:
DATE | Investment | Flow + Stock(last row) |
---|---|---|
2012-04-04 | 3 | 100 |
2012-09-14 | 3 | 1000 |
Given that I have a lot of data, manually creating each dataframe is cumbersome, plus I would like this code to update the IRR when I have new information. I want to do this because I want to see the evolution of IRR for each date that I have information on the stock for each investment. Sort of like a time series of IRR for the investments. I will calculate the IRR with the dataframes created.
I have tried ranking the Dates for which I have information on stock, for each investment but have trouble with the loop.
Thank you very much
EDIT: Per request of Henry Ecker, this is a sample of the merged database.
DATE_x Investment Flow DATE_y Stock
355 2018-08-29 1 1371300 2020-09-30 2904678,03
3076 2016-03-31 2 -4535569 2015-06-30 0
1564 2017-11-28 3 1142227 2014-09-30 10378007,31
3666 2018-02-22 2 1622857 2020-03-31 122203846,09
1394 2017-05-16 3 3116642 2017-12-31 0
472 2013-11-09 3 -4364500 2015-12-31 45789217,93
446 2021-02-23 1 325117 2020-03-31 13176648,97
1641 2018-01-31 3 623695 2015-09-30 0
1297 2017-03-21 3 1146193 2015-09-30 32103654,6
2080 2020-09-15 3 461123 2017-09-30 47763628,79