1.006 Trading Signal
comments 8

A Simple Machine Learning Model to Trade SPY

I have created a quantitative trading strategy that incorporates a simple machine learning model to trade the SPY as part of my ongoing research in quantitative trading. The focus here was not on creating a strategy with alpha but rather to develop a framework both in my mind and in code to develop more advanced models in the future.

1. Does SPY Exhibit Short-Term Mean Reversion or Momentum?

Examining whether SPY exhibits short-term mean reversion or momentum is the central idea in this strategy. If negative returns tend to precede positive returns (or positive returns precede negative returns), that suggests that mean reversion exists. If positive returns tend to precede positive returns (or negative returns precede negative returns), that suggests that momentum exists.

In other words, short-term mean reversion or momentum exist if there is some relationship between the daily return of SPY and the lagged daily return of SPY over various time periods.

Below I plot the daily returns of SPY versus the lagged daily return of SPY over periods of one day to nine days. The top left plot shows the relationship between the daily return of SPY versus the one-day lagged daily return of SPY. The blue line in the plots is a smoother to aid in detecting patterns in the data.

Scatterplots of SPY daily returns versus the lagged daily return over various time periods.

Scatterplots of SPY daily returns versus the lagged daily return over various time periods.

So is there any evidence of mean reversion or momentum? The interesting plots are the first two plots which represent the one-day and two-day lagged returns. The plots suggest that there is weak evidence of some short-term mean reversion, especially when there has been a large negative return in the past. There are relatively more extreme observations in the top left quadrant of the plots compared to the other quadrants. These observations represent instances where the SPY has gone down by a lot and then corrected upwards the following day. The smoother has a slight negative slope which confirms the weak evidence for short-term mean reversion.

Why might this happen? The narrative is that occasionally broad-based and panic-induced selling occurs which exhausts all the short-term selling pressure, so the market corrects the following day.

2. Training a Logistic Regression Model

We have established that there might be some relationship between SPY daily returns and the one-day and two-day lagged daily returns, but is this enough to build a predictive model? I decided to treat this problem as a classification problem to keep things simple — I am only interested in predicting the direction of future returns (positive or negative) and not the magnitude.

The machine learning method I decided on using is logistic regression which is a simple learning method that is often used before more flexible learning methods.

The data was split into a training set that consists of SPY observations from 2000 to 2013. The following 10 models were trained on this training set with each model identical to the model above it but with the addition of an additional lagged return as a predictor. Models 3 through 9 are not shown for brevity.

m01 <- glm(daily_return_sign ~ 
             daily_return_lag1, 
           data = SPY_training, 
           family = binomial)
m02 <- glm(daily_return_sign ~ 
             daily_return_lag1 + 
             daily_return_lag2, 
           data = SPY_training, 
           family = binomial)
...
m10 <- glm(daily_return_sign ~ 
             daily_return_lag1 + 
             daily_return_lag2 + 
             daily_return_lag3 + 
             daily_return_lag4 + 
             daily_return_lag5 + 
             daily_return_lag6 + 
             daily_return_lag7 + 
             daily_return_lag8 + 
             daily_return_lag9 + 
             daily_return_lag10, 
           data = SPY_training, 
           family = binomial)

3. Assessing Model Accuracy

Logistic regression models output the probability that an observation will belong to each class (in our case, whether the daily return will be positive or negative), but it is up to the practitioner to decide on a logical probability threshold to assign the observation to a class. Which probability threshold to decide on is a trade off between the number of true positives you want versus false positives. This post on StackExchange helped me understand this concept more clearly.

One logical probability cut off to choose is if the probability that the observation belongs to a class is greater than 50%, then assign that observation to that class. Unfortunately, I am unable to use this decision rule in this case because the model predicts for almost all the observations that the probability that the daily return is positive is greater than 50%. In fact, the predicted probabilities are clustered around 54% — which is also the probability that the daily return is positive in the data.

Simply put, the models were not able to predict with much confidence given the predictors available to them, so the predicted probabilities that the daily return is positive had a very narrow range centered around the observed mean probability for positive return in the data. As such, I set the probability threshold of around 54% to assign observations to a predicted positive return or predicted negative return.

