I've got the investment portfolio, see the example below:
portfolio <- data.frame(ISIN = c('ABC US Equity', 'DEF US Equity', 'GHI US Equity'),
Buy.Date = c('22.10.2014', '02.05.2015', '07.08.2016'),
Sale.Date = c('30.05.2015', '15.02.2016', '31.12.2017'),
Buy.Price = c('107.1','101.3','97.8'),
Sale.Price = c('85.69', '102.5', '100'),
Position = c('200','250','300'))
> portfolio
ISIN Buy.Date Sale.Date Buy.Price Sale.Price Position
ABC US Equity 22.10.2014 30.05.2015 107.1 85.69 200
DEF US Equity 02.05.2015 15.02.2016 101.3 102.5 250
GHI US Equity 07.08.2016 31.12.2017 97.8 100 300
My aim is to obtain the single data.frame of the following view:
> portfolio
ISIN Buy.Date Sale.Date Buy.Price Sale.Price Position
ABC US Equity 22.10.2014 30.05.2015 107.1 85.69 200
ABC US Equity 02.05.2015 30.05.2015 107.1 85.69 200
DEF US Equity 02.05.2015 15.02.2016 101.3 102.5 250
DEF US Equity 30.05.2015 15.02.2016 101.3 102.5 250
DEF US Equity 07.08.2016 15.02.2016 101.3 102.5 250
GHI US Equity 07.08.2016 31.12.2017 97.8 100 300
GHI US Equity 15.02.2016 31.12.2017 97.8 100 300
Namely, it should be a snapshot of portfolio holdings on single Buy and Sale Dates, i.e. actually it requires merging together the following data frames:
ISIN Buy.Date Sale.Date Buy.Price Sale.Price Position
ABC US Equity 22.10.2014 30.05.2015 107.1 85.69 200
ISIN Buy.Date Sale.Date Buy.Price Sale.Price Position
ABC US Equity 02.05.2015 30.05.2015 107.1 85.69 200
DEF US Equity 02.05.2015 15.02.2016 101.3 102.5 250
ISIN Buy.Date Sale.Date Buy.Price Sale.Price Position
DEF US Equity 30.05.2015 15.02.2016 101.3 102.5 250
ISIN Buy.Date Sale.Date Buy.Price Sale.Price Position
DEF US Equity 07.08.2016 15.02.2016 101.3 102.5 250
GHI US Equity 07.08.2016 31.12.2017 97.8 100 300
ISIN Buy.Date Sale.Date Buy.Price Sale.Price Position
GHI US Equity 15.02.2016 31.12.2017 97.8 100 300
For now I've just came up with two loop cycles which creates for me single data frames for each of the Buy and Sale Dates
for(i in unique(portfolio$Buy.Date)) {
nam <- paste("portfolio", i, sep = ".")
assign(nam, portfolio[portfolio$Buy.Date==i,])
}
for(j in unique(portfolio$Sale.Date)) {
nam <- paste("portfolio", j, sep = ".")
assign(nam, portfolio[portfolio$Buy.Date==i,])
}
Thus, the problem is now to merge all these data frames in a way so that it will correctly represent portfolio snapshots at different time points as I described above. Could R provide me the solution for this task?
Kindly appreciate all of your help, thank you!
P.S. I do have some current holdings in my portfolio which are not yet sold, that's why I have Sale Date = 31.12.2017. So, last rows should indicate that Date.