NEW+--
Min 75% OFF | Pay Day Sale Extended till 3rd June
Move Left Complete list

How to Avoid Common Mistakes in Backtesting?

Backtesting is a critical step in developing a trading strategy. It involves simulating the performance of a strategy using historical data to gauge its potential profitability. However, many traders make mistakes during backtesting that can lead to unrealistic expectations and poor real-world performance.

We have already covered the backtesting process for a trading strategy in the preceeding post. In case you have missed it, you can go through our article on How to backtest a trading strategy?

In this post, we are going to focus on the often-overlooked aspects of transaction costs and slippage in backtesting trading strategies that lead to incorrect expectations and sub-optimal results in live trading.

Note: The links in this tutorial will be accessible only after logging into quantra.quantinsti.com

 

The topics covered in the post are:

  1. Why is incorporating transaction costs and slippage important?
  2. What exactly constitutes transaction costs?
  3. How can you incorporate transaction costs into backtesting?
  4. Why does slippage occur?
  5. How can you model slippage in backtesting?
  6. What are the best practices to minimise slippage?

 

After going through this post, we recommend you check out the practical implementation of transaction costs and slippages in Python. If you are looking for using AI for Trading, Options Backtesting, Quant Trading Strategies, Portfolio Management, our All Courses Bundle is a perfect solution.

 


 

Why is incorporating transaction costs and slippage important?

 

When working with low-frequency trading strategies that involve trading liquid financial markets assets less than once a month, transaction costs and slippage are generally minimal. However, when assessing the profitability of intraday trading strategies, it's crucial to thoroughly analyse the impact of commissions and slippage on the trading edge. Estimating commissions is relatively simple since most brokers clearly outline the cost per share for each transaction. The more challenging aspect is estimating the slippage incurred with each trade. 

Let’s understand this with a scenario:

John, a trader from Brooklyn, has created and backtested an intraday  trading strategy that shows a $1,000 profit on a $1000 capital over a month in the backtest. He thinks he has found a money making machine and deploys the strategy in the live market. 

But he is astonished to see that his profitable strategy is losing money. He is wondering what could be the reason for that. Could you help John?

John did not account for transaction costs in his backtest. 

He incurred roughly $3 as transaction cost on each trade. He analysed his backtest and found that he did 500 trades. he total transaction cost for 500 trades is 500 × $3 = $1,500. Instead of a $1,000 profit, John actually had a $500 loss. 

Net profit = Total Profit - Transaction Cost

                = $1000 - $1500

                = -$500

 

Would John had deployed this strategy in live markets? Definitely not.

John understands his mistake and modifies his strategy. He backtests based on modified entry and exit rules and also incorporates transaction cost. He is able to generate a decent profit. He proceeds to live trading. 

John is trading in cryptocurrency. He places an order to buy 10,000 units of the crypto. The last traded price (LTP) is $100. However, due to the large order size and low liquidity in the asset, the average execution price ends up being $103 per unit. This results in a 3% higher cost and significantly reduces profits. Instead of using $1,000,000, he ends up spending $1,030,000, incurring additional $30,000 due to slippage.

This additional cost of $30,000 due to slippage was not considered in the backtest.

This scenario is not unique to John; it happens to many traders who fail to consider the nuances of transaction costs and slippage. 

Let's start by understanding transaction costs, and then we will talk about slippage and how we can model them in the later part of the post.

 


 

What exactly constitutes transaction costs?

 

Transaction costs are basically the expenses associated with executing trades. These costs can vary significantly depending on the trading exchange, instrument, and volume and directly affect the net profitability of trades. Some key components of transaction costs include:

Brokerage Commissions: These are fees paid to brokers for executing trades on your behalf. These commissions can be structured in various ways:

  • Fixed per Trade: Some brokers charge a fixed fee for each trade, regardless of the trade size. For example, a broker might charge $5 per trade. 
  • Percentage of Trade Value: Some brokers might charge a commission based on a percentage of the trade value. For instance, a broker might charge 0.1% of the trade value. For example, if you buy 100 shares of a stock priced at $50 per share, the trade value is $5,000. At a 0.1% commission rate, the brokerage fee would be: $5,000 × 0.001 = $5
  • Tiered Pricing: Some brokers offer tiered pricing based on transactional value, where the commission rate decreases as the total transactional value increases. For example, a broker might charge a commission rate of 0.5% for the first $10,000 of transactional value in a month and 0.4% for any transactional value beyond that.

 

