In 2021, there was a surge in “YOLO” stock investments that actually funded the startup costs of R U Coding Me. This post is near and dear to my heart, especially as having an automated stock market prediction system to tell you when to invest and in what company would have been an amazing tool!
Before we get too far into the post, I think it’s VERY necessary to point out that there is no surefire way to guarantee you will make a profit. Some of the methods have a higher success rate than others, however, no method is perfect. I am not a financial expert; I just to the technical stuff. Even if you don’t use these to boost your portfolio, these are really cool python projects to help boost your resume!
But if you do decide to invest, make sure it is with money you do not intend to get back. You should not put all of your eggs into one basket when it comes to investment (or life in general).
So with all those disclaimers out of the way, let’s dive into some potential investment strategies and see how we can code them up.
Traditional Stock Market Prediction
There are a few ways we can automate the traditional approaches of stock market prediction. Namely, we can create web scrapers to pull data from financial institution websites and scrape for important details. Unfortunately, each website will have a different format and it makes it difficult for aggregating data.
Nonetheless, let’s explore each method:
Reports
One of the most traditional forms of investment come from stock market reports. Experts will often release monthly/quarterly/semiannual or yearly reports to indicate the forecast of the stock market.
These are more driven for those with a stronger background in stocks, however, there are several folks that can help you get started. You will still need to manually research and invest into companies.
Mutual Funds
A mutual fund is a group of investors that invest in shares from a variety of companies selected by the fund manager. Since everyone is putting their money in the same pile, it is beneficial for the manager to select the best shares on average. This allows for a higher return rate.
Keep in mind, there are other considerations with mutual funds, such as fees and procedures for each fund. However, this is probably the most automated way to invest your stocks effectively.
Not-As Traditional Prediction Techniques
This is definitely one of those riskier options, however, it effectively allows you to coordinate with several Internet friends to effectively create your own insider trading (more on this in a later section).
These groups tend to invest in meme stocks or YOLO stocks, but if this can be very profitable if you are constantly monitoring the market, buy low/sell high and play very cautiously.
Random Number Generator
It should also be noted that a YouTuber created fish code to out-predict one of the notorious groups on Reddit, WallStreetBets.
Even with the fish code, it is still not that easy to automate the process. It would still involve a web scraper of some sorts along with robust filters to get you the information you need.
Using an LSTM to Predict Your Stocks
Using deep learning to solve this problem is a great idea. Many of the trends we see can easily be predicted and calculated in the long run. However, this does not predict world events that could potentially skew these results. While they are not perfect, AI provides a relatively consistent prediction if done correctly.
You should have an environment for programming as well as some experience with Python through personal projects or through a dedicated program before you begin this method. This will help you to work your way through the example and make any changes if necessary.
It would be beneficial to have some experience with deep learning, however, it is not required to do this. One of the best ways to get into AI is to do these fun projects, especially with complex data. If you are unfamiliar with some of these topics, be sure to research more and feel free to leave a question in the comments.
Processing The Dataset
In any machine learning or deep learning project, much consideration should be given to the dataset. With this in mind, there is a popular 5 year history dataset on GitHub that has more than enough data to help us out.
The top of the .csv file will have the following labels:
date,open,high,low,close,volume,Name
- Date will tell us the day of the transaction
- Open is the opening price of the stock for the day
- High is the highest value on that day
- Low is the lowest value on that day
- Close is the closing price for the day
- Volume is the number of shares sold that day
- Name is the abbreviated name of the company stock
These values will be used to pass into our neural network.
The LSTM Neural Network
There are several great options in the deep learning field for pattern recognition. From my academic research experience, using an LSTM or transformer neural network would be advantageous over an FFN or CNN style network in this case. This is because we can correlate daily information about the stock linearly. In other words, the information about each stock depends on the previous day’s information.
LSTMs and transformers utilize a look-back capability by storing the previous weights and influencing the next iteration during training.
AskPython Python Project Implementation
Rather than reinvent the wheel, we used the AskPython LSTM implementation to do our initial testing (implemented here). In this implementation, only one epoch is necessary to obtain relatively accurate values of the predicted stocks.
We experienced issues generating the visual graph on our side. This was caused by a “length of values does not equal length of index” error. You can check out our implementation if you would like
However, we were able to obtain a consistent .0042 training accuracy with ranging performance accuracy between 1.6 and 2.5 on 10 different experiments.
With an accurate plot, we can feed in current conditions from the past few days (or months) and let the network predict what the trend will be for the company.
Using Capitol Trades to Leverage Insider Trading
For those unfamiliar, insider trading is the act of leveraging one’s business/political/etc. position to buy and sell shares in companies using knowledge that is not publicly available. Ultimately, insider trading can be illegal if the action is not registered to the SEC.
Implementation
For those of us with normal jobs, we don’t have this ability to leverage the stock market. Thanks to capitol trades (an online tracker of U.S. politicians’ insider trading that uses 2iQ’s APIs), we can quickly see the top companies that are being invested in through a simple web scraper code!
'''
Original Author: R U Coding Me
License: Attribution 3.0 Unported (CC BY 3.0)
NOTE: Use
https://rucodingme.com/2022/06/24/stock-market-prediction-with-python/
for attribution
'''
import certifi
import pycurl
from bs4 import BeautifulSoup
from io import BytesIO
FILENAME = 'report.html'
# This will get us our page and save it to the
# current working directory
def get_page():
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, 'https://www.capitoltrades.com/issuers')
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()
body = buffer.getvalue()
text = body.decode("iso-8859-1")
f = open(FILENAME, 'w')
f.write(text)
f.close()
# This will load the HTML file and parse it for links
# A more detailed readout can be obtained if you
# go into Developer mode on their website and select
# the desired tag with your information.
#
# Additionally, this also picks up a couple of extra
# links that are not companies... I am ok with this
def parse_page():
HTML = open(FILENAME, 'r')
index = HTML.read()
S = BeautifulSoup(index, features="html.parser")
for tag in S.find_all('a'):
print(f'{tag.name}: {tag.text}')
# ENTRY POINT
get_page()
parse_page()
running our code will give you the following output (or similar)
This code will return all of the companies that are being invested in according to the website. You may notice a couple of odd entries. This is because the code takes all of the links available in the HTML file. This was done because of the way their webpage is set up. All of the companies listed share this in common and it was easy to develop around this premise.
Additional modifications can be made to this so that you can grab other pieces of information. You can also change the page to report back different pieces of information.
You can also modify this to take information from other public sources online, however, capitol trades is the most popular 2iQ public installation at the moment. For our purposes, we just want to see the top companies we should look into for investments.
In Conclusion
There are a lot of techniques we can use to predict stock market trends, and Python is a more than capable language for implementing every one of them!
What’s your favorite technique? Comment below what you do to invest. Thanks for reading!
Founder and CEO of R U Coding Me LLC. Jacob obtained his Bachelor’s of Computer Science at the University of Central Florida. He likes to go to the gym and teach people about technology.