A Python command-line tool for calculating UK Capital Gains Tax (CGT) using the HMRC Section 104 pooling method. Designed specifically for processing investment fund transaction history and generating HMRC-compliant reports.
This calculator processes transaction CSV files exported from investment platforms and calculates capital gains tax liabilities using the Section 104 pooling method as required by HMRC. It handles the complexities of:
- Multiple funds with separate Section 104 pools
- Management fee allocation
- Mid-year CGT rate changes (specifically the 30 October 2024 rate change)
- Automatic duplicate transaction removal
- Multi-file Transaction Loading: Load and merge multiple CSV files from a folder, automatically removing duplicates
- Section 104 Pooling: Proper implementation of the HMRC Section 104 averaging method for share pooling
- Tax Year Handling: Configurable tax year with automatic detection of the previous tax year
- Mid-Year Rate Changes: Special handling for the 30 October 2024 CGT rate change (10%/20% to 18%/24%)
- Management Fee Allocation: Proportional allocation of management fees to transactions in the same month
- Excel Output: Comprehensive Excel report with multiple sheets including:
- Method Statement explaining the calculation approach
- Detailed transaction log with formulas
- Fee Summary breakdown
- HMRC Summary with figures ready for your tax return
- CGT Adjustment Calculator for 2024-25 tax year rate change
The tool expects CSV files with the following characteristics:
- Header rows: The first 5 rows are skipped (metadata/header rows from platform exports)
- Encoding: Supports
cp1252,latin1, andutf-8encodings - Required columns:
Trade date- Date of the transaction inDD/MM/YYYYformatReference- Transaction reference (used to identify management fees via "MANAGE FEE")Description- Fund/security description (used to extract fund names)Unit cost (p)- Price per unit in penceQuantity- Number of units bought or soldValue (£)- Transaction value in pounds (negative for purchases, positive for sales)
[Header row 1 - skipped]
[Header row 2 - skipped]
[Header row 3 - skipped]
[Header row 4 - skipped]
[Header row 5 - skipped]
Trade date,Reference,Description,Unit cost (p),Quantity,Value (£)
15/06/2024,TRD001,Fund Name ABC @ 150.00,150.00,100.000,-150.00
20/06/2024,MANAGE FEE,Management Fee,,,-5.00
01/09/2024,TRD002,Fund Name ABC @ 180.00,180.00,50.000,90.00python cgt_calculator.py /path/to/transaction/folderThis will:
- Process all CSV files in the specified folder
- Calculate CGT for the previous tax year (automatically determined)
- Output results to
cgt_calculations.xlsx
python cgt_calculator.py /path/to/transactions -t 2024-25python cgt_calculator.py /path/to/transactions -o my_cgt_report.xlsxpython cgt_calculator.py ~/Documents/InvestmentExports -t 2024-25 -o cgt_2024-25.xlsx| Argument | Description | Default |
|---|---|---|
input_folder |
Folder containing transaction CSV files (required) | - |
-t, --tax-year |
Tax year in format YYYY-YY (e.g., 2024-25) |
Previous tax year |
-o, --output |
Output Excel filename | cgt_calculations.xlsx |
The tool generates an Excel workbook with the following sheets:
Explains the calculation methodology used, including:
- Section 104 holding pool treatment
- Loss offset rules
- Management fee allocation approach
Detailed transaction log with Excel formulas showing:
- Fund name and transaction date
- Buy/Sell indicator
- Quantity and price
- Raw cost, fee, and net cost
- Running pool quantity and cost
- Running average cost per unit
- Cost basis and gain/loss for sales
Monthly breakdown of management fees showing:
- Total management fee per month
- Number of transactions
- Average fee per transaction
Ready-to-use figures for your Self Assessment tax return:
- Number of disposals
- Disposal proceeds
- Allowable costs
- Gains and losses in the year
- Split by CGT rate change date (for 2024-25)
- Net gain after losses
- Annual Exempt Amount (£3,000 for 2024-25)
- Taxable gain
Interactive worksheet for calculating the CGT adjustment needed due to the mid-year rate change on 30 October 2024. Features:
- Input cells for your income and tax band information
- Automatic calculation of optimal loss and AEA allocation
- Correct tax calculation using both rate periods
- Comparison with HMRC system calculation (which uses old rates)
- Final adjustment figure to enter in your HMRC return
The Section 104 pooling method works as follows:
- Pool Creation: Each unique fund/security maintains its own pool
- Purchases: When you buy units:
- Units are added to the pool quantity
- Purchase cost (plus allocated fees) is added to the pool cost
- A new average cost per unit is calculated
- Sales: When you sell units:
- The cost basis is calculated using the current average cost per unit
- Gain/Loss = Sale Proceeds - (Units Sold × Average Cost)
- The pool quantity is reduced by the units sold
- The pool cost is proportionally reduced
| Transaction | Units | Price | Pool Qty | Pool Cost | Avg Cost |
|---|---|---|---|---|---|
| Buy 100 @ £1.50 | 100 | £1.50 | 100 | £150.00 | £1.50 |
| Buy 50 @ £2.00 | 50 | £2.00 | 150 | £250.00 | £1.67 |
| Sell 80 @ £2.20 | 80 | £2.20 | 70 | £116.67 | £1.67 |
For the sale: Proceeds = £176, Cost Basis = 80 × £1.67 = £133.33, Gain = £42.67
Python 3.7 or higher
pandas
openpyxl
pip install pandas openpyxlOr using a requirements file:
pip install -r requirements.txt- The Annual Exempt Amount is set to £3,000 for 2024-25 tax year onwards
- Management fees containing "MANAGE FEE" in the reference are automatically identified and allocated
- Duplicate transactions (same date, reference, and value) are automatically removed when loading multiple files
- The tool handles floating-point precision issues for "sell all" scenarios
This tool is provided for informational purposes only. While it aims to implement HMRC calculation methods correctly, you should verify all calculations and consult a qualified tax professional for advice on your specific tax situation. The author accepts no responsibility for errors in tax calculations or submissions.