The models were evaluated on a test set that consists of SPY observations from 2014 to present. I decided to use the accuracy rate as a measure of assessing model fit which is simply the percent of observations that the model predicted correctly. Here are how the models performed.

 model accuracy
1 m01 0.5204
2 m02 0.5302
3 m03 0.5432
4 m04 0.5383
5 m05 0.5106
6 m06 0.5155
7 m07 0.5334
8 m08 0.5318
9 m09 0.5188
10 m10 0.5253

4. Choosing the Model and Constructing Trading Signal

These might seem like good results since the models have accuracy over 50% until you remember that a classifier that would simply predict a positive daily return for every observation would have an accuracy of 54% (since 54% of the time, the daily return of SPY is positive).

Unsurprisingly, predicting future returns is hard and these models are bad. But let’s take a closer look at model m03 which has an accuracy of  54.3%. Here is a table of actual daily returns versus predicted daily returns.

                                   negative_predict      positive_predict
negative_actual 160                                  120
positive_actual  160                                   173

When model m03 predicted negative returns, it was right 160 times and wrong 160 times with an accuracy of 50%. When model m03 predicted positive returns, however, it was right 173 times and wrong 120 times with an accuracy of 59%. This is pretty promising, so let’s use model m03 and only trade when it predicts a positive return.

The logic for constructing the trading signal is to use the 54% probability threshold for assign observations to a predicted positive return or negative return, go fully long when the predicted class is a positive return, and be flat when the predicted class is a negative return.

5. Assessing Strategy Performance

Here is the equity curve. It turns out that this simple strategy isn’t complete garbage and has actually outperformed the buy-and-hold return of SPY, at least over this test set. The strategy performed extremely well from late 2015 to present. Adding in transaction costs, however, would cause this strategy to underperform the SPY because there are a large number of trades.

1.006 Equity Curve

Here is the SPY closing price with a mapped trading signal color gradient. Blue indicates times when the model was fully long and black indicates times when the model was flat. The plot shows that the strategy tends to go long as the market dips and hopes for a correction which exploits the short-term mean reversion explored earlier in this post.

1.006 Trading Signal

The code for this post is comprised of 218 lines of code in R and can be found on my Github.

Filed under: R
comment 0

Thoughts on Applying Machine Learning to Trading

Recently, I have been interested in applying machine learning to trading. This post contains some of my thoughts regarding a framework for thinking about trading as a machine learning problem, treating trading as a classification or regression problem, and transforming the output of a machine learning model into a trading signal.

1. Introduction to Machine Learning Applications to Trading

Machine learning refers to the construction of algorithms that can learn from and make predictions from data. Fundamentally, the purpose of any trader is to predict the future and enter into positions in financial securities that reflect this view of the future, so how can we make trading a machine learning problem?

First, let us define machine learning with more precision. Suppose we observe a response variable Y and p different predictor variables, X1, X2, …, Xp. We assume that there is some relationship between the response variable and the predictor variables in the following form:

Y = f(X) + ε

The interpretation is that Y is some unknown function of X plus some random error term which is independent of X and with mean zero. Machine learning refers to the methods used for estimating f. Why do we want to estimate f? Often times we have available to us the predictor variables X but not the response variable Y. If we have a good estimate of f, then we can create good predictions for Y, the variable that we care about. Other times we are interested in learning more about the relationship between X and Y.

Back to trading. As I said before, fundamentally, the purpose of any trader is to predict the future and specifically the future returns of some financial security. So to make trading a machine learning problem, all we have to do is let the response variable Y represent future returns. The future returns can be on any time scale depending on your trading strategy, from the next tick (for high frequency strategies) to the next 12 months or beyond (for long-term strategies).

In the context of trading, what is X? X could be anything that is available to us today that we suspect has some relationship with future returns. X could be macroeconomic indicators, fundamental data, market data, or anything else that you can think of.

Suppose you have a set of historical training observations where each observation contains predictor variables that are known at that point in time as well as the return of some financial security over the next period (the future returns of the security relative to that point in time). This historical training set can be used to train a machine learning algorithm, and the hope is that the the predictions from this machine learning algorithm will perform well not only on the training data but also on test observations that were not used to train the algorithm.

