I have two datasets, one with details on stocks purchased called buy and the other with details on stocks sold called sell
buy = data.frame(TransactionID = c(1:10),
Ticker=c(rep('MSFT',4),rep('AMZN',3),rep('DOCU',3)),
Date=c(rep('01-01-2020',2),rep('01-14-2020',2),rep('01-01-2020',2),rep('01-14-2020',1),'01-01-2020','03-15-2020','04-06-2020'),
Price=c(100,102,102,107,2000,2010,2011,197,182,167),
Quantity=c(10,10,5,5,1,1,2,12,15,15))
sell = data.frame(TransactionID=c(1:7),
Ticker=c('MSFT','MSFT','AMZN','AMZN','DOCU','DOCU','DOCU'),
Date=c('01-07-2020','01-20-2020','01-01-2020','01-30-2020','01-15-2020','04-10-2020','04-20-2020'),
Price=c(97,110,2100,2050,210,205,225),
Quantity=c(7,12,1,3,10,5,3))
The Date is inputted in the format mm-dd-YYYY
My objective is to calculate the aggregate gain/loss on all transactions for the time period included in the data, using the FIFO (First in First Out) method.
I'm trying to do this programmatically using R but have not been successful yet.
Method and desired output
I attempted the calculation manually to demonstrate the FIFO calculation method and the final result that I reached. I have been looking for a way to do this using R, but have not found a successful method yet -
1) 01-07-2020 - Ticker= MSFT - Sold 7 @ $97 - Total = $679
FIFO Cost of 7 @ $100 - Total = $700
Gain/Loss = -$21
2) 01-01-2020 - Ticker=AMZN - Sold 1 @ $2100 - Total = $2100
FIFO Cost of 1 @ $2000 - Total = $2000
Gain/Loss = +$100
3) 01-15-2020 - Ticker=DOCU - Sold 10 @ $210 - Total = $2100
FIFO Cost of 10 @ $197 - Total = $1970
Gain/Loss = +$130
4) 01-20-2020 - Ticker=MSFT - Sold 12 @ $110 - Total = $1320
FIFO Cost of 12 @ $[3x$100 + 9x$102] - Total = $1218
Gain/Loss = +$102
5) 01-30-2020 - Ticker=AMZN - Sold 3 @ $2050 - Total = $6150
FIFO Cost of 3 @ $[1x$2010 + 2x$2011] - Total = $6032
Gain/Loss = +$118
6) 04-10-2020 - Ticker=DOCU - Sold 5 @ $205 - Total = $1025
FIFO Cost of 5 @ $[2x$197 + 3x$182] - Total = $940
Gain/Loss = +$85
7) 04-20-2020 - Ticker=DOCU - Sold 3 @ $225 - Total = $675
FIFO Cost of 3 @ $182 - Total = $546
Gain/Loss = +$129
Output
Total Loss: -$21 , Total Gains: $664
Net gains are $643
Any help on how to reach this answer programmatically would be greatly appreciated.