Machine Learning Algorithm To Predict Stock Direction (2024)

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.

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.

Machine Learning Algorithm To Predict Stock Direction (2)

Let’s plot the data:

Machine Learning Algorithm To Predict Stock Direction (3)

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:

Machine Learning Algorithm To Predict Stock Direction (4)

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

Machine Learning Algorithm To Predict Stock Direction (5)

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.

Machine Learning Algorithm To Predict Stock Direction (6)

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.

Machine Learning Algorithm To Predict Stock Direction (7)

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.

Machine Learning Algorithm To Predict Stock Direction (8)
Machine Learning Algorithm To Predict Stock Direction (9)

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.

Machine Learning Algorithm To Predict Stock Direction (10)
Machine Learning Algorithm To Predict Stock Direction (11)

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:

Machine Learning Algorithm To Predict Stock Direction (12)
Machine Learning Algorithm To Predict Stock Direction (13)

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.

Machine Learning Algorithm To Predict Stock Direction (2024)

FAQs

Machine Learning Algorithm To Predict Stock Direction? ›

The LSTM algorithm has the ability to store historical information and is widely used in stock price prediction (Heaton et al. 2016). For stock price prediction, LSTM network performance has been greatly appreciated when combined with NLP, which uses news text data as input to predict price trends.

How to predict the direction of the stock market? ›

Watch the slope – The slope of a trend indicates how much the price should move each day. Steep lines, moving either upward or downward, indicate a certain trend. However, if the line is too flat, it calls into question both the validity of the trend and its predictive powers.

What is the best machine learning algorithm for the stock market? ›

Which machine learning algorithm is best for stock prediction? A. LSTM (Long Short-term Memory) is one of the extremely powerful algorithms for time series. It can catch historical trend patterns & predict future values with high accuracy.

Can I use AI to predict stock market? ›

Stock markets have always been hard to predict, but the best the AI can do is to create a model to predict prices with some degree of legitimacy. Then there are applications like the use of AI-Bots, which are known to use algorithms that can consistently beat the market returns.

Which is the best AI model for stock prediction? ›

10 AI Tools for Stock Trading & Price Predictions
  • Top 10 AI Tools for Stock Trading in 2024.
  • EquBot.
  • Trade Ideas.
  • TrendSpider.
  • Tradier.
  • QuantConnect.
  • Sentient Trader.
  • Awesome Oscillator.
Jun 4, 2024

What is the best algorithm for stock prediction? ›

