Code Snippet: Forex Commission Scheme

The code snippet in this post contains a forex commission scheme designed for use in Backtrader. You may or may not be aware but most forex brokers hide their commissions inside the spread. This can cause inaccurate backtest results due to the fact that Backtrader does not track the bid / ask spread. The code in this post attempts to take a first step towards rectifying this (with some limitations).

Forex Commission Scheme Code

Code Commentary

First of all, the code attempts to be flexible enough so that it supports Japanese yen pairs. If you are trading a JPY pair, you must flag it as a parameter when the scheme is initialized. For those of you who are not aware, JPY pairs require a different calculation due to the fact the price quotes only go to two decimal places instead of the usual 4.

On the other hand, the scheme does have some limitations. The first is that the account currency must be the same as either the base currency or the counter currency. It does not support the account being held in a 3rd currency (For example a GBP account trading USDJPY). If it did support a 3rd currency, the latest exchange rate for the account would be needed for the calculation. The second limitation is that scheme assumes a fixed spread. With that in mind, if you have a strategy that operates around news events (when spreads tend to widen) then this scheme may not be for you.

As a final note, the scheme applies half the commission when buying and half when selling. Alternatively one could apply the full scheme on only one direction (i.e the buy or the sell side).

Using the forex commission scheme

To use the commission scheme, you can load it into cerebro with the following lines of code

Note that the defaults for the scheme assume the account currency is the counter currency. If your account currency is the same as the base currency, you will need to initialize it like this:

The Result

To test and check the resulting commission, I suggest using a simple example such as:

  • Using a USD denominated account
  • Trading a pair where the USD is a counter currency (e.g GBPUSD)
  • Only performing one trade
  • Opens a position size of 10,000, so that 1 pip = $1

In addition to this, I also recommend  including a method in your strategy that allows you to print the profit / loss of the trade. Below is a super simple strategy that does this. It will open a position on the first bar (candle) and close the position on the 10th bar.

Running the above script should result in some output that is similar to the image below. Naturally the actual profit and loss will change depending on the data that you are using. Just to be clear, I ran the script with a spread of 10 pips and set the account currency to be the counter currency.

The result from applying the forex commission scheme

Wait…but my broker does not charge a commission….

As mentioned earlier, the brokers commissions are hidden in the spread. This is why they love retail traders who operate on a lower timeframe. More trades placed == more commissions because you have to jump over their spread every time you put a trade on.

If you  are still not convinced, think of it this way. In Backtrader we are generally using a mid price. When we close a position we also close it at the mid price and calculate the profit and loss between those two mid price levels. However, in the real world, your trade would have x pips less profit because of the bid and ask spread. In other words, if you were going long, you would have bought at a slightly higher price and sold at a slightly lower price. This scheme will correct the profit and loss for that difference. Hopefully you will find it to give more accurate backtest results.

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.