Spread Costs: It is the difference between the bid price (the price at which buyers are willing to buy) and the ask price (the price at which sellers are willing to sell). The spread represents an implicit transaction cost that traders incur when entering and exiting positions. The magnitude of the spread can vary based on market liquidity and the specific asset being traded:

  • Tight Spreads: Highly liquid markets, such as major forex pairs or blue-chip stocks, typically have tight spreads, sometimes as low as a fraction of a cent. These tight spreads are favourable for traders as they minimise the cost of entering and exiting positions. For example, if the bid price of a stock is $50.00 and the ask price is $50.01, the spread is $0.01.
  • Wide Spreads: Less liquid markets, such as small-cap stocks or exotic forex pairs, often have wider spreads. Wider spreads increase the cost of trading and can significantly impact the profitability of frequent trading strategies. For example, if the bid price of a stock is $50.00 and the ask price is $55, the spread is $5.

 

Regulatory Fees: These are costs imposed by regulatory bodies and exchanges to fund their operations and ensure market integrity. These fees can take various forms, including transaction taxes, exchange fees, regulatory charges, etc. These costs can vary across exchanges and might be updated regularly. They need to be checked regularly on the exchange’s website and incorporated while backtesting. 

 


 

How can you incorporate transaction costs into backtesting?

 

To incorporate transaction costs into backtesting, you should:

1. Adjust Trade Prices: When a trade is executed, the actual price paid or received may differ from the mid-market price due to the bid-ask spread. You need to have bid-ask price data to account for this:

  • Buying at the Ask Price: When entering a long position, use the ask price instead of the bid or midpoint price. Example: If the bid price is $50.00 and, the ask price is $50.05, and you plan to buy 100 shares, the entry price should be $50.05 per share.
  • Selling at the Bid Price: When exiting a long position or entering a short position, use the bid price. Example: If the bid price is $50.00 and, the ask price is $50.05, and you plan to sell 100 shares, the exit price should be $50.00 per share.

 

2. Commission Deductions: Brokerage commissions can be modelled as either fixed costs per trade or as a percentage of the trade value. To include these costs:

  • Fixed Commissions: Deduct a fixed amount from the PnL for each trade. Example: If the broker charges $5 per trade, deduct $5 from the profit or loss of each buy and sell transaction.
  • Percentage-Based Commissions: Deduct a percentage of the trade value from the net PnL. Example: For a trade value of $5,000 with a 0.1% commission rate, the commission would be $5,000 × 0.001 = $5.

 

3. Fee Integration: Regulatory fees, such as transaction taxes and exchange fees, should be accounted for in your cost model:

  • Transaction Taxes: Calculate the tax based on the trade value and deduct it from the PnL. Example: For a $5,000 sell transaction with a 0.005% exchange fee, the fee would be $5,000 × 0.005% = $0.25.
  • Exchange Fees: Calculate the fee based on the number of shares traded or the trade value and deduct it from the PnL. Example: For a trade of 1,000 shares with an exchange fee of $0.003 per share, the fee would be 1,000 × $0.003 = $3.

 

Another important factor to consider while backtesting is the slippage that might occur while live trading.

 


 

Why does slippage occur?

 

Slippage occurs when there is a discrepancy between the expected price of a trade and the actual price at which the trade is executed. There are two main reasons for the occurrence of slippage:

 

Market Volatility: Rapid price changes that occur between the time an order is placed and when it is executed. This can occur for several reasons:

  • News and Events: Unexpected news releases, earnings reports, economic data, or geopolitical events can lead to sharp price movements. 
  • Market Orders: Using market orders, which are executed at the best available price, can lead to slippage in volatile markets. If the price moves rapidly, the order may be filled at a less favourable price than expected. For example, If you place a market order to buy a stock at $50.00, but a sudden surge in demand pushes the price to $50.10 by the time the order is executed, you experience a $0.10 slippage per share

 