2. Trading as a Classification Versus Regression Problem

Variables can either be quantitative or qualitative. Quantitative variables take on numerical values while qualitative variables take on values from  one or more categories. A person’s age is an example of a quantitative variable while a person’s gender is an example of a qualitative variable. The terminology in the machine learning field is to refer to attempts to predict a quantitative variable as a regression problem while attempts to predict a qualitative variable as a classification problem.

Given that we want to use machine learning to predict future returns of some security, should we treat trading as a regression problem or a classification problem? I have seen implementations using both approaches and each approach has a different set of learning algorithms available.

2.1 Trading as a Classification Problem

Let us examine trading as a classification problem first. In this case, the learning method is simply predicting whether the future returns of some security is positive or negative. Here the future returns of the security can be encoded as a 0 or 1, where 0 represents a negative future return and 1 represents a positive future return.

Predicting the future is hard, so sometimes it can be better to simplify the problem to just predicting the direction of future returns instead of the direction and magnitude. This may be sufficient if you are holding for short time periods (one day or less) where the range of potential returns is narrow.

Another consideration for treating trading as a classification problem is that several classification machine learning methods output the probability that the future return will be positive or negative. This can be extremely helpful when it is time to transform the predictions of the machine learning method into a trading signal because probability represents confidence. A high probability that the future return will be positive or negative implies a strong long or short trading signal. You can even implement logic where the trading signal will be flat (take no position) when the confidence of choosing either positive or negative returns is too low.

2.2 Trading as a Regression Problem

The argument for treating trading as a regression problem is that traders want to optimize the overall return of the strategy and not optimize the percentage of profitable trades. A strategy that sustains small losses often but has a small chance of  an extremely large gain can still be profitable. So clearly predicting the direction and magnitude of future returns is important, and that implies treating trading as a regression problem.

This can be more appropriate for strategies with longer holding periods where the range of potential returns is wide.

Predicting future returns as a numerical value also allows for ranking across securities. Suppose a machine learning model predicts the future returns of 100 securities. These 100 securities can then be ranked and a portfolio can be constructed that goes long the top 10 securities and goes short the bottom 10 securities.

3. Creating the Trading Signal

Once the machine learning model has generated predictions for the future returns of one or more securities, transforming that output into a trading signal is straightforward. There are two simple principles to keep in mind: (1) if the model predicts a positive future return, you should go long the security and vice versa, and (2) the more confidence the model has in its prediction or the greater the magnitude of the predicted future return, the stronger the signal should be. I briefly described what a trading signal is in my previous post.

For a classification problem, a simple example is to go long the security when the model predicts a greater than 50% probability of a positive future return and to go short the security when the model predicts a less than 50% probability.

The specific implementation can vary depending on the strategy, but the logic that is applied to create the trading signal should conform to these two principles.

4. Future Research

My cursory review of the available literature for quantitative trading and machine learning suggests there is a large amount of common ground between the two fields. But the material available for quantitative trading doesn’t place sufficient emphasis on some of the most fundamental techniques that are common in machine learning like strict separation of data into training and test sets, cross validation, consideration given to the bias-variance tradeoff, and assessing the quality of model fit.

In my next post, I will implement a simple learning algorithm in R for trading the SPY. The motivation is to slowly build out a suite of R code and functions for use in actual quantitative trading strategies.

 

comments 2

Measuring Performance of a Trading Strategy

This post contains some of the research I have done regarding how to measure the performance of a trading strategy. The immediate goal of conducting this research is to build out a collection of performance measure tools in R to evaluate, compare, and measure the performance of trading strategies. The motivation behind creating these tools is to aid in the development of systematic or quantitative trading strategies in the future.

1. Defining a Quantitative Trading Strategy and Trading Signal

Before identifying performance measurement tools, I define a simple quantitative trading strategy. The output of a quantitative trading strategy is one or more trading signals. A trading signal identifies a security and how much to go long or short that particular security. A simple strategy involving one security would have one signal bounded between -1 and +1, where -1 means to go fully short the security and +1 means to go fully long the security. Intermediate values between -1 and +1 are possible, where 0 means take no position in the security. The signal can be continuous (the signal can take values like +0.5 which means enter into a long position with only 50% of allocated capital) or discrete (the signal can only take values -1, 0, and +1, for example).

