Hello guys,
I have a an input file which has a list of Buy and Sell transactions sorted as per the Transaction Date. Please note that the highlighted column 'QTY Remaining' is the output column and the values are the desired output.
The task I have is to traverse through each of the transaction and perform following operations :-
1) If the 'Transaction Type' is Buy then the 'Qty Remaining' should be the 'Qty Purchased'
2) If there are say 2 consecutive Buy transactions then the 'Qty Remaining' should be the addition of 'Qty Purchased' from the current Buy transaction and the 'Qty Purchased' from the previous Buy transaction
3) If the 'Transaction Type' is Sell and the preceding transaction is Buy and the 'Qty Remaining' should be 'Qty Sold' from the Sell transaction minus the 'Qty Purchased' from the Buy transaction from the previous row.
4) If the 'Transaction Type' is Sell and the preceding 2 transactions are Buys and the 'Qty Remaining' should be 'Qty Purchased' from the previous 2 Buy transactions minus 'Qty Sold' from the Sell transaction.
The Point is that everytime when we encounter a 'Sell' transaction, we need to loop through the previous 'Buy' transactions which do not have corresponding Sell transaction and deduct the amount based on each Buy.