Capital Gains Calculator with FIFO, LIFO, HIFO and Average Cost
Operator: Abdulla Al Fahad. How tools are built and checked
A sale only has a gain once it is tied to a purchase. If you bought the same coin or share in five lots at five prices and sell part of the holding, the answer to "what did I make" depends on which lots you say were sold. Exchange exports give you a flat list of trades, and the lot matching is left to you or to a paid tax package.
This calculator does the matching for a trades table you paste or open. It reads buys and sells, applies FIFO, LIFO, HIFO or average cost, folds fees into the cost of a buy and out of the proceeds of a sell, and shows one line per sale-and-lot pair with its holding period. It also lists the lots you still hold, can value them at prices you type, and totals everything by year and by asset. It is a calculation helper, not tax advice: rules differ by country and by asset type.
Key features
- FIFO, LIFO, HIFO and average-cost matching, chosen with one dropdown so you can compare totals on the same file
- Fees added to the cost of a buy and subtracted from the proceeds of a sell, spread across the lots a sale touches
- Holding period in days per disposal line, with a long-term threshold you set (default 365 days)
- Sales larger than your holdings are flagged with the line number, and you choose whether the excess is left out or counted at zero cost
- Open lots with an unrealised gain once you type a current price for each asset
- Totals by year sold and by asset, split into short-term, long-term and unmatched
- Downloads: disposals CSV, a Form 8949-style column layout, a summary CSV and an XLSX whose gain and summary cells are formulas
Columns the trades file needs and how each method picks lots
The first row must be a header. Columns are found by name, ignoring case and punctuation: date (also timestamp, trade date), asset (symbol, coin, ticker, token), a side column named type, side, action or buy/sell holding values such as buy, sell, bought, sold, quantity (qty, units, shares, amount), price (unit price, rate) and, if there is no price, total (value, proceeds, cost), plus optional fee (commission) and currency columns. Dates can be YYYY-MM-DD, the same with a time, or slash dates read as month/day/year or day/month/year according to the setting beside the method. A time of day is dropped as written, so a timestamp in UTC and one in local time can land on different dates; edit the file if the day matters near midnight.
FIFO consumes the oldest remaining lot of an asset first. LIFO takes the newest. HIFO takes the lot with the highest cost per unit, with the older lot winning a tie, which pushes the lowest-gain lots to the front. Average cost keeps one running price per asset: every buy re-averages the whole pool, and every sale is charged at that price. Because average cost has no individual lots, the holding period for it follows the age of the oldest units, as if the pool were consumed first in, first out; treat that term as indicative.
On the same day, buys are processed before sells, so a morning sale listed above an afternoon purchase does not trigger a false oversell. Other ties keep the order of the file. Quantities carry up to ten decimal places and are exact for eight-decimal amounts at ordinary sizes; with quantities in the millions the last digits can drift. Money is rounded to cents on each disposal line, and the proceeds of one sale are split across its lots so the lines add up to the sale's net proceeds exactly.
The long-term test follows the common one-year rule when the threshold is 365: a holding is long-term when the sale date is after the first anniversary of the purchase date, so a lot bought on 15 January 2023 and sold on 15 January 2024 is still short-term, and one sold on the 16th is long. Any other threshold is a plain count of days, long when the days held exceed it. Fees are assumed to be in the same currency as the prices. If the file lists several currencies the page warns that nothing is converted.
How it works
Rows are validated one at a time. A row with an unreadable date, an unknown side such as a deposit or a transfer, a missing asset, a quantity of zero or less, or no price and no total is skipped, and the page lists the first few skipped lines with their line numbers instead of failing the file. Valid trades are sorted by date and matched asset by asset.
For a sale, the engine takes lots in the order the method dictates until the quantity is covered. Each piece becomes a disposal line with its own proceeds, cost, gain and holding days. Anything not covered by any lot becomes a warning, and is either dropped or reported as a zero-cost line marked No lot, depending on your setting. Lots left at the end are the open lots. For the XLSX the matching is done here and written as values; the gain, the By year and By asset sheets and the value of open lots are formulas you can audit.
When to use it
- Seeing how much of a year's bitcoin gain would be short-term and how much long-term under FIFO versus HIFO before choosing which lots to sell next
- Turning a multi-year exchange export into per-sale lines with acquisition dates and holding periods for an accountant
- Checking an exchange's own gains report by recomputing the same trades with the method it says it used
- Finding the missing purchase or transfer behind an oversell warning before relying on any total
- Valuing what is left in an account at today's prices to see the unrealised gain by lot
Inputs
- Paste your trades, or open a CSV or XLSX, with at least date, asset, buy or sell, quantity and price or total. Use Sample to see the layout.
- Pick the lot-matching method, set the long-term threshold, and choose how an oversold quantity should be treated.
- Read the Disposals tab line by line and compare the Total row on By year and By asset.
- On Remaining lots, type a current price for each asset you hold to see unrealised gains.
- Download the disposals CSV, the Form 8949-style layout, the summary, or the XLSX.
Worked example
Two buys and one sale of bitcoin
date,asset,type,quantity,price,fee
2023-01-10,BTC,buy,0.50,17000,12
2023-06-15,BTC,buy,0.25,26000,8
2024-01-20,BTC,sell,0.40,41500,15Gain on the 20 January 2024 sale (net proceeds 16,585.00)
FIFO 0.40 from the Jan 2023 lot cost 6,809.60 gain 9,775.40 375 days, long
HIFO/LIFO 0.25 from Jun 2023 lot, 0.15 from Jan 2023 lot cost 9,061.60 gain 7,523.40 (3,857.63 short + 3,665.77 long)
Average 0.40 at 20,026.67 per BTC cost 8,010.67 gain 8,574.33Tips for best results
- Run the same file through all four methods and compare the Total row. The spread between FIFO and HIFO on a rising asset shows what lot choice is worth on your own history.
- When a sale triggers an oversell warning, look at the line number in the message first. A coin received by transfer or airdrop has no buy row, so add one with its price at receipt rather than letting the engine guess.
- Set the long-term threshold to match your jurisdiction's holding rule before reading the Short-term and Long-term columns. Some countries have no holding-period split at all, in which case read the Net gain figure.
- Split the file by exchange or wallet if your rules track basis per account. This tool pools every row of an asset together.
- Put a total instead of a price when an exchange gives you only a trade value; the price is derived as total divided by quantity before fees.
Limits
This is a calculation helper, not tax advice. Wash-sale rules, country-specific pooling such as UK share pooling and its same-day and thirty-day matching, staking and mining income, gifts, transfers between your own wallets, and conversion between currencies are not modelled.
Using LIFO or HIFO is not always allowed. In the US, for example, choosing specific lots generally requires that you identify them at the time of sale and keep records; FIFO is the usual default when you do not. Check what your tax authority accepts before reporting a method.
The Form 8949 layout only arranges columns in that order, with short-term and long-term lines separated. It is not a filed form, leaves the adjustment code and amount blank, and does not group lines by reporting category.
Crypto-to-crypto swaps appear in an exchange export as a sale of one asset and a purchase of another at a market value. Make sure each leg is present as a sell and a buy with a price in your reporting currency, or the swap will be missed.
Common errors and how to fix them
- "Could not find a column for: buy/sell (side, type or action)"
- Rename the column that holds buy and sell to type, side or action, or add one. Values such as buy, sell, bought and sold are recognised.
- "unreadable date" on every row
- Use YYYY-MM-DD, or switch the date setting to day/month/year if your slash dates put the day first. Excel serial numbers in a CSV are not read.
- "is not a buy or a sell; skipped"
- Rows such as deposit, withdrawal, staking reward or transfer are skipped on purpose. Add a buy row with the acquisition price if one of them created a lot you later sold.
- "sold ... but only ... was held"
- Earlier history is missing. Add the missing buy or transfer-in, or switch the setting to count the excess at zero cost if that is the basis you intend.
Best practices
- Reconcile the open lots with what your account actually holds before trusting the sale totals; a mismatch usually means a missing row.
How fees enter the numbers
A fee on a buy is added to that lot's cost, so a lot of 0.5 coin bought at 17,000 with a 12 fee costs 8,512 and carries 17,024 per coin. A fee on a sale is subtracted from the proceeds before the proceeds are split across lots. In the example above the sale is 0.40 at 41,500, which is 16,600 gross and 16,585 net after the 15 fee.
If your exchange charges its fee in the asset itself, convert it to your reporting currency at the trade price and put it in the fee column, and reduce the quantity by the same units. The tool does not infer fee currencies.
Frequently asked questions
Which method should I use?
The one your tax rules allow and that you can support with records. FIFO is the most widely accepted default. LIFO, HIFO and average cost are shown so you can see the difference, not as a recommendation.
Why does the long-term split change when I edit the threshold from 365?
At 365 the tool uses the calendar rule, which treats a holding as long-term only after the first anniversary, so leap years do not shift the result by a day. Any other number is a plain count of days held.
Can I export for a spreadsheet?
Yes. The XLSX has Disposals, By year, By asset, Remaining lots, Trades and Notes sheets. Gain, the summaries and the market value of open lots are formulas, and the price cells on Remaining lots can be edited.