Once you have a time series containing the trading signal and a time series containing the return of the underlying security, you can multiply the two time series to get a time series of the return of the trading strategy.

For the purposes of this post, I will measure the performance of a simple trading strategy that consists of going fully long Chipotle Mexican Grill (CMG) equity for the first nine months of a year and going fully short for the remaining three months of a year. I label this strategy as M01 in the plots below. This trading strategy is for illustrative purposes.

2. The Equity Curve Versus Benchmark

The equity curve is the standard tool for measuring performance of a strategy and the first one that I will look at. The equity curve refers to the cumulative return of the strategy over time. Here I plot the performance of the M01  strategy against SPY, the benchmark that I selected for this strategy.

1.005 Equity Curve

When observing equity curve plots, I look for the performance of the strategy versus the benchmark, the correlation of the strategy versus the benchmark, the overall return over time, how volatile the strategy is, what the historical drawdowns are like, and identifying periods of underperformance.

For this particular strategy, I make the following observations:

  • The strategy has not consistently outperformed the benchmark which means that the strategy may not be very good. It is important to compare the strategy to some benchmark to determine how much alpha the strategy generates.
  • The strategy is highly correlated to the benchmark and for the most part seems like a more volatile version of the benchmark which also suggests the strategy may not be very good. Good strategies have high return with low volatility and low correlation to the benchmark. The alternative to any active investment strategy is always passively tracking some index, so the active strategy needs to be a more attractive investment than the index for it to be worthwhile.
  • Maximum drawdown occurs in 2008 and is roughly 75%. This is not a good number.

3. The Trading Signal

A plot of the trading signal and the price of the underlying security is the second plot that I look at. The purpose of this plot is to ensure that the trading signal is well behaved and to identify and investigate periods where the trading signal did not do well. This provides a starting point to investigate the logic and data behind the trading signal and eventually make improvements.

Regarding the shape of the trading signal, I recommend constructing a trading signal that has no net long or short bias if you plan on investing other people’s capital and are charging fees. Not only does this mean that you can profit on the long and short side, but it ensures that the strategy will have low correlation to the benchmark. A strategy that is highly correlated with the benchmark is useless because that means the strategy can be replicated cheaply.

1.005 Trading Signal

As a reminder, the trading signal above is to go fully long the first nine months of a year and fully short the last three months of a year.

An alternative way to view the two plots is to map the trading signal onto the price of the underlying using a color gradient. This plot allows me to understand the signal in the context of the underlying’s closing price. It can clearly show periods where the signal was wrong.

1.005 Price and Trading Signal

4. Rolling Returns

Rolling returns are the cumulative return of the strategy over a specified window of time. Compared to looking at the equity curve, rolling return plots highlight periods of underperformance, volatility, and drawdowns over smaller periods of time.

Below I look at the three month and one year rolling returns. These are logical windows that provide a short-term and long-term view on how the strategy has performed.

The extreme volatility of the strategy is clear in these plots. The strategy underperformed the benchmark from 2012 to 2015. Three month returns of -25% or more have occurred several times in the past, and an annual return of -75% occurred in 2009.

1.005 Rolling Returns 3 Months 1.005 Rolling Returns 12 Months

5. Drawdowns

Drawdown refers to the percentage decline in the strategy from the historical peak profit at each point in time. This plot focuses on the downward potential and volatility of the strategy. Drawdowns measures the pain, fear, and uncertainty that a trader would feel while the strategy is operational.

1.005 Drawdown

I examine the frequency of drawdowns, the size of the maximum drawdown, and the time it takes to recover from drawdowns. In this case, the maximum drawdown was around -80% and it took almost 10 years to recover from the maximum drawdown.

6. Sharpe Ratio

The sharpe ratio measures the risk-adjusted performance of a strategy. This ratio is commonly reported by asset managers and provides an easy way to compare the performance between strategies or asset managers.

There are many subtle variations on how to calculate the sharpe ratio, but in it’s most common form, it is the excess return of the strategy (the return in excess of either a benchmark or the risk-free rate) divided by the standard deviation of the returns. The interpretation of the ratio is the amount of excess return you get per unit of risk, with higher levels being more desirable.

