Algorithm To Predict Stock Direction
Last updated
Last updated
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.
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
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
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.
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.
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.
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.
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.
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).
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.
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().
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