The LSTM algorithm has the ability to store historical information and is widely used in stock price prediction (Heaton et al.

What is the most accurate stock predictor? ›

Capital Economics has been named the most accurate forecaster of major global stock indices in Reuters polls. The 2023 LSEG StarMine Award was given for forecasting accuracy across 11 equities benchmarks and reflects the breadth and depth of our global coverage of macro and markets.

Is it illegal to use AI to predict stocks? ›

Absolutely, it is legal to use AI in the stock market, and many traders and investors are increasingly turning to AI-powered trading platforms like Ethereum Code to enhance their strategies. These platforms leverage AI algorithms to analyze market data, identify trends, and generate trading signals.

Can ChatGPT predict stocks? ›

While ChatGPT is a powerful tool for general- purpose language-based tasks, it is not explicitly trained to predict stock returns. In addition to evaluating ChatGPT, we also assess the capabilities of other prominent natural language processing models.

Can you use AI to pick stocks? ›

Standard uses for AI include stock picking, portfolio construction and management, and risk management. Many robo-advisory companies leverage AI. It's important to do your due diligence before using AI in your portfolio.

Which AI tool is best for stock market analysis? ›

1. Stock Pulse. StockPulse is an AI tool designed to analyze financial news and other data sources to predict stock market trends. It provides insights on stocks, helping investors make informed decisions.

What is the best free AI stock prediction app? ›

Incite AI's status as the best free AI stock prediction tool is not static. It's part of an ongoing journey of innovation and adaptability. The tool keeps improving as more people us it to their advantage.

Which is the best AI for prediction? ›

The best predictive analytics software at a glance
Best forStandout feature
SAS ViyaAutomated forecastingFlexible automations
One ModelPeople analyticsBuilt for HR use cases
SAP Analytics CloudGenerative AIWell-integrated generative AI assistant
QlikInteractive forecastingNo-code utility
2 more rows
Apr 11, 2024

What is the formula for predicting the stock market? ›

This method of predicting future price of a stock is based on a basic formula. The formula is shown above (P/E x EPS = Price). According to this formula, if we can accurately predict a stock's future P/E and EPS, we will know its accurate future price.

How to predict stock movement? ›

Some of the common indicators that predict stock prices include Moving Averages, Relative Strength Index (RSI), Bollinger Bands, and MACD (Moving Average Convergence Divergence). These indicators help traders and investors gauge trends, momentum, and potential reversal points in stock prices.

Which method is best for stock market prediction? ›

Alongside the patterns, techniques are used such as the exponential moving average (EMA), oscillators, support and resistance levels or momentum and volume indicators. Candle stick patterns, believed to have been first developed by Japanese rice merchants, are nowadays widely used by technical analysts.

How do you know which way a stock is going? ›

Generally, you want to see up weeks in higher volume and down weeks in lower trade. Also look for churn, or heavy volume with little change in stock price. This type of action can signal a change in direction for stocks, either up or down.

Top Articles
Long-Term Effects and Prognosis for Back and Neck Herniated Disc
The 10 Things You Need to Know to Pass a CLEP Exam [Report]
Foxy Roxxie Coomer
Diario Las Americas Rentas Hialeah
123 Movies Black Adam
Garrison Blacksmith Bench
Best Transmission Service Margate
Umn Pay Calendar
Craigslist Phoenix Cars By Owner Only
Ohiohealth Esource Employee Login
Camstreams Download
Synq3 Reviews
Pac Man Deviantart
Dirt Removal in Burnet, TX ~ Instant Upfront Pricing
Inter-Tech IM-2 Expander/SAMA IM01 Pro
Hollywood Bowl Section H
Sni 35 Wiring Diagram
라이키 유출
Music Go Round Music Store
We Discovered the Best Snow Cone Makers for Carnival-Worthy Desserts
Fsga Golf
Mc Donald's Bruck - Fast-Food-Restaurant
The EyeDoctors Optometrists, 1835 NW Topeka Blvd, Topeka, KS 66608, US - MapQuest
Purdue 247 Football
Boston Dynamics’ new humanoid moves like no robot you’ve ever seen
Rubber Ducks Akron Score
Cain Toyota Vehicles
Crossword Help - Find Missing Letters & Solve Clues
Silky Jet Water Flosser
Jcp Meevo Com
15 Primewire Alternatives for Viewing Free Streams (2024)
Rek Funerals
The Fabelmans Showtimes Near Baton Rouge
Weather Underground Durham
CohhCarnage - Twitch Streamer Profile & Bio - TopTwitchStreamers
Uno Fall 2023 Calendar
Davita Salary
Ravens 24X7 Forum
A Grade Ahead Reviews the Book vs. The Movie: Cloudy with a Chance of Meatballs - A Grade Ahead Blog
Jambus - Definition, Beispiele, Merkmale, Wirkung
Nsu Occupational Therapy Prerequisites
Robot or human?
Craigslist Hamilton Al
Acadis Portal Missouri
Me Tv Quizzes
Puretalkusa.com/Amac
Ursula Creed Datasheet
Yale College Confidential 2027
Windy Bee Favor
Secondary Math 2 Module 3 Answers
Competitive Comparison
Fetllife Com
Latest Posts
Article information

Author: Merrill Bechtelar CPA

Last Updated:

Views: 5432

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Merrill Bechtelar CPA

Birthday: 1996-05-19

Address: Apt. 114 873 White Lodge, Libbyfurt, CA 93006

Phone: +5983010455207

Job: Legacy Representative

Hobby: Blacksmithing, Urban exploration, Sudoku, Slacklining, Creative writing, Community, Letterboxing

Introduction: My name is Merrill Bechtelar CPA, I am a clean, agreeable, glorious, magnificent, witty, enchanting, comfortable person who loves writing and wants to share my knowledge and understanding with you.