The Journey of a Quantitative Trading Strategy Development (Part 2)

A no-code overview

Diego Pesco Alcalde
6 min readJun 4, 2023
Source: Freepik.com

This article is the part 2 of a series of articles on quant trading. My goal is to present the steps of a quant strategy development in a simple and non-programmatic way. The scripts and frameworks developed to this series are on my Github.

Disclaimer: This article was developed for informational purposes only, it is not a trading recommendation.

Disclaimer 2: All charts presented in this article were developed by myself.

Backtesting

Testing hypothesis is the core of quant trading. Only by testing traders will be able to verify if and to which conditions their assumptions hold true. They are able to measure the effectiveness of their strategies, and will develop new ideas based on their test results. On the other hand, this is the most complex and detailed part of a quant trading project. Testing requires good and clean data, and well written code. If a testing algorithm is not able to simulate an agent trading in the market the conclusions of a study are useless. It can return bad results for good strategies or even worse, good results for bad strategies, which will cost traders money. Of course, no algorithm is able to reproduce the exact effects of an agent in the market, so it’s important to define its limitations and assumptions before accepting the results that come from it. Backtesting is the testing of a strategy on historical data. In another words, we will analyze what would be the impact in our portfolio if a strategy was applied on the past months or years, and compare it with our market benchmark. That being said, backtesting scripts can be as complex as you want it to be, but the fundamental idea is to start simple. Vectorized backtesting algorithms are easier to write and good enough to analyze many simple strategies. On the other hand, iterative backtesting frameworks can be much more representative of real trading, and allow the backtesting of more complex strategies, but are hard to develop.

In part 1 of this article we stated our hyphotesis and calculated three moving averages to flag when a price trend is starting, so we can enter in a position and profit from it. Now I developed a vectorized backtesting algorithm, which will adjust the portfolio position based on the moving averages values on each step of time.

Let’s check how the strategy performs in the backtest. Everytime the moving averages cross each other the algorithm buys or sells, and the positions can be seen by the yellow line in the chart below. A long position is represented by 1, a short position by -1 and a neutral one is 0.

As we can see there are many trades in the backtest. Long positions will accumulate profits when price moves up, and short positions when it moves down. Neutral positions keep the portfolio balance as it is regardless of price changes. Since we have the portfolio position and price changes for every hour of the backtest, we can calculate the Profit and Losses, or “PnL”, and plot them in a chart.

It’s awesome. We found a strategy that in a period of 56 months increased the value of our initial investment by a factor of 17. The calculated Compound Annual Growth Rate is 0.85, which means that we are almost doubling the portfolio value every year. Let’s put the money in and enjoy life. It’s a silly joke, but once you spend a lot of time coding it’s easy to forget discipline and start betting your money on unfinished business. After all, this result is good news, it means that in a perfect world we would beat the market by far, but we still have a long journey and reality is right around the corner waiting for us.

Hidden Costs

Hidden costs are losses that come with trading operations, and in some cases are neglected or mismodeled. Let’s talk about them.

If we are backtesting to actually trade later in the market, it means that we will need a broker platform to execute our trades. And what do brokers love? You’re right, commissions. Trading fees are a significant component when backtesting an idea, especially when we’re talking about mid to high-frequency strategies, and I will show you why.

Fee rates on Sep 16th, 2022. Source: Binance

From the image above, we can see that Binance charges a regular user a fee of 0.1% for each order placed in the platform. With that information we have to recalculate the PnL results subtracting 0.1% everytime that a trade is performed on the backtest. For our study this is how the resulting chart looks.

It is definitely frustrating to see more than half of the profits vanishing just by considering that small cost per order. If you’re considering trading in a platform that charges no fees, understand that nothing comes for free, so please balance other aspects such as liquidity, robustness and safety.

But even if you are not paying trading commissions, there is a cost that you cannot avoid, and it is the Half Spread costs. Spread is the name given to the difference between the best Bid price and the best Ask price in the offers book. Our backtest algorithm is using hourly close prices to define the portfolio position, but in reality the trade that comes after will be subject to the prices available in the book, so the cost is always a half spread, i.e., the distance between the average price to the next best offer.

Order Book with Bid-Ask prices and Spread. Source: Binance

Another hidden cost that you will also face during trading is called slippage. Slippage is a market liquidity effect that shifts the trade average price from the intended. It happens for example when a market order requires several limit orders from the book to be fully attended, which will “slip” the average execution price from the best price of the book. Another example of slippage is the price shift from when the order is placed on your interface to when it is actually executed in the broker’s server.

Modelling Half Spread and Slippage costs is complex, since liquidity changes through time which directly impacts those costs. We are targetting a mid to low frequency strategy, so for the sake of simplicity we will not take those into consideration in our study for now, and if needed we will come back to them in the future.

Thank you for taking the time to read this article. In part 3 of this series we will continue our journey in crypto quant trading, talking about portfolio metrics! Feel free to contact me on LinkedIn for any questions or suggetions.

--

--