Algorithm To Predict Stock Direction

Machine Learning Algorithm To Predict Stock Direction

In 2014 the Robinhood Commission-free trading app opened up for business. I eagerly signed up, put money in, and imprudently bought a few high-tech biotech stocks that caught my eye. One year later, my hastily scraped together “portfolio” was down 40%. Fast forward 4 years later, and now I set to apply quantitative techniques to determine stock price direction in order to turn a profit.

Goals

This post will teach the reader how to apply ML techniques to predict stock price direction. In addition, it will shed light on how to use the repository’s backtesting module for use with your own algorithms.bballboy21/stock_surfaceMachine Learning Algorithm To Predict Stock Direction — bballboy21/stock_surfacegithub.com

High School Math → Machine Learning

Mostly everyone in high school had some sort of class where they took observations (maybe measuring the height of a plant over time in biology class).

Here is an example of plant height data.

Let’s plot the data:

Then in class, you would be asked to add a trend line (blue dotted line). The trend line or line of best fit, tries to draw a line through the points in a way that minimizes the vertical distance (or error) between the line and each point. After you had your line, if someone asked you how tall is the plant on day 6.5, you would look at your trend line and see where the Y value intersects the 6.5 value on the trend line (~6.1).

A linear regression is fairly trivial and can even be computed by hand; however, what if we varied the amount of water the plant was given each day as well as varied the amount of sunlight each day? Now our input data would look like this:

Let’s plot the data (the axis are a little shifted):

Now if someone were to ask you what would you expect the height of the plant to be on a day 3 with .7 water and 5 light, it becomes a little more challenging; however, that’s where we can start to use some machine learning techniques. Let’s add some new vocabulary to our old thinking of statistics. Our input data points are now called features and what we’re predicting/measuring are our target values.

In our problem of predicting stock direction, we aren’t looking at something like height that can take on any number 0 to +infinity. We are looking at only two possible outcomes, either the stock goes up in a few days or goes down (binary classification). Let’s relabel our columns and change our Y_height column to only include two outcomes. Now our table looks more like it could be our stock data.

Now, our goal is to train a model where we could give it a new unseen feature set and have it predict the price direction for some future target date.

ex: [5, 0.6, 7] → 1, [4, 0.3, 8] → -1

Background — ML Techniques

Note: This module is written in Python and uses the Scikit-learn library. I highly recommend this package for anyone looking to get started with ML.

ML can be broken down into supervised and unsupervised learning. Unsupervised learning is when the feature set doesn’t come with target values, and the algorithm’s goal is to group the input data based on the different features of the input data. Imagine taking out the Y column from above and telling the computer to group the input features into a certain amount of groups. Supervised learning is when the target values are provided for each of the feature sets. This module uses supervised learning. More specifically, it solves a binary classification problem using supervised learning. We are referring to it as “Binary” since the stock does only one of two things, it goes up or down. The price can stay the same, but we’re counting this as a negative outcome in this case.

The module lets the user input their own custom feature sets, and it matches them up to a target value, +1 stock goes up -1 stock goes down, for a specified amount of days into the future. For example, you can provide the module with a moving average for the past X days, and then it will look ahead 2 days from now to determine how the stock price moved. The moving average data will be the feature set and the binary outcome (price direction up or down) will be the target value. Then, the module pipeline generates a model that can be used to predict the stock price direction on a new unseen set of data.

Let’s Get Coding

First, we need to fetch the stock data. Since Yahoo Finance no longer supports the pandas_datareader library, I switched to the Morning Star API. Pandas_datareader still has a really nice way to fetch the data.

Above is the abridged code. In the module, I have some helper functions that clean up the response from the API. For example, there is a function that removes missing data. The module only takes into account daily stock closing prices; however, you can modify it to use different types of data.

Selecting A Feature Set

One of the most important parts of any machine learning algorithm is the selection and manipulation of data into a feature set you believe is correlated with what you are trying to predict. I recently had an interesting feature set I wanted to test, hence motivating this entire project. It is a twist on a common indication known as a divergence from the mean. Stocks in an industry tend to move together. This feature set is supposed it pick up stocks that are going against industry momentum and should soon correct themselves.

  1. Manipulate stock data and put it all in terms of percent change per day. This is important in order to make sure the numbers we’re working with are all to the same scale.

