Anthony Garner

Anthony Garner
Lawyer, financial market analyst.

HCA Trend Following Algorithm

Trend Following

The HCA Trend Following Algorithm allows you to explore market timing systems, using a simple momentum calculation over a user defined look-back period.

Trends are a recognized and well-known factor in markets. If an instrument is in an uptrend, it may be driven further as investors are drawn to a stock and follow its upwards momentum. The reverse may be true of an instrument in a downtrend, but the HCA Trend Following Algorithm does not trade short. Short trends are remarkably difficult to catch in the stock market –they tend to have happened by the time your signal gets you short.

Trend following works well when strong, long-lasting trends are seen in markets. A trend is established, a given trend following system gives a signal to enter and a position is held until the trend reverses. At which time a signal is given to exit. The worst markets for a trend following system are choppy, trendless markets where positions are entered and exited with continual losses.

A trend following system may typically record a low percentage of winning trades (perhaps 40% winners / 60% losers) but winning positions are run while losing trades are cut short.  Thus the system may be profitable overall.

Trend following has been around for as long as markets have existed. In a sense, the modern stock index is based on the principle of trend following. Stocks increasing in value are included in the index as they surpass a given threshold– so you catch the rising star. When the sun inevitably sets on a company, its market capitalization declines, and it is excluded from the index. Bearing in mind that the great majority of listed investments become worthless over time, the stock index has provided a reliable gauge of healthy companies and an extremely good way to invest.

Much has been made of trend following over the past few decades. It was and is the staple fare of the CTAs – Commodity Trading Advisors, who trade everything from metals and soft commodities to bonds, currencies and stocks using the futures market.

Perhaps the best-known example of trend following in recent decades has been the so-called Turtles.  Richard Dennis and William Eckhardt were successful commodities speculators, who had profited hugely from following trends in the inflation driven commodities markets of the 1970s.  Dennis believed successful trading could be taught and following a bet otherwise from Eckhardt,he trained a group of people with no financial background to trade commodities using a simple trend following system – going long when the trend was up and short when the trend was down. Apparently, the experiment was highly successful,and a number of the group went on to establish their own commodity trading firms, managing considerable outside capital.



HCA allows a user to select a portfolio from listed US stocks, mutual funds, and ETFs. Clearly this choice will be a key determinant of performance and the primary use of this system is to back test or trade a portfolio of funds, rather than individual stocks.  Other HCA Systems are intended to be used for stocks, where the most important consideration is to avoid look back and selection bias.  This can be achieved by using a universe of listed and de-listed stock covering some section of the market or the overall market. An example would be a rolling portfolio, to include the 500 largest stocks in terms of market capitalization at any given date.

Top X out of Y Stocks

If the portfolio contains a total of 10 (or any number) of stocks, the user can experiment by allocating to some smaller number of stocks on the re-balance date, in the hope of rotating into winning positions.  And thus, aiming to achieve a higher absolute return than a given benchmark.

Re-balance Dates

A choice of re-balance periods is provided. Users can choose to re-balance their portfolio daily, weekly, monthly, quarterly, or annually.


The algorithm’s definition of trend is a simple one: if the look-back period chosen is 60 days (by way of example) then the calculation is:

(yesterday’s closing price / the close 60 days ago)-1.

In other words, simple percentage change. If the percentage change is greater than a user defined variable, the instrument is assumed to be in an uptrend.

The parameter choice is therefore the look-back period over which the presence or absence of a possible trend is identified.


Users can define what percentage change over the look back period defines a trend and causes an entry at the appropriate re-balance date.  If they choose to set this variable at 0, then any stock which has been greater than flat over the look-back period will qualify for investment. Equally, a user could set this at 0.50 – in which case the stock would need to have risen by over 50% to be included in the portfolio at the next re-balance data. By setting the figure at -1, a stock will be included regardless as to how it has performed over the look back period.

Reserve Asset

It is usual in stock market trend following systems to provide a reserve asset, usually cash or bonds, in which the portfolio or some part of it will be invested during times of equity market turmoil. To give an example, take a portfolio consisting solely of SPY, an index tracking ETF for the S&P 500. You could provide (by way of example) to assess the trend of the market monthly. If the market has risen (if momentum is greater than zero) over the look-back period of 60 days (by way of example), the portfolio will consist solely of SPY. If the market has been flat or declined, the portfolio will consist solely of the reserve asset – perhaps cash or perhaps a bond ETF such as TLT, the iShares Barclays 20+ Yr Treasury Bond ETF.  Or you might choose a selection of 11 ETFs representing the sectors of the US stock market and decide to rotate into the 6 best performing sectors on each re-balance date.  If one of more of those 6 sectors has been flat or worse for the look-back period, its place in the portfolio will be taken by the reserve asset.

Does Market Timing Work?

This is not a question we, nor we suspect, anyone else can answer. The decision rests with the investor who must exercise his own judgement. The major problem with back testing such systems is that with even with a comparatively small number of parameters to choose from, a combinatorial explosion is inevitable.

A less seasoned practitioner will find himself testing many different portfolios. Coupled with many different look-back dates, many different definitions of trend, and many different re-balancing periods. Even the most un-mathematical of minds will readily understand that the number of back tests conducted could run into many thousands, or indeed hundreds of thousands.

Such a procedure is dangerous in the extreme; while profitable combinations will undoubtedly be discovered based on past data, the parameters chosen may well fail to live up to their promise in the future.

Important decisions should be made based on fundamental factors before any back testing begins.  A portfolio should be chosen based not on what appears to “work” best based on past data, but on what sort of portfolio provides sensible balance and diversity.  A look-back period should make sense to the investor – not too short (which will provoke too many pointless trades) and not too long (which may make the parameter insensitive to market conditions). The same consideration goes for periodic re-balancing – daily re-balancing may prove excessive for such a system while annual re-balancing may destroy the ability to react to changing market conditions.

Whatever the portfolio chosen and whatever parameters are considered appropriate, the past will not replicate the future in any exact sense. A system which enabled a switch from volatile stocks to safe short-term bonds in 1929 may fail to achieve the same purpose in 1987. Or 2099.  There are no absolutes in stock markets except the fact of uncertainty. And of course, even the buy and hold investor must live with that.

Sample Back Test

We have chosen a single back test to demonstrate the system. We have no idea how such a portfolio traded on such parameters will behave in the future. All we can say is that such a system has been touted by many stock market pundits in the past, based on back testing.

The idea behind the system is to invest in risk assets (stocks) when markets are performing well and to retreat into bonds when stocks are falling in value.

The back test below has a single instrument in its portfolio – SPY, the S&P 500 index tracking ETF. At the beginning of each month, the momentum for SPY is calculated for the preceding 60 days. If the momentum is above 0%, the portfolio for the following month will consist solely of SPY. If the momentum is less than or equal to zero, the portfolio will be invested wholly in the reserve asset, in this case TLT, the iShares Barclays 20+ Yr Treasury Bond ETF.

As can be seen, in back testing at least, this system has outperformed the S&P 500 both in absolute terms and relative to volatility.  The absolute return has been considerably greater than the market index while the volatility has been similar to that of the index.

As for peak to valley draw-down, the system suffered a mere 19% drop (thanks to a timely retreat to bonds) as opposed to a collapse of some 55% for the index itself.

Quo vadis?

As they say in Ecclesiastical Latin. Whither goest thou?

View our videos
Sign up for an invite to our live streaming.

Learn about algorithmic trading and strategies, through
zipline backed research, backtest, and live trading.
When live streamng, ask questions...get!

Thank you! Your submission has been received!

Oops! Something went wrong while submitting the form