Low Liquidity: A lack of sufficient buyers or sellers at the expected price level can lead to execution at less favourable prices. The following factors can contribute to low liquidity:

  • Lesser Traded Assets: Some assets, such as small-cap stocks, exotic forex pairs, or certain commodities, naturally have lower trading volumes. This means fewer orders are available at any given price level, increasing the likelihood of slippage. Example: If you place a buy order for a lesser traded stock at $10.00, but there are only a few sellers, you might end up buying at $10.05 due to insufficient sell orders at $10.00.
  • Large Orders: Placing large orders relative to the average trading volume can move the market price. When an order is too large to be filled at a single price level, it must be executed across multiple price levels, resulting in slippage. Example: If you place a large buy order at $20.00 for 10,000 shares, but only 5,000 shares are available at $20.00, the remaining shares might be filled at higher prices, leading to slippage.

 


 

How can you model slippage in backtesting?

 

To model slippage, you would want to create realistic scenarios for trade execution under different market conditions. This can be done using the following methods:

  • Percentage-Based Slippage: Apply a fixed percentage to the trade price. For example, if historical analysis shows an average slippage of 0.1%, adjust the execution price by this percentage. For a trade expected to execute at $50, the adjusted price is: 50 × (1 + 0.001) = $50.05
  • Dynamic Slippage Models: Use volatility and liquidity metrics to adjust slippage dynamically. For example, during periods of high volatility, if slippage increases to 0.2%, for a trade expected at $50, the adjusted price is: 50 × (1 + 0.002) = $50.10
  • Order Book Simulation: Simulate the order book to estimate the impact of large orders on price. This involves modelling how your order would move through the market depth and potentially affect prices. However, this requires the order book data and is only relevant if you have a significantly large order size.

 


 

What are the best practices to minimise slippage?

 

While slippage cannot be entirely eliminated, several strategies can help minimise its impact:

  • Using Limit Orders: Instead of market orders, you can use limit orders to control the execution price. This ensures that the trade is only executed at or better than the specified price.
  • Trading During High Liquidity Periods: Execute trades during times of high market activity to benefit from tighter spreads and higher liquidity.
  • Breaking Large Orders: Enter and exit positions in smaller increments to avoid significant market impact. This is particularly important for large orders in less liquid markets.

 


 

Wrapping Up

 

Now that you have an idea about the various transaction costs and slippage and the importance of incorporating them in backtesting, you're ready to take the next step. The Backtesting Trading Strategies course covers the incorporation of transaction costs and slippage using Python. You can refer to Section 9, Unit 4, to get the code for the implementation. All you have to do is take a free preview of this course. You can do this by clicking on the green-coloured Free Preview button on the right corner of the screen next to the FAQs tab and going to Section 9, Unit 4 of the course. If you want to learn about different quant and algo trading strategies, then you can explore All Courses Bundle.

 


 

Additional Readings

 

1. Optimizing Order Types in Indian Futures Market: This paper discusses a model designed to predict bid-ask crossover events to reduce slippage costs during order execution. 

Source: SSRN

 

2. Beat the Market: An Effective Intraday Momentum Strategy for S&P500 ETF (SPY): This paper investigates the profitability of an intraday momentum strategy applied to SPY and the impact of commissions and slippage on the overall profitability of the strategy.

Source: SSRN


 

IMPORTANT DISCLAIMER: This post is for educational purposes only and is not a solicitation or recommendation to buy or sell any securities. Investing in financial markets involves risks and you should seek the advice of a licensed financial advisor before making any investment decisions. Your investment decisions are solely your responsibility. The information provided is based on publicly available data and our own analysis, and we do not guarantee its accuracy or completeness. By no means is this communication sent as the licensed equity analysts or financial advisors and it should not be construed as professional advice or a recommendation to buy or sell any securities or any other kind of asset.

RELATED KEYWORDS