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 224
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

I'll be honest, I wasn't exactly sure how to help you solve your problem - so I fed your question to Appster (just to see what it would say, maybe it would help me with some inspiration or something)... but it's answer didn't really help me much.

Maybe it will mean something to you?

__________________________________________________________________________________________

To calculate the capital gain tax efficiently in AppSheet, you will have to structure your data and app with certain steps and components. Given the complexity of what you're trying to achieve, it might need more than a couple of virtual columns and slices; you might want to think about setting up an action or a series of actions that get triggered to calculate this. Since we have to avoid using SELECT() due to performance concerns, let's opt for a more structured approach.

Here are the steps you can follow:

  1. Implement Transaction Data Capture: Ensure your sale transaction form captures all the necessary data for the calculation (such as the Unit Price of the Sale Transaction, the number of units sold, and the Ticker Symbol). Store this transaction in another table if needed, separate from purchase transactions.
  2. Create Slices to Filter Purchase Transactions: Create two different slices of Table A, one for purchases with holding time of more than 180 days ("Long_Term_Holdings") and another for those with holding time of 180 days or less ("Short_Term_Holdings"). The filter condition for each slice would be a formula like `[Holding_Time] > 180` and `[Holding_Time] <= 180`, respectively.
  3. Utilize Parent-Child References: If not already set up, you may want to establish a parent-child relationship between your stocks main table (Table A) and the table where your sales transactions are stored. This will enable you to more easily work with groups of related records when processing sales.
  4. Create Actions to Process FIFO Logic: Since we're avoiding brute force methods, you'll have to design a process (probably a series of actions and possibly some additional helper columns/slices) that can be triggered during a sale transaction to calculate the stock units being sold in a FIFO manner. This might involve flagging rows in your transactions table that have been โ€˜used upโ€™ in the sales.
  5. Compute Capital Gain Taxes: You will likely need to implement a bot that gets triggered after each sale to calculate the capital gains tax based on the FIFO allocation. It would work on the processed sets of stock records flagged as part of the sale.
  6. Adjust Stock Balances: As part of the FIFO process, you'll also need to update the stock balances in Table B, subtracting the number of stocks sold from the oldest purchase transaction forward.
  7. Create Virtual Column for FIFO Accumulation: You might need a virtual column that will accumulate the total FIFO stocks for INN.VX until it reaches the number of stocks needed for the sale โ€“ while also respecting the holding time to determine the tax rate applicable.
  8. Virtual Column Formula for Capital Gain Tax: Now onto the calculation โ€“ youโ€™d create a Virtual Column (VC) in the sales transaction table (or wherever you store your sales data) for the calculated capital gains. This formula would iterate through the records of the stocks based on the FIFO logic.

While the specific formulas and actions will depend on your app's structure and precise needs, the concept remains the same: setting up a smart structure that can process loads efficiently without causing performance bottlenecks.

Due to the limitations in detailing automation processes and formulas through this medium, you might need to directly engage with resources and experts at MultiTech Visions for more tailored guidance. I would suggest structuring your tables and relationships with this in mind, and move the logic of calculation into the actual actions linked with your Sale Transaction form to ensure FIFO processing and the capital gains tax calculation are executed correctly.

__________________________________________________________________________

It looks like it's trying to create an action looping system for the FIFO, is this something that you'll need to do?    Do you need to process each record one by one sequentially?

___________________________________________________________________________________________
Engage with Appster yourself
- You can chat with the LITE VERSION of Appster if you have a ChatGPT Plus subscription.
- Gain access to the FULL VERSION by signing up at www.MultiTechVisions.com/answers (Main Menu > Support Tiers)

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.

Top Labels in this Space