2. Line up stocks and plot it as a surface so we can see it (oooh pretty colors). The X-axis is the different stock tickers lined up (sorted by market cap). The Y-axis is the days, and the Z-axis is the price change per day.

You can click to zoom and pan.

3. For each day, sum the difference between the stock of interest and the rest of the stocks in the pool. Let’s call this new column the stock’s “Slope Sum” since it sums the slope for each of the days compared to each of the stocks in the pool.

4. Run a sliding window over the Slope Sums in order to batch them together into a “feature set” for each stock. I chose to look at an 18-day sliding window.

What we’re expecting to find is that stocks that have an abnormally high or low Slope Sum batch should have a price reversal.

Target Values

Now we need to match up a target value for each 18 day batch of Slope Sums. The target value will be a -1 or 1 depending on whether or not the stock price increased or decreased on a given day into the future. I chose to look 2 days into the future.

Visualize What We Just Did

I usually find it helpful to visualize algorithms. For example, let’s take 3 stocks in an Excel file.

This example takes a Slope Sum sliding window batch of 3 and checks a target value 2 days into the future. In this case, the target value would be -1 since the stock price dropped over the next two days (blue cell → green cell).

Plugging In The Machine Learning

Now that we have our feature set and our target values associated with our feature set, let’s train a supervised learning algorithm to predict price direction based on our feature sets. I chose to go with Scikit-learn’s Support Vector Machine (SVM), but also added support for other supervised learning algorithms in the full Github repository.

First, we split the incoming data into our testing and training data. Then, we train the model and save it for future backtesting.

Backtesting

Now that we have our model, we need to provide it with new feature sets and see what price direction it predicts. For example, for the stock Facebook, we will send it the sliding window Slope Sum batches for whatever date range we are interested in. Then, the backtesting module creates a new array called a Bid Stream. The bid-stream-creator-function takes a feature set for a given day and predicts how the stock will move in the future using the model we trained. It returns a 1 or -1 depending on if it predicts the stock will go up or down respectively. This Bid Stream is then fed into the take_bid_stream_calculate_profit() function in order to determine our profit if we acted on the algorithm’s output (still assuming we used Robin Hood… no commission fees!).

Let’s see if our model can make us any 💵 💵 💵 ! In the GitHub repository, you will find that I trained the model on the top 30 or so tech stocks by market cap. In the file tests/plotting.py you can find a function “test_plot_stock()” that takes a stock symbol and plots the stock’s close prices, our algorithm’s returns, and the bid stream.

It looks like the algorithm beat the stock by $6.50 over the ~2.5-year span. If you zoom in, it looks like the algorithm correctly predicted the drop in price and avoided it. Boom!

Let’s plot another stock:

Not so hot for Adobe. We lost about $1.25 and if you zoom in it appears that the algorithm incorrectly timed the price drop.

Overall, if we sum the returns for all of the ~30 top tech stocks the algorithm comes out on top by $7.06 or by about +0.4% over a ~2.5-year period. Backtesting module for this calculation is in tests/test_backtest.py::test_on_array_of_tickers_profit().

Summary

I hope this post provided an informative overview of some ML techniques and how one could apply them to the stock market. I encourage the reader to clone the repository and experiment with your own feature sets.

Moving forward, I am going to apply the same algorithm to a portfolio of stocks and other market sectors, as well as publish some more quantitative benchmarks on the algorithm’s returns.

Please leave a comment if you would like me to embellish on any aspects of the post. It was getting a little lengthy, so I had to exclude some topics and pitfalls I ran into while building the module.

This presentation is not intended to be relied upon as advice to investors or potential investors and does not take into account the investment objectives, financial situation or needs of any investor. All investors should consider such factors in consultation with a professional advisor of their choosing when deciding if an investment is appropriate.

Reference : https://medium.com/@jasonbamford/machine-learning-algorithm-to-predict-stock-direction-d54b7666cc7c

Last updated