Skip to content

UK Capital Gains Tax calculator using HMRC Section 104 pooling method

Notifications You must be signed in to change notification settings

markgewhite/hmrc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 

Repository files navigation

UK Capital Gains Tax Calculator

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.

Overview

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

Features

  • 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

Input Format

CSV Structure

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, and utf-8 encodings
  • Required columns:
    • Trade date - Date of the transaction in DD/MM/YYYY format
    • Reference - 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 pence
    • Quantity - Number of units bought or sold
    • Value (£) - Transaction value in pounds (negative for purchases, positive for sales)

Example CSV Structure

[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.00

Usage

Basic Usage

python cgt_calculator.py /path/to/transaction/folder

This will:

  • Process all CSV files in the specified folder
  • Calculate CGT for the previous tax year (automatically determined)
  • Output results to cgt_calculations.xlsx

Specifying Tax Year

python cgt_calculator.py /path/to/transactions -t 2024-25

Custom Output File

python cgt_calculator.py /path/to/transactions -o my_cgt_report.xlsx

Full Example

python cgt_calculator.py ~/Documents/InvestmentExports -t 2024-25 -o cgt_2024-25.xlsx

Command-Line Arguments

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

Output

The tool generates an Excel workbook with the following sheets:

Method Statement

Explains the calculation methodology used, including:

  • Section 104 holding pool treatment
  • Loss offset rules
  • Management fee allocation approach

Transactions

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

Fee Summary

Monthly breakdown of management fees showing:

  • Total management fee per month
  • Number of transactions
  • Average fee per transaction

HMRC Summary

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

CGT Adjustment Calculator (2024-25 only)

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

Tax Logic: Section 104 Pooling

The Section 104 pooling method works as follows:

  1. Pool Creation: Each unique fund/security maintains its own pool
  2. 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
  3. 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

Example

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

Requirements

Python Version

Python 3.7 or higher

Dependencies

pandas
openpyxl

Installation

pip install pandas openpyxl

Or using a requirements file:

pip install -r requirements.txt

Notes

  • 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

Disclaimer

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.

About

UK Capital Gains Tax calculator using HMRC Section 104 pooling method

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages