In a previous post, we showed three ways you can trade the Donchian channel indicator. The methods were all valuable and show how you could use it, but the strategies were incomplete and so I wouldn't put my money into something so deficient in key areas such as risk management.

In this post, I want to show you how you can build a complete trend following model using the Donchian channel and backtest it.

This model is easy to understand and outperforms the S&P 500 in a 22-year backtest.

4 Rules of a Trading System

Any trading system has 4 basic rules:

  1. Instrument Rule: What instruments do you trade?
  2. Entry Rule: When do you enter a trade?
  3. Exit Rule: When do you exit a trade?
  4. Position Rule: How much do you put on a trade?

These can be as simple or complex as you like, but any complete system needs to have answers to these questions.

These rules also must be based on objective data - like price or volume - so that we can automate them and run a backtest (and no, I don't consider most technical analysis to be "objective" in any meaningful sense).

Let's turn to define each of these rules and show how they work together.

What Instruments to Trade?

We’ll be doing all of this with Raposa’s free Strategy Builder which allows us to design and backtest a trading strategy in just a few minutes without any coding required!

The free account gives you access to all of the stocks in the S&P 500, so that’s our selection universe. From 500, we’ll narrow this down to just these 20 stocks:

XOM CVX SHW BRK.B
DD BA UNP JPM
UNH JNJ DUK AMZN
MCD KO PG AAPL
MSFT GOOG AMT SPG

How did we decide on these 20?

Easy, we just grabbed a list of the various sectors in the stock market (there are 11) and got 1-2 of the biggest names from each until we had 20 stocks. We opted for older companies that have been in the S&P 500 for a long time and traded for quite some time - many of these companies are over 100 years old!

Why did we do it this way?

Two reasons.

First, it gives us some reasonable diversification by getting exposure to a variety of sectors.

Second, getting companies that would have been representative a decade or more ago helps reduce lookback bias in our backtest. We could cherry pick stocks we know did great and then run an awesome backtest on them. For example, it’s really easy to have a great strategy that just traded Tesla since it went public. But is that really representative of what the future will look like? Probably not.

Your screen should look something like this:

donchian-channel-instruments.png

Notice that we’re running this from 2000 to maximize our data. This time frame gives us multiple bull and bear markets to see how our model handles a variety of different regimes.

Now that we have our stocks, let’s move on to our Position Management settings.

How Much to Bet on Each Trade?

We have a lot of options for position sizing. You could have an equal allocation to each, so that you have 20 stocks in your system, so each gets 5% of your capital.

I like to use a volatility-based position sizing module. This scales your allocation based on the volatility of the stock, so the higher the volatility, the lower the allocation and vice versa.

I usually use a 252-day lookback period (there are 252 trading days per year, so that’s a 1-year lookback) to get a reasonable sample size of the volatility.

donchian-channel-position-settings.png

Below that, we have our Target Risk. If we set this to 10%, it means that each position will be risk-adjusted so it takes 10% of our portfolio.

Let me give an example so this is clear.

Say we have two stocks, A and B with a 10% Target Risk. If the volatility of stock A is 2 and the volatility of stock B is 5, then we’d allocate 5% of our capital to stock A and 2% to stock B after adjusting for risk.

Allocation A = 10% / 2 = 5%

Allocation B = 10% / 5 = 2%

If the volatility is very low, i.e. less than 1, we could run into a situation where the risk-adjusted volatility is greater than our target risk. Setting the Is Target Risk a Hard Ceiling? option to Yes ensures that we never put money in above our target risk. Setting it to No removes that cap meaning we could take a very large position size for a low-volatility stock.

Our final option here is Position Management. You can use this to rebalance your portfolio if volatility starts to increase or decrease over time. Personally, I prefer to simply leave this off so that positions don’t change until I exit the trade.

When to Enter a Trade?

We’re going to keep our entry rule quite simple and just use a Donchian Channel breakout over 100 days.

This is a classic trend following indicator. I find I get better results with longer term signals (100+ days) but you could easily make it shorter term as well to suit your needs.

donchian-channel-buy-settings.png

The Donchian Channel looks at the high over the past 100 days and creates an envelope around the price. If the price reaches a new 100-day high (e.g. breaks above the channel) then we have a signal that tells our algorithm to buy!

donchian-channel-sell-settings.png

When to Sell?

The last piece for our strategy is the sell signal. Again, in this case, I’m going to keep it simple with a trailing stop loss.

donchian-channel-sell-settings.png

The trailing stop loss will get triggered when your trade reaches a 25% drawdown from its high. This might seem like a lot of give back, but we’re dealing with longer term trends and often times they’ll give back 10% or more before the trend continues and takes off again.

In my experience, stop losses for trend following models between 15-25% typically perform quite well, but you can test different values to see what works best for you!

Ok, let’s see these results!

With all of that in place, you can hit the orange, Run Backtest button and see how it performs.

With this many stocks and 20+ years of data, it may take 2-3 minutes to execute, so just be patient and wait for the results.

When they do come in, however, it looks pretty good!

donchian-channel-backtest-curve.png

Our model beats the S&P 500 over the same time period by about 2X with no leverage needed!

We can look at the stats to get a better feel for its performance:

donchian-channel-backtest-stats.png

It is slow and steady making 118 trades over 22 years for ~5.5 trades per year, just letting those profits run. Personally, I like this because it cuts down on any transaction costs, but it can be boring for some traders and may lead to questions of whether the model is working or not if you go 3-4 months without a signal.

But be patient! The system is continuing along and working for you!

It compounds at 10% per year and turns in a reasonable Sharpe ratio.

With some additional work, I’m sure you could boost these results up a few points and hit 12-15% annualized returns and get a Sharpe ratio > 1.

Also, take a look at the max drawdown. The strategy had a max of 23%, whereas the S&P 500 had multiple 50%+ drawdowns over this same time period!

This slow and steady trend following model looks great and only takes a few minutes to get up and running!

How You Can Get These Returns

If you want to trade this strategy, you could put all of this into Excel and update it every day while waiting for the next signal to hit.

It’s possible but with such a slow model, you’ll likely lose interest after a few months of the tedious work of downloading daily prices for 20 different stocks and putting that data into a spreadsheet and double-checking all of the results.

Additionally, say you want to make a change to your model. How do you test that in Excel?

Unless you’re a whiz with macros and VBA, I don’t know how you could actually run a backtest in Excel to see if your new model parameters improve your results or hurt it. Not to mention the headaches from the long evenings of debugging all of that code, downloading tens of thousands of rows of price data, and keeping that maintained for years.

With a Raposa membership, we make this easy. Just a few clicks and you’re off to the races.

As a member you also get thousands of stocks and ETFs - everything on the US exchanges - to test a wide variety of ideas. Plus, once you’re satisfied with a strategy, you can save your bot and let it run for you. You’ll get a daily trade alert telling you what your bot is doing based on the rules you provided.

On top of that, you’ll get access to our exclusive membership community and our development team that is working to add new signals, markets, and features according to your requests!

Raposa puts you in control so you can get all of the great benefits of algorithmic trend following to manage your portfolio.