Calculation of Capital Gain based on holding time and FIFO logic

Dear Experts,

This is my transaction Table A that maintains all stocks sale purchase transactions

Neeraj_Malik_0-1710036295512.png

And this is another Table B, subset of Table A, that maintains only the purchase transaction that has stock balances more than 0 which is updated after every sale/purchase txn and data is always sorted by date.

Neeraj_Malik_1-1710036480472.png

Now, while initiating a sale transaction I need to calculate the captial gain based on the holding time.

For example, As per table B, Stock with Ticker Symbol INN.VX has total balance 5500 (Sum of column "Stock_Purchase_Balance" for all rows for Ticker Symbol INN.VX) and need to initiate a sale transaction for selling 4000 stocks of INN.VX

In the sale transaction of 4000 stocks, the stocks needs to be picked from Table B on FIFO basis. And, the capital gain for 2500 stocks from first three Purchase Txn would be at rate of 1% that are having more than 180 days holding time and for the balance 1500 stocks that would be picked from other two Purcashe Txn with holding time of less than 180 would be at rate of 4%

I'm struggling to get the right expression for Capital Gain Tax that will based on the formula for Capital Gain tax as

(Unit Price for Sale Txn - Unit Price for respective Purchase Txns) * 0.01 * (Qty of stocks picked from respective purchase lot with holding time of more than 180 i.e 2500 stocks in this example) + (Unit Price for Sale Txn - Unit Price for respective Purchase Txns) * 0.04 * (Qty of stocks picked from respective purchase lot with holding time of less than or equal to 180 i.e 1500 stocks in this example).

Request, if any of our friend can help me in resolving this struggle. @Steve @Suvrutt_Gurjar @WillowMobileSys @Marc_Dillon @LeventK @MultiTech 

 

Solved Solved
2 2 249
1 ACCEPTED SOLUTION

Hi @MultiTech 
Thanks for your efforts to help me out in best possible manner. Looking at your response I realised that my reported problem is not that simple as I was thinking. Initially I thought I might be missing something basic as a beginner where guidance from experts like you can help me with instant solution. And, your response clarified the complication involved in problem.

Accordingly, I reworked on the overall logic and broken down the whole transaction in two part with the inclusion of state machine.

So in this new implementation, when a sale transaction is initiated, the purchase transactions with stock balance are picked on FIFO basis along with purchase price and parked in a separate table. Multiple purchase transactions are picked till the total of stock balance matches the quantity of sale transaction and Capital Gain is calculated separately for each purchase transaction in this table.

Then, upon the change of state of the sale transaction, the sum total of the Capital Gain from this table is updated against the sale transaction in the original table.

This is how I'm able to achieve what I wanted to. Once again thanks for your time & effort you have put in looking into my problem.

View solution in original post

2 REPLIES 2
Top Labels in this Space