Coder Social home page Coder Social logo

kapji / capital-gains-calculator Goto Github PK

View Code? Open in Web Editor NEW
101.0 3.0 36.0 1.13 MB

UK capital gains tax calculator which supports transaction history from different brokers

License: MIT License

Python 96.55% Jinja 3.12% Dockerfile 0.33%
schwab capital-gains-calculator trading212

capital-gains-calculator's Introduction

CI PyPI version

UK capital gains calculator

Calculate capital gains tax by transaction history exported from Charles Schwab, Trading 212 and Morgan Stanley. Generate PDF report with calculations.

Automatically convert all prices to GBP and apply HMRC rules to calculate capital gains tax: "same day" rule, "bed and breakfast" rule, section 104 holding.

Report example

calculations_example.pdf

Installation

Install it with pipx (or regular pip):

pipx install cgt-calc

Prerequisites

  • Python 3.8 or above.
  • pdflatex is required to generate the report.
  • [Optional] Docker

Install LaTeX

MacOS

brew install --cask mactex-no-gui

Debian based

apt install texlive-latex-base

Windows

Install MiKTeX.

Docker

These steps will build and run the calculator in a self-contained environment, in case you would rather not have a systemwide LaTeX installation (or don't want to interfere with an existing one). The following steps are tested on an Apple silicon Mac and may need to be slightly modified on other platforms. With the cloned repository as the current working directory:

$ docker buildx build --platform linux/amd64 --tag capital-gains-calculator .

Now you've built and tagged the calculator image, you can drop into a shell with cgt-calc installed on $PATH. Navigate to where you store your transaction data, and run:

$ cd ~/Taxes/Transactions
$ docker run --rm -it -v "$PWD":/data capital-gains-calculator:latest
a4800eca1914:/data# cgt-calc [...]

This will create a temporary Docker container with the current directory on the host (where your transaction data is) mounted inside the container at /data. Follow the usage instructions below as normal, and when you're done, simply exit the shell. You will be dropped back into the shell on your host, with your output report pdf etc..

Usage

You will need several input files:

  • Exported transaction history from Schwab in CSV format since the beginning. Or at least since you first acquired the shares, which you were holding during the tax year. Schwab only allows to download transaction for the last 4 years so keep it safe. After that you may need to restore transactions from PDF statements. See example.

  • Exported transaction history from Schwab Equity Awards (e.g. for Alphabet/Google employees) since the beginning (Note: Schwab now allows for the whole history of Equity Awards account transactions to be downloaded). These are to be downloaded in JSON format. Instructions are available at the top of the parser file.

  • Exported transaction history from Trading 212. You can use several files here since Trading 212 limit the statements to 1 year periods. See example.

  • Exported transaction history from Morgan Stanley. Since Morgan Stanley generates multiple files in a single report, please specify a directory produced from the report download page.

  • Exported transaction history from Sharesight Sharesight is a portfolio tracking tool with support for multiple brokers.

    You will need the "All Trades" and "Taxable Income" reports since the beginning. Make sure to select "Since Inception" for the period, and "Not Grouping". Export both reports to Excel or Google Sheets, save as CSV, and place them in the same folder.

    Sharesight aggregates transactions from multiple brokers, but doesn't necessarily have balance information. Use the --no-balance-check flag to avoid spurious errors.

    Since there is no direct support for equity grants, add Stock Activity as part of the comment associated with any vesting transactions - making sure they have the grant price filled.

    See example.

  • CSV file with initial stock prices in USD at the moment of vesting, split, etc. initial_prices.csv comes pre-packaged, you need to use the same format.

  • (Optional) Monthly exchange rates prices from gov.uk. exchange_rates.csv gets generated automatically using HMRC API, you need to use the same format if you want to override it.

Then run (you can omit the brokers you don't use):

cgt-calc --year 2020 --schwab schwab_transactions.csv --trading212 trading212/ --mssb mmsb_report/

See cgt-calc --help for the full list of settings.

Disclaimer

Please be aware that I'm not a tax adviser so use this data at your own risk.

Contribute

All contributions are highly welcomed. If you notice any bugs please open an issue or send a PR to fix it.

Feel free to add new parsers to support transaction history from more brokers.

Testing

This project uses Poetry for managing dependencies.

  • For local testing you need to install it.
  • After that run poetry install to install all dependencies.
  • Then activate pre-commit hook: poetry run pre-commit install

You can also run all linters and tests manually with this command:

poetry run pre-commit run --all-files

capital-gains-calculator's People

Contributors

a-square avatar amaramrahul avatar danielkza avatar deed02392 avatar dependabot[bot] avatar goofy57 avatar hermanho avatar ivendor avatar kapji avatar nihn avatar raffomartini avatar romanandreev avatar stepancheg avatar thibwk avatar vmartinv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

capital-gains-calculator's Issues

Schwab csv has 8 columns now

I have just downloaded csv files for my accounts in Schwab.
Get the following error:

..
cgt_calc.exceptions.ParsingError: While parsing XXXXX....csv, Second line of Schwab transactions file must be a header with 9 columns

version: 4a22b6d

Excerpt from my CSV. Both my accounts trading and equity awards have the same layout.

"Date","Action","Symbol","Description","Quantity","Price","Fees & Comm","Amount"
"12/29/2022","Credit Interest","","SCHWAB1 INT 11/29-12/28","","","","$70.18"

Equalisation and notional distributions from accumulation units?

Thanks so much for this tool!

Would you mind clarifying how the calculator handles any equalisation payments (which I believe should reduce the cost base) or notional distributions from accumulation units (which would increase the cost base)? As Sharesight doesn't seem to support these directly I want to make sure I'm entering them correctly (and therefore being exported here accurately).

Sharesight file formats

Hi there, I'm trying to use cgt-calc with Sharesight files. However, when I export files from Sharesight (AlltradesReport and taxable_income report) they are downloaded as .xlsx files (not .csv) and the format bears no resemblance to the example files you provide. Can you please check or given instruictions for how to download from sharesight the files in the correct .csv formats. Many thanks

Schwab Transaction History format has changed (price is now before qty)

File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/cgt_calc/parsers/schwab.py", line 194, in read_schwab_transactions
raise ParsingError(
cgt_calc.exceptions.ParsingError: While parsing schwab-transaction-history.csv, First line of Schwab transactions file must be something like 'Transactions for account ...'

Looking into the code and the file, it seems that it used to expected price column to be after quantity but this seems to have changed in the latest CSV export format

Support for Vanguard

I was wondering if we have considered adding support for funds sold in Vanguard (similar to Schwab and T212) and how much work that would be? :)

AssertionError: `same_day_quantity <= acquisition_quantity`

Hi,
Firstly, thanks for open sourcing this tool!
Secondly, this bug is not something that's causing me "Jan 31 worries".
Thirdly, the raw format is quite handy for debugging and excluding trading-platform-specific parser issues, thanks for that, too.

With the following raw format, I manage to trigger an assertion error:

2023-06-30,SELL,STOCK,100,100.00,1.0,USD
2023-06-30,STOCK_ACTIVITY,STOCK,50,99.00,1.0,USD
2023-06-25,SELL,STOCK,30,100.00,1.0,USD
2023-01-01,STOCK_ACTIVITY,STOCK,500,101.10,1.0,USD

I can't see anything particularly wrong with the transaction data (it's a minimal & anonymised repro of real data, and if I exclude the sale on 2023-06-25 then the tool runs fine).

repro:

python -m cgt_calc.main --year 2023 --exchange-rates-file ./hmrc_fx.csv --report ./raw_test.pdf --raw ./raw_test.csv

Output:

INFO: No schwab file provided
INFO: No schwab Equity Award JSON file provided
INFO: No trading212 folder provided
INFO: No mssb folder provided
INFO: No sharesight file provided
First pass completed
Final portfolio:
  STOCK: 419.00
Final balance:
  Unknown: 13098.00 (USD)
Dividends: £0.00
Dividend taxes: £0.00
Interest: £0.00
Disposal proceeds: £10589.62

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/whatever/capital-gains-calculator/cgt_calc/main.py", line 717, in <module>
    init()
  File "/home/whatever/capital-gains-calculator/cgt_calc/main.py", line 713, in init
    sys.exit(main())
             ^^^^^^
  File "/home/whatever/capital-gains-calculator/cgt_calc/main.py", line 696, in main
    report = calculator.calculate_capital_gain(acquisition_list, disposal_list)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/whatever/capital-gains-calculator/cgt_calc/main.py", line 587, in calculate_capital_gain
    ) = self.process_disposal(
        ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/whatever/capital-gains-calculator/cgt_calc/main.py", line 434, in process_disposal
    assert same_day_quantity <= acquisition_quantity
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

This is with the current HEAD revision (339b854), but I don't think the issue is very new as I also hit it with da4a4da.

Support for in Schwab Action "Journaled Shares"

An entry in Schwab with "Journaled Shares" action is not supported. I saw this entry when I transferred (gifted) shares to spouse account.

Here is the sample entry from my Schwab transactions history CSV file.

"Date","Action","Symbol","Description","Quantity","Price","Fees & Comm","Amount"
"05/13/2022","Journaled Shares","FB","META PLATFORMS INC CLASS A","-32","$198.62","",""

I also find a corresponding entry with the same action in my spouse's exported transaction history.

"Date","Action","Symbol","Description","Quantity","Price","Fees & Comm","Amount"
"05/13/2022","Journaled Shares","FB","META PLATFORMS INC CLASS A","32","$198.62","",""

Kindly update logic to support transfer of shares.

Unrealized gains calculation

I was thinking it would be useful to have an estimation of unrealized gains for the ongoing years, the way I was thinking of doing this is the following:

  • add a —show-unrealized-gains flag
  • If the flag is set, include it in the final portfolio (in the console output)
  • To calculate the unrealised gains for each symbol fetch the current price (we can use yahoo finance api) and compare it with the calculated pool price. unrealized gains= current price*quantity-pool price

Does that sound like something we want to have? I can add a PR for this.

Sharesight problem - not handling Share Splits

Hi there, After tweaking the formats (see previous issue) and using Google Sheets to save as .csv (don't download direct Excel from Sahresight !) I've not hit a problmem where a share I own has undergone a Split - so in the .csv file the a row has Split for the TYpe of action. cgt-calc throws up an error (Unknown action: Split). This is needed as without the split when I came to sell the shares cgt-calc threw an invalid transaction error as it thought I was trying to sell more shares than I owned. Can this be easily fixed?

Consider using nix for dependency management and setup

Nix would provide a fully deterministic development environment, also for people who just want to use this tool without developing (similar to the current Docker-based solution, but much more usable).

I can contribute the nix files and instructions if there is interest.

More Help please ref #475

Hi - so I have installted the Miktex console on windows pc and my csv file is ready.

Anyone able to provide the step by steps what I do next to get the cgt calcs and report please? (or direct me to an article that explains this?)

Thanks!

Schwab equity award transactions history export csv is updated

I am receiving the following error when running the command:

$ cgt-calc --year 2022 --schwab XXXXX611_Transactions_20240104-182027.csv --schwab-award EquityAwardsCenter_Transactions_20240104182317.csv 
Traceback (most recent call last):
  File "/home/rahul/.local/bin/cgt-calc", line 8, in <module>
    sys.exit(init())
  File "/home/rahul/.local/pipx/venvs/cgt-calc/lib/python3.9/site-packages/cgt_calc/main.py", line 713, in init
    sys.exit(main())
  File "/home/rahul/.local/pipx/venvs/cgt-calc/lib/python3.9/site-packages/cgt_calc/main.py", line 672, in main
    broker_transactions = read_broker_transactions(
  File "/home/rahul/.local/pipx/venvs/cgt-calc/lib/python3.9/site-packages/cgt_calc/parsers/__init__.py", line 57, in read_broker_transactions
    transactions += read_schwab_transactions(
  File "/home/rahul/.local/pipx/venvs/cgt-calc/lib/python3.9/site-packages/cgt_calc/parsers/schwab.py", line 178, in read_schwab_transactions
    awards_prices = _read_schwab_awards(schwab_award_transactions_file)
  File "/home/rahul/.local/pipx/venvs/cgt-calc/lib/python3.9/site-packages/cgt_calc/parsers/schwab.py", line 264, in _read_schwab_awards
    raise UnexpectedColumnCountError(
cgt_calc.exceptions.UnexpectedColumnCountError: While parsing EquityAwardsCenter_Transactions_20240104182317.csv, The following row doesn't have 8 columns:
...

I believe the format of csv export for Schwab Equity Awards Transaction history has changed.

Help please

Hi,

Sorry I am unable to find the page in the readme file:-

See cgt-calc --help for the full list of settings. - Please can you provide a link?

I want to use the application for UK 30 day rules - I use Barclays stockbrokers, can I still use the app. I presume I can just convert my trades into into a readable csv file? Is there a step by step process anywhere? I guess it is in help section?

Thanks and sorry for the basic questions!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.