Developing A Robust ETF Trading Algorithm

Nov. 01, 2013 11:01 AM ETSPY, SH, QQQ, IWM, RWM, TLT, GLD
Please Note: Blog posts are not selected, edited or screened by Seeking Alpha editors.

Long/Short Equity, ETF investing, Short-Term Horizon

Contributor Since 2013

CTO, FLHP Trading Systems, LLC Aerospace and Computational Engineer with 28 years experience in Engineering and Scientific Modeling and Programming including large scale vector and parallel processing. Work includes Finite Element Modeling in Structural Analysis, Magnetic Modeling, and Fluid Dynamics codes primarily for the Aerospace community. 30+ years of stock market and mutual fund investing, 8+ years of ETF and options trading.

The investing world is rife with advice; buy low, sell high, buy on dips, buy when the VIX is low, invest with a long-term horizon, and so on. There's a plethora of gurus, pundits, analysts, technicians, fundamentalists, and so on vying for your attention and telling you what to buy (and precious little about what to sell or when to sell) and how to navigate the stock market. My journey through this noise has been guided by Earl Nightingale's famous quote: "Look at what the majority of people are doing, and do the exact opposite, and you'll probably never go wrong for as long as you live" . So the starting point for building a market timing model is simply the old adage "You can't time the market" and taking the exact opposite tack.

Let's start with a simple model or two and build from there. There are now a number of indexes which have well correlated ETFs that enable a trader to trade a basket of stocks like the S&P500 using (SPY). Others of interest include the Russell 2000 (IWM), NASDAQ 100 (QQQ), and DJIA (DIA). These ETFs (and their option chains) are heavily traded and highly liquid; often the bid/ask spreads on the ETFs during trading hours are 0.01 so there's very little cost in trading these other than commission costs; i.e., there's little slippage. More importantly, there are readily available and fairly liquid inverse ETFs available to trade for each of these index ETFs, SH (-S&P500), RWM (-Russell 2000), QQD (-NASDAQ 100), and DOG (-DJIA). That's key to implementing and trading a successful bimodal model; you need an easily traded vehicle to track the model when the model dictates that you should be short the index or market. So the first question is: how accurate are the inverse ETFs?

Graph 1

Graph 1 is a graph of the daily and cumulative percentage differences between SPY and SH daily from the inception of the SH ETF on June 21, 2006. If the inverse ETF (SH) was perfectly accurate in tracking the inverse of the S&P500 the graph would be a straight line at zero. Up until the end of 2008 this was a great inverse ETF, it actually paid a dividend! However from January 2009 to September 2013 the ETF has slipped a little over 14% in 57 months or approximately 0.75% per quarter about 2/3 of which can be accounted for by the dividend. Not unlike actually shorting a dividend paying stock it's not surprising to see a debit for the amount of the quarterly dividend. Accounting for the dividend the actual slippage rate for SH is on the order of 1% per year, which is acceptably small.

Performance Graphs

First, a few notes on performance graphs. I've seen many performance graphs across a host of financial websites that use compounding. Compounding is everyone's free lunch; and if you're careful to cherry pick a starting point even mediocre ETFs and mutual funds can look enticingly good when presented in that fashion (return on a $10,000 investment over 5 years is a favorite choice). What I've found that shows a much truer picture of an ETFs or a model's performance is to present it using an uncompounded data chart. In addition, a true comparison of a bimodal or switching algorithm (i.e., a model that swings from long to short) for an ETF like SPY which also pays a dividend is to compare the performance against "Adjusted" Values of the ETF that account for the dividend. The model performance results shown do not account for any potential dividends should you be holding the ETF long through the ex-dividend date; later on we'll look at how often a model is long on ex-dividend days.

A Simple Model First

So a very simple model might be: "go long the S&P 500 (i.e., buy shares of SPY) when the price is greater than the 200-day Simple Moving Average (SMA) and go short the S&P 500 (i.e., buy shares of SH) when the price is less than the 200-day SMA". We are either 100% long or 100% short, we are never in cash. So the first question that we can easily model and answer is "what values other than 200 for a SMA might generate a higher overall yield?" The answer is, interestingly, nowhere near 200 but rather 8.

Graph 2

Graph 2 details the gain, in SPY points, from the inception of the SPY ETF in 1993 through September 2013 for a range of SMA values using this very simple approach. So right off the bat we've got a very simple model that potentially outperforms SPY by a factor of 1.7:1 by simply buying SPY when SPY's price is above its 8-day SMA and buying SH when the price of SPY is below its 8-day SMA. Position adjustments (if needed) are made daily at the close; if the closing price of SPY is greater than its 8-day SMA then you either go long or stay long, and if the closing price of SPY is less than its 8-day SMA you either go short or stay short at the close. This can be done automatically with most brokers using a conditional MOC (Market on Close) order.

Graph 3

Graph 3 details the performance of a Simple 8-day SMA Model from the inception of the SPY ETF in 1993 through September, 2013 where the light blue line is the Adjusted Value of SPY including dividends. This is a synthetic model using -SPY when the model is short but realizable results using SH would be very close since we've already established that SH tracks SPY within 1% / year. Overall Gain is 1.7:1.

