Code Snippet: Stamp Duty Commission Scheme

If you are based in the UK or just have an interest in trading UK equities, one thing that you need to be aware of is the UK stamp duty when buying stocks and shares:

An excerpt from https://www.gov.uk/guidance/stamp-duty-reserve-tax-the-basic

You pay SDRT on paperless transactions when you buy:

  • shares in a UK company
  • shares in a foreign company with a share register in the UK
  • an option to buy shares
  • rights arising from shares already owned
  • an interest in shares, like an interest in the money made from selling them

The tax you need to pay is 0.5% on any equity on listed on the main market. Note that you do not have to pay stamp duty on AIM listed stocks at the time of writing.

Adding the stamp duty commission scheme to backtrader

Defining our own commission schemes in backtrader is possible and defined in the documentation. What makes stamp duty a little unique is that we need to apply the extra tax to purchases and not sales.

The code

As you will see in the code, we just need to simply check whether the size of the trade is above or below 0 in order to tell whether transaction is long or short (buy or a sell). Once we have this information we can alter the return value accordingly.

The stamp duty value and fixed transaction fee apply to my broker in the UK. That might not be the case for you, therefore they have been added as parameters so the default value can be changed when calling the class.

To add it to your script (using the default settings) simply do the following:

That is all there is to it!