Here I plot the 12-month rolling sharpe ratio for the strategy and the benchmark. I smooth the sharpe ratio a little bit so that the interpretation is more clear.

1.005 Sharpe Ratio

The interpretation is that the M01 strategy only rarely  outperforms the benchmark. Between 2012 and 2016 was a long period of underperformance.

An additional note regarding negative sharpe ratio values — the sharpe ratio doesn’t really apply when the excess returns are negative because low volatility (which is good) leads to a more negative sharpe ratio, but more negative excess returns (which is bad) also leads to a more negative sharpe ratio.

There are a lot of other risk-adjusted measures (information ratio, treynor ratio, sortino ratio, Jensen’s alpha, and so on), but they largely attempt to capture the same thing. These ratios are used more to compare strategies and to communicate to others, and have a somewhat limited use to a designer of a trading strategy, so I don’t really spend a lot of time thinking about the sharpe ratio. A designer should be able to develop a better understanding of the strategy using other tools and plots.

7. Further Research

The code to generate these plots is only 100 lines in R and can be found at my Github. I plan to further develop this performance reporting code into generalized functions later on. This should help in developing some simple quantitative trading systems that I plan on building.

 

Filed under: R
comment 1

Curated Lessons From Reminiscences of a Stock Operator

This post contains a curated list of lessons from Reminiscences of a Stock Operator using passages from the original source. For more information on the book, please read my post Why You Should Read Reminiscences of a Stock Operator. If you do not want to read the roughly 250 page book, however, I have condensed the book into a curated collection of passages from the source material. Emphasis was on selecting passages from the book relevant to today’s traders, investors, and speculators.

The following is a list of curated themes or lessons from the book. Clicking on any of them will link to a curated selection of passages.

Read More

comment 1

Why You Should Read Reminiscences of a Stock Operator

Every field has seminal introductory texts. For traders, investors, and speculators, Reminiscences of a Stock Operator by Edwin Lefèvre is considered by many to be one of the seminal introductory texts. The book is written as first person fiction and was originally published in the form of several articles, but it is generally accepted as the biography of Jesse Livermore, an American trader that operated in the early 1900’s. Livermore made and lost several multi million fortunes by trading in financial markets. After the 1929 market crash, he was reportedly worth $100 million, which is equivalent to around $1.4 billion in today’s dollars. The surprising thing is that he did this without managing other people’s capital.

I am obsessed with retaining knowledge and fearful of forgetting it, so I have often found myself re-reading things over again after some time has passed. I should stop doing this. And hopefully writing this post about why you should read Reminiscences of a Stock Operator will help me stop doing this.

Read More

1.002 GDX
comment 0

I Shorted The Gold Miners Leveraged ETF Pair

I entered into a short position in both NUGT and DUST (the Direxion Daily Gold Miners Bull and Bear 3X ETFs) with notional value of $5,600 each. Of the leveraged ETF pairs that I considered, this pair is the most attractive. Cost to borrow is fairly low at 5% per year and historical 30-day volatility has been high at around 150%. Please reference my guide to shorting leveraged ETFs for more information.

This is how the underlying gold miners index has performed since January 2015. Predicting future volatility is difficult, but the historical volatility for gold miners is high, and this short position is a bet that volatility will remain elevated in the future.

Read More

1.002 SPY and SPXL
comment 1

A Guide To Shorting Leveraged ETFs

This post documents some of my research in creating a trading strategy centered around shorting leveraged exchange-traded funds (ETFs). I present the following thought experiment to motivate readers:

  • Suppose an underlying instrument increases by 25% on day 1 and decreases by 20% on day 2. The return of the underlying instrument is (1 + 0.25) * (1 – 0.20) – 1 = 0%.
  • Now suppose I construct a leveraged ETF designed to track 3x the daily return of the underlying instrument. The return of the leveraged ETF is (1 + 0.75) * (1 – 0.60) – 1 = -30%.

The intuition here is that in flat but volatile markets, leveraged ETFs exhibit price decay due to the effect that volatility has on cumulative returns.

Read More