How to create an automated stock recommendation system in Google Cloud Platform

Kaneel Senevirathne
5 min readJan 1, 2023

--

This blog post is not intended as investment advise. It is only intended to share an open source DIY stock screener created in Google Cloud Platform.

With so much data and information out there today, investment research has become very challenging. Following price movements, balance sheets, earnings and current news for each and every stock is tedious and time consuming. While there are a lot of popular websites promising tools for investment research, most of them cannot be customized for your exact needs. Therefore, knowing how to create your own stock screener can be very beneficial for investors.

In this blog post, I share an example stock screener I created in Google Cloud Platform (GCP). Although my intention is not to share a breakthrough algorithm that picks the next Apple or Amazon, my goal is to share the full code so that you can create your own stock screener in GCP. If you are knowledgeable in Python and have fundamental knowledge in Google Cloud, Docker and Terraform, you should easily be able to change the code, adjust it to fit your own criteria and upload it to GCP.

If you like to check the code, visit my GitHub Repository. You are expected to have fundamental knowledge in Python, Google Cloud Platform, Docker and Terraform to run the code.

The Stock Screener

Last couple of years showed us the importance of retail investors in the stock market. According to data, retail investors account for 52% of global assets and is expected to rise over 61% by 2030. Therefore, understanding the retail investor sentiment can be identified as an important indicator today. While there is no easy way of tracking retail investor sentiment on a daily basis, social media platforms like Reddit & Twitter can be used to understand the general consensus.

Like mentioned earlier in the blog, there are a lot of research tools that help you to asses stocks using company financials. (e.g. — EPS, P/E, PEG, P/B). However, my goal was to create a stock screener that uses indicators that are hard to filter. So I decided to use the number of mentions in Reddit as the main indicator for my stock screener. While retail sentiment cannot solely be used to pick stocks, in this instance I use it for simplicity. I will later discuss how we can use other indicators and tools to improve the quality of the picks.

My stock screener is essentially a Python script that scans through subreddits “wallstreetbets”, “investing” and “stocks”. The script goes over a list of stocks to calculate the number of mentions of each stock in each subreddit. This gives us a rough idea of what retailers are looking at. The script then finds the top three stocks and creates a summary for each stock. The summary includes a sentiment score for the stock (calculated by the vader sentiment analyzer from nltk library), target prices, analyst ratings and put to call ratios for future option expiration dates. An example can be found in Figure 1.

Figure 1: An example PDF created by the stock screener.

Google Cloud Platform (GCP) Architecture

An ideal stock screener would run daily and create a watchlist or a list of recommendations. So we need to create a system that runs on a schedule, creates a watchlist and stores the collected data for later use.

Figure 2: Google Cloud Platform (GCP) architecture.

Figure 2 shows the GCP architecture of my stock screener. The main resources I used to create this architecture are Cloud Scheduler, Cloud Run and Cloud Storage. The Cloud Scheduler is setup to invoke the Cloud Run app daily at 10.30pm. Once the Cloud Run app is invoked, the python script goes over a given list of stocks to pick the best ones to buy. The script then creates a summary PDF and a csv file with collected data. These outputs are then saved in Cloud Storage. Figure 1 shows an example page of the summary PDF created by the script.

In order to create this infrastructure in GCP, I used Terraform. Terraform is an open source tool created to automate infrastructure on any cloud platform. While you can also use the Google Cloud Console or the Google Cloud Command Line Interface (CLI) to replicate this on GCP, terraform allows you to write infrastructure as code. This is very useful since you can easily share your infrastructure with others by sharing your code.

The cost to run this infrastructure in GCP is almost zero. The use of CPU resources and the free monthly invocations makes the cost of Cloud Run minimal. Also since GCP has three free scheduler jobs per month, we can run our Cloud Scheduler free of charge as well.

Visit this GitHub Repository to access the full code. You are expected to have fundamental knowledge in Python, Google Cloud Platform, Docker and Terraform to run the code.

Improving the Quality of the Recommendations

The current state of the screener relies only on the retail investor sentiment. While this is a potential indicator, retailer sentiment alone is not enough to understand stock growth. Therefore, in order to optimize the screener, you can use other indicators and change the criteria to select stocks. Furthermore, you can add additional social media platforms such as twitter to see what stocks are mostly talked about among investors. Finally, you can also use natural language models trained specifically to understand stock sentiment to improve the quality of the picks.

Concluding Remarks

In this blog post, I shared a customizable stock screener created in Google Cloud Platform. While most available stock screeners use filters to pick stocks, creating a customizable stock screener allows you to utilize modern Machine Learning algorithms to improve your picks. The stock screener introduced in this blog scrapes through Reddit to find the most mentioned stocks and creates a summary for the top picks. All this is done in an automated manner in the Google Cloud Platform. Added to that, the cost of running this process is minimal. So if you are creative, you can use this example as a template and create your own stock recommendation system and improve your investment efficiency.

Thanks for reading!

My GitHub — https://github.com/kaneelgit/

Follow me on twitter — https://twitter.com/kaneel_s

While I am eligible to apply to the Medium partner program and monetize my blog posts, I have decided not to. If you liked my post and it brought value to you, you can use the “Give a Tip” button below. Thank you!

--

--