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.
import backtrader as bt
This commission scheme uses a fixed commission and stamp duty for share
purchases. Share sales are subject only to the fixed commission.
The scheme is intended for trading UK equities on the main market.
params = (
def _getcommission(self, size, price, pseudoexec):
If size is greater than 0, this indicates a long / buying of shares.
If size is less than 0, it idicates a short / selling of shares.
if size > 0:
return self.p.commission + (size * price * self.p.stamp_duty)
elif size < 0:
return 0 #just in case for some reason the size is 0.
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:
#Create an instance of cerebro
cerebro = bt.Cerebro()
#Add the new commissions scheme
comminfo = stampDutyCommissionScheme()
That is all there is to it!
Find This Post Useful?
If this post saved you time and effort, please consider support the site! There are many ways to support us and some won’t even cost you a penny.