A Complex Model

Over the last year we've developed a far more sophisticated complex model that can be optimized for almost any index ETF, commodity, bond or bond fund, and even individual stocks as long as 10-20 years of price data is available. The model is not terribly complex internally however the optimization of the model is very CPU-intensive; for the SPY model over 2.5 billion iterations were run to ensure that the solution was optimal. At a high level the best way to describe how this model behaves is that day by day it determines the relative strengths of two competing phenomenon based on a set of inputs; one is a medium term trend and the second is a short term reversion to mean. As those two competing phenomenon play out the model finds, day by day, a 50-50 point (or pivot point) where the model calculates that the underlying index or ETF has a 50-50 chance of going up or down. For most equity indexes the reversion to mean component is quite strong, so generally if the model is currently long the pivot point to switch to short is above the current price. For Gold (GLD) and some other commodities the reversion to mean component is very weak, and so the pivot point to switch to short when the model is long will often be lower than the current price.

How Much Price Data is Required?

So what elements constitute a robust market or ETF timing algorithm? First let's look at the price data that is a key input to the model. How many years of data is needed to be confident that the algorithm accurately models the underlying behavior of the market or ETF being modeled?

Graph 4

Graph 4 details the results of building and optimizing a sophisticated complex SPY model using N years of data, beginning with 3 years of data (1993 - 1995) and then using that N year data model going forward to September, 2013 without updating. Even with 3 years of data the model outperforms the SPY ETF by a 2.0:1 factor or 53% of the current 21 year price data full complex model gain. At 10 years the model outperforms the SPY ETF by a 3.1:1 factor or 80% of the current 21 year data model, and at 15 years the model reaches 100% of the current 21 year complex model's performance at 3.73:1.

Model Bias

The complex SPY model tends to have a bit of a bearish bias which causes it to underperform in bull markets. The reason for this is simple - the model is told to optimize for the maximum percent gain over 20 years. The model is "looking" for movement, either up or down, to realize gains, and equally "weights" every day's movement. So when does the market move?

Graph 5

Graph 5 details the total up and down movement in SPY year by year broken out by daily movement (open to close) and the gap at the opening bell (close to next day's open). The three largest total movement years are 2000, 2002, and 2008 so the optimization process tends to overweight those years to achieve the maximum overall gain. Since all three are bearish years the model tends to exhibit a small bearish bias.

Closing Price vs Opening Price

One of the basic constraints in developing the model was no day trading so essentially we have two distinct times to trade - either at the Open or at the Close. Initially a study was done to determine whether a Close - Close algorithm or an Open - Open algorithm produced the best results. Further study of those two different sets of prices yielded a startling result; for SPY, DIA, and GLD (and presumably others) the bulk of the overall gain in the ETF can be harvested by simply buying the close and selling the following day's open.

Graph 6

Graph 6 shows the gain for SPY from 1993-2013 using two different strategies; buy the close and sell the following day's open (Green -Gap) versus buying the open and selling the close within the same day (Red - Daily). Amazingly, applying that simple trading strategy for SPY results in an average 9.61% annual gain for the 1993-2013 period when trading Close to Open. Trading Open to Close results in an average -0.44% loss for the same period. Interestingly, the same phenomenon occurs across a broad range of indexes and asset classes. Of course, trading in this fashion would generate a large number of trades (500) per year so commission costs would be a major factor.

Graph 7

Graph 7 shows a similar result for the Gold ETF although the daily Open to Close gain is slightly positive overall.

Full Complex Model Performance

We've developed and tested fully complex models for several popular equity index ETFs, Gold, and US Treasury Bonds. Together they can be combined to add diversification.

Graph 8

Graph 9

Graph 10

Number of Transitions (Trades)

An important element in any trading model is how often it trades. While commission costs have continued to decline (today you can find brokers with $0.005/ share commissions) it is important to understand the number of commissions and commission costs a given model will incur.

Graph 11

Graph 11 details the number of trades required to follow the complex SPY model year by year. The average number of trades/year is 81, and comparing this to Graph 5 the model tends to generate larger numbers of trades when the overall volatility is high as expected.


For the SPY model dividends are important to note. For the time span of the model (1994-2013) the SPY ETF paid 80 quarterly dividends. Of those dates, the complex SPY model was long 46 times and short 34 times resulting in a 57% chance that the model will be positioned long on the ex-dividend date and collect the dividend.

Win Rate

The current complex SPY model has an overall win rate of 53.42%. Furthermore, the average gain on a winning day is 0.89% while the average loss on a losing day is -0.79%. Zero gain days are counted as winning days. What is striking is that such a small edge is capable, over time, of producing a nice overall gain.

Disclosure: I am short SPY, GLD.

Recommended For You


To ensure this doesn’t happen in the future, please enable Javascript and cookies in your browser.
Is this happening to you frequently? Please report it on our feedback forum.
If you have an ad-blocker enabled you may be blocked from proceeding. Please disable your ad-blocker and refresh.