Calculating UK trading tax liability with Python
Do you trade in the UK? I do. I have a scientific trading system which buys and sells futures, and I additionally do a bit of fairness and ETF buying and selling. This 12 months I even have made a few cash, so I want to pay some tax. There are 3 ways you can be taxed:
1) Not at all.
This handiest applies to those using spread-bets, or allegedly my ex boss Stanley.
2) Income tax.
It is probably feasible to pay earnings tax for your trading income, in case you're classed as incomes it by 'buying and selling' (the HMRC definition) in place of through hypothesis. If you earn the majority of your income from buying and selling, and you fulfill positive other exams, then you may have your buying and selling earnings taxed underneath the earnings tax regime.
Three) Capital Gains Tax.
Probably the bulk of human beings working as traders inside the UK will pay capital gains tax (CGT) on their buying and selling profits.
I intend to ship HMRC both calculations this yr, plus an evidence of exactly what I am doing, to see in the event that they think my trading profits ought to be taxed as earnings. This may also or now not be high quality, but as HSBC are learning, you don't mess with the taxman or girl so it is better to be open and honest.
The regulations for UK capital gains are ... Interesting.
If you just purchase and promote UK shares however you can make use of fine web sites like http://www.Cgtcalculator.Com/. However this could't cope with quick selling, or overseas foreign exchange, or futures for which fee does not identical charge. It additionally would not without problems cope with the .Html reports produced by interactive agents (IB).
So I've spent the closing week or so hacking collectively a few python code which offers with those problems.
I can't emphasise enough which you should be very cautious with this code. It can be complete of mistakes. Tax regulations may also exchange. My interpretation of how HMRC regulations are carried out to futures and foreign forex trades may be incorrect. Its no substitute for expert advice. Be careful available.
Getting and jogging the code
You'll want python and the pandas library. If you are going to use the .Html reviews produced by using IB you'll additionally want Beautiful Soup.
You'll probably also need the quandl python API for FX prices, unless you have another source you prefer (in which case you'll need to patch it in yourself).
You can get the source code from githubhere..
If all is properly you ought to be capable of run the code in the instance.Py report. This will produce a document for some made up data that I created.
Trade and position source files
You can get exchange statistics from eithier the .Html reports produced via IB, or from flat .Csv files. Made up examples of each are included inside the repo. To get your personal IB alternate documents log in to Account manager... Reports.... Exchange confirmations. And then keep as .Html. Reports need to cover the duration from whilst you opened your account. You can simplest run three hundred and sixty five days of alternate reports at a time, so its a terrific idea to run them often and shop them
We glaringly need trade files, but why do we need positions? These are non-compulsory, but very useful. The very last positions I have after processing my trades ought to in shape what I even have from the location documents. To get the IB positions log in to Account manager... Reports.... Pastime file. Save as .Html
Spot FX positions are hard to extract from the position reports IB produce and even harder to reconcile. So its quite normal to get a break between trades and positions for this asset class.
It's feasible to join collectively multiple trade and position items, and I show how this is executed in the example.
Output file (argument reportfile)
The instance outputs to a text document which you could trade. If you eliminate this argument from the calling characteristic then it will document to the display screen.
FX Data (argument fxsource)
There are 3 methods to get FX records. You can use FIXED FX quotes, a fixed of which I've blanketed (that is the default in the example). You can download them from the top notch internet site QUANDL. I use my own DATABASE. Naturally this option may not give you the results you want, until you convert the code in databasefxrates.Py.
Calculation method (argument CGTCalc)
As I said above its no longer clear if us buyers need to calculate our tax using earnings tax or capital profits. If you place this flag to False it'll calculate trading income the use of a easier approach of average price thus far versus realised value.
Verbosity (argument reportinglevel)
There are numerous degrees of detail available.
- ANNUAL - Gives a summary for the year
- BRIEF - As above, plus one line per trade
- NORMAL - As above, plus matching details
- CALCULATE - As above, plus explicit calculations
- VERBOSE - As above, plus a breakdown of the matching trades
Digging into the statistics
If you run the report at the BRIEF level as in the example you might want to dig into specific trades. For example lets have a look at the December 2014 French Bond (BTP) futures trades. We can eithier just look the trades for that code (at whatever level of detail we want):
taxcalc_dict['FBTP DEC 14'].
Display_taxes_for_code(taxyear=2015, CGTCalc=CGTCalc, reportinglevel="CALCULATE")
Or there is probably a specific exchange we're interested by. We will want the reference variety, which is highlighted in the document extract underneath (note that is different from the TradeID which we will see later)
2: SELL 1 FBTP DEC 14 Futures on 16/10/2014 at EUR 129,750 each gives PROFIT of EUR 1,926 equals GBP 1,589
3: SELL 2 FBTP DEC 14 Futures on 16/10/2014 at EUR 126,900 each gives LOSS of EUR -6,958 equals GBP -5,742
4: SELL 1 FBTP DEC 14 Futures on 23/10/2014 at EUR 128,820 each gives LOSS of EUR -876 equals GBP -726
5: SELL 1 FBTP DEC 14 Futures on 06/11/2014 at EUR 130,180 each gives PROFIT of EUR 484 equals GBP 401
Trade quantity three seems thrilling, so we will get a complete breakdown for that
taxcalc_dict['FBTP DEC 14']. matched[3].group_display_taxes(taxyear=2015, CGTCalc=CGTCalc, reportinglevel="VERBOSE")
As a bonus you might need to analyse the earnings or losses in your very own functions.
## Bonus characteristic - examine income
income=taxcalc_dict.Return_profits(2015, CGTCalc)
profit_analyser(profits) Returns:
920 Trades Profits 412 Losses 505
Average income 185.18 Average loss -ninety four.34
Total profits with the aid of code:
code profit
76 VXX4 -2117
61 BRWMl -1843
108 FVS NOV 14 -1339
34 014Z -1026
......
196 VXN4 1630
224 FOAT SEP 14 1934
178 FVS JUN 14 2107
52 SLIl 2325
65 CGLl 3219
32 VXM4 4173 June was clearly better than November for european volatility futures....
What does all of it imply?
Capital gains tax example
Here is a especially interesting German 2 year bond (Shatz) future - my least favored marketplace right now.
SELL 9 FGBS DEC 14 Futures on 06/11/2014 at EUR 110,975 each gives PROFIT of EUR 195 equals GBP 129Commission EUR 18 and taxes EUR zero on SELL
We sold 9 lots of the Shatz contract on the 6th November. We made 195 Euros, which is converted to GBP at the FX rate on the date we closed the trade.Trade information:ID 1550 Code FGBS DEC 14 Date 2014-11-06 05:00:10 Quantity -9 Price a hundred and ten.975 Value in keeping with block 110,975
The exchange ID of the remaining alternate is 1550. These ID's are allotted whilst we read inside the .Html files; when you have your own source of exchange facts you can use your very own (assuming they're unique). Note we are differentiating among rate and value - for this future one is 1000 times bigger than the opposite. Sometimes you'll see trade ID's like this 1500:1 or 1500:2. That means I've split a 'natural' alternate right into a close and an open, in which the alternate as a whole adjustments the signal.
Total allowable cost EUR 998,562 Total disposal proceeds EUR 998,757
CGT profit is the distinction between these values. Disposal is what we get when we promote. Allowable cost is what we paid to shop for. Commissions and expenses are deducted from both values.
Now we input the whacky world of matching. Each of these nine masses wishes to be matched with buys. First we fit with trades that happened the equal day.
Matches with:
SAME DAY TRADE(S) Matches with BUY of 1 FGBS DEC 14 at common of EUR one hundred ten,985 every
Commissions EUR 2 Taxes EUR 0
Trades:
ID 1531 Code FGBS DEC 14 Date 2014-11-06 02:fifty two:40 Quantity 1 Price 110.985 Value in step with block 110,985
There was a buy of 1 lot a few hours earlier. Next we look at trades made in the 30 days after the closing trade - the so called 'bed and breakfust' rule.
SUBSEQUENT 2 TRADE(S) Within 30 days among 2014-11-14 and 2014-eleven-27: Matches with BUY of 2 FGBS DEC 14 at of EUR one hundred ten,935 each
Commissions EUR four Taxes EUR 0
Trades:
ID 1532 Code FGBS DEC 14 Date 2014-eleven-14 02:fifty one:17 Quantity 1 Price 110.950 Value in keeping with block a hundred and ten,950
ID 1533 Code FGBS DEC 14 Date 2014-11-27 05:22:12 Quantity 1 Price a hundred and ten.920 Value consistent with block one hundred ten,920
Finally we've got the delights of the section104 protecting. This is efficiently a seasoned-rata of the unmatched trades which you held earlier than you bought. Because its a seasoned-rata we want to cut up out a proportion of the previous trades.
PRO-RATA SECTION 104: Quantity 6.000000 FGBS DEC 14 allotted from total preserving of 32, made up of 10 trades between 2014-09-01 and 2014-11-05
At common fee of EUR 110,948 Commissions EUR 12.0000 Taxes EUR 0
Trades:
ID 1499a Code FGBS DEC 14 Date 2014-09-01 05:03:forty three Quantity 4 Price 110.930 Value consistent with block one hundred ten,930 (Allocated from: ID 1499 Quantity 20)
ID 1514a Code FGBS DEC 14 Date 2014-10-02 02:59:forty one Quantity zero.200000 Price 111.0.5 Value in step with block 111,half (Allocated from: ID 1514 Quantity 1)
..... Several trades eliminated ....
ID 1529c Code FGBS DEC 14 Date 2014-11-03 02:49:50 Quantity 0.200000 Price one hundred ten.985 Value in line with block a hundred and ten,985 (Allocated from: ID 1529 Quantity 3)
ID 1530a Code FGBS DEC 14 Date 2014-eleven-05 08:00:37 Quantity zero.200000 Price a hundred and ten.970 Value in line with block a hundred and ten,970 (Allocated from: ID 1530 Quantity 1)
Thus the suffix a,b,c....On each TradeID, the fractional change sizes, and the information telling us which trade the seasoned-rata allocation comes from. Finally the specific calculation:
CALCULATION: (9*110975) - 18 - 0 -(1*110985) - 2 - 0 -(2*110935) - 4 - 0 -(6*110948) - 12.0000 - 0 = 195
This spells out how we made our cash. Each term shows the variety of masses traded multiplied through price, much less trades and commissions.
Short tax example
Let's examine a short sale (I've removed a few detail here).
CLOSE SHORT 100000 AUD.USD the Forex market on sixteen/12/2014 at USD 0.821980 every gives LOSS of USD -ninety nine equals GBP -60
Commission USD 1.28000 and taxes USD zero on CLOSE SHORT
Total allowable cost USD 82,196.72 Total disposal proceeds USD 82,100
CALCULATION: -(100000*0.821980) - 1.28000 - 0 +(100000*0.821000) - 0 - 0 = -99
Notice that the allowable value and disposable proceeds are reversed; the cost still refers back to the purchase despite the fact that this is the ultimate exchange. The minus check in front of the primary term of the calculation, and the plus signal inside the 2nd term, additionally highlights this.
Annual precis
Note we get annual summaries of a number of this records, on the way to correspond to entries to your tax go back.
Summary for tax 12 months finishing fifth April 2015
Figures in GBP
Disposal Proceeds = 441,615, Allowable Costs = 424,080, Disposals = 918
Year Gains = 93,924 Year Losses = -76,389 PROFIT = 17,535
Income tax
Let's look at the equal exchange with CGTCalc =False, on an earnings tax foundation. Here we just do a honest suit of the near with the common fee of our previous buys.
SELL of nine FGBS DEC 14 Futures on 06/eleven/2014 at EUR one hundred ten,975 every Net PROFIT of EUR 12 equals GBP 8
Trade information:ID 1550 Code FGBS DEC 14 Date 2014-11-06 05:00:10 Quantity -9 Price a hundred and ten.975 Value in keeping with block 110,975
BUY at average value 110,970 each between 2014-09-01 and 2014-11-06. Total round-trip commission EUR 36, and taxes EUR 0
Trades:
ID 1499q Code FGBS DEC 14 Date 2014-09-01 05:03:forty three Quantity 0.908314 Price a hundred and ten.930 Value consistent with block one hundred ten,930 (Allocated from: ID 1499 Quantity 20)
ID 1500q Code FGBS DEC 14 Date 2014-09-02 02:forty eight:32 Quantity zero.0908314 Price one hundred ten.940 Value consistent with block 110,940 (Allocated from:
ID 1500 Quantity 2)
.... Many trades removed ...
ID 1530a Code FGBS DEC 14 Date 2014-eleven-05 08:00:37 Quantity 0.290323 Price one hundred ten.970 Value in keeping with block one hundred ten,970 (Allocated from: ID 1530 Quantity 1)
ID 1531a Code FGBS DEC 14 Date 2014-eleven-06 02:52:forty Quantity zero.290323 Price a hundred and ten.985 Value in step with block one hundred ten,985 (Allocated from: ID 1531 Quantity 1)
CALCULATION: (9*110975) - 18 - 0 -(9*110970) - 18 - 0 = 12
Here is the once a year summary for profits tax:
Summary for tax 12 months finishing fifth April 2015
Figures in GBP
Gross buying and selling earnings 20,263, Commission paid 755.20, Taxes paid 152.94, Net income 19,355
Not protected: hobby paid, interest received, statistics and other costs, internet connection,...
Hardware, software program, books, subscriptions, office area, Dividend earnings (record seperately)
Note that in case you are incomes profits as a dealer then there are in all likelihood additional costs you could declare against your profits (but I'm not an accountant, so don't take my phrase for it). Taxes on dividends is another kettle of fish, and another phase of your tax return, although happily a highly easy one to fill in.