Predicting $TSLA Stock Prices: Set-up
The context of the stock market, algorithmic traders, and the electric car company, Tesla, is introduced. The pipeline for training various recurrent neural networks and deploying them is discussed at length.
Introduction
Context of the Stock Market
Correctly predicting stock prices can be very lucrative. Anybody who spends time on social media would be able to explain the tremendous amount of finance gurus or investing channels that you can join. All of them have the secret edge that retail investors are looking for — and you can join for a very low monthly price!
This investing craze primarily came from the substantial market growth since 2020. During March of 2020, when the COVID pandemic prompted global lockdowns, the stock market took a tremendous downward hit. A lot of institutional and retail investors took this opportunity to invest in companies such as Google, Tesla, Zoom, or Peloton. In the next year, those who held these stocks saw ridiculous returns.
Alongside all of this, the robo traders in the FinTech market grew a tremendous amount. You likely have your investments in a firm that offers a robo trader program, or you have seen an ad for a robo trader such as Acorns. Robo traders are automated trading algorithms that invest the money you give them. They guarantee a small but consistent return on investment in the long term. But if your account is successful in garnering a return, the robo trader service charges a small percentage as a fee.
Many of these robo traders are built using a rule-based logic system, where a programmer sets a series of if-then rules to ensure the decisions the algorithm makes are based in economic literature rather than a black box. Other robo traders rely on machine learning algorithms, which may lead to a black-box effect where the programmer does not know the decision-making paths of the algorithm. Finally, there are many in-between, with components of machine learning and rule-based logic, in order to get the best of both worlds.
My goal with this project is to create a stock prediction algorithm for the electric car company, Tesla ($TSLA).
Context of Tesla
Why Tesla? Because this company provides a very interesting scenario. Elon Musk has been the CEO and biggest shareholder of Tesla since he joined the company. In the past five years, Tesla has skyrocketed in terms of its stock price and its cultural salience. This has resulted in Elon Musk becoming the world's richest man during the COVID pandemic and Tesla being worth more than the next 10 car companies combined.
A lot of the hype and speculation behind Tesla originates from Elon Musk selling a vision to the shareholders and the market at large. Tesla is not just a car company — it is a data company. Tesla could be a future cab company. If you buy a Tesla, you aren't just buying into an eco-friendly electric car lifestyle. You are buying a car that will be able to drive itself. You are buying into an investment that can be rented out as an autonomous ride-hailing service. You are buying into the future that Elon Musk is actively trying to create.
This vision-centric speculation is often criticized as too dependent on Elon Musk and his personal reputation. Every time Elon tweets, the market responds, attempting to guess the next moves. Many of these tweets have landed Elon and the company in a lot of trouble, such as a tweet implying that Elon secured funding to take Tesla private.
With the cultural salience of Elon and Tesla in mind, an algorithm can be built to predict the Tesla stock price by simply using Elon's tweets. In this project, my goal is to explore how traditional stock price prediction algorithms and culture-based stock price prediction algorithms intersect.
Objectives
- Get Tesla's stock data alongside other stock data (market indicators)
- Get cultural data from Twitter and Reddit that may pertain to Tesla or Elon Musk
- Create various recurrent neural network (RNN) models to predict the Tesla stock price
- Compare the models' performance to understand the impact of traditional-only stock data, cultural-based data, and a hybrid model approach
Tentative Plans for Model Building
Figure 1: Training Models Pipeline

Reddit Scraping & Data Extraction
First, there will be a list of subreddits that I will be targeting to get a gauge on how retail investors are thinking about the TSLA stock:
- /r/wallstreetbets (13.4m followers)
- /r/StockMarket (2.6m followers)
- /r/TeslaMotors (2.1m followers)
- /r/TeslaLounge (75.4k followers)
- /r/RealTesla (54.5k followers)
- /r/TeslaInvestorsClub (75.7k followers)
The first two subreddits are general investing subreddits where people talk about their trading strategies, mindset, and stock opinions. The last four are Tesla-specific subreddits that are either pro-Tesla or anti-Tesla in mindset.
The plan is to scrape these subreddits for Tesla-relevant posts and capture the posts' sentiment and engagement statistics. The prior day's scraped posts will be used to determine the current day's outcome (either the closing price or whether the stock goes up, down, or stays the same). A rolling average of posts could be used as well, such as the last 3 or 7 days of posts for the current day.
From these posts, the post content will be analyzed to determine various sentiments and emotions within the text data. The same could be done for the comments under each post to gauge community feedback. Another method to incorporate community feedback is to collect each post's engagement metrics: the number of awards, the number of comments, the upvote score, and the upvote ratio.
Twitter Scraping & Data Extraction
Similar to the Reddit scraping process, Twitter will be scraped for Tesla-relevant information. First, @elonmusk — Elon Musk's Twitter page — will be scraped for all of his posts and their metrics. Second, any tweets talking about Elon or Tesla will be scraped by using the pertinent tags (#Tesla, #Elon, #ElonMusk, $TSLA). Taking either all of the tweets or a representative subset, the sentiment and engagement metrics will be extracted.
The tweets from the previous day (or 3 to 7 days) will be used to predict the current day's Tesla stock performance. From these tweets, sentiment and emotionality will be extracted as various measures, and the views, likes, retweets, and comments will serve as engagement statistics.
Stock Market Data Extraction
For information on the Tesla stock itself, I will be using yfinance (a Python API that scrapes Yahoo's stock information). Specifically, for Tesla's stock I am able to gather the following information for every day the Tesla stock was public:
- Open (the opening price)
- High (the highest price)
- Low (the lowest price)
- Close (the closing price)
- Adjusted Close (the closing price + after-market activity)
- Volume (amount of shares traded)
Moreover, it would be possible to look into incorporating some leading indicators such as:
- Relative Strength Index (RSI)
- Stochastic Oscillator
- Williams %R
- On-Balance Volume (OBV)
Finally, stock market indices data can be collected in order to understand the market at large:
- NASDAQ Composite
- S&P 500
- Dow Jones Industrial Average
Training the Models
Using the data from Reddit, Twitter, and the stock market for the appropriate time frames, various models will be built. There are a lot of parameters that can be changed:
- Type of RNN: There are two sequential recurrent neural networks of interest: Long Short-Term Memory (LSTM) and Gated Recurrent Units (GRU).
- Parameters and hyperparameters of the RNN: Once the architecture is selected, the number of layers, activation function, optimizer, loss function, batch size, learning rate, number of epochs, and normalization methods can be tested to find the best-performing model.
- Range of data: The look-back window can vary — the previous 24 hours of posts, or the past 3 or 7 days.
- Sources of data: One of the primary objectives of this project is to understand what matters more — stock market data or cultural commentary from retail investors. Therefore three sets of models will be built: (1) stock market data only, (2) cultural data only, and (3) both combined.
- Type of prediction: Either a regression model predicting the closing price of $TSLA at end of day, or a classification model predicting whether the stock will go up, down, or stay the same.
Based on the available data, various models will be built for live verification. There must be complete data from Reddit and Twitter for the models that rely on such data, so different models will be built upon different timeframes of data.
Tentative Plans for Deployment
Figure 2: Live Models Pipeline

Data Pipeline in Production
Once the models are built, the same pipeline used in building the models will be employed for live data gathering and prediction. Using Amazon's ecosystem as an example, Figure 2 illustrates how the model will be executed. Daily scraping, cleaning, and analysis of Reddit and Twitter can be set to occur at a set time. The stock market data can be gathered once the previous trading day and after-market trading close. Once this set of data is constructed, the live data for the current day can be run through the models to output a prediction.
Model Deployment & Predictions
For the set of models being compared, it would be good to see the different predictions made by the various models, alongside a graphical representation of the predictions and the real outcomes over time. Using these, each model's accuracy can be displayed. It would be great to display feature importance via permutation importance or saliency maps, but RNNs are extremely context-dependent and this may not be useful. Finally, the performance of the various models (stock-market-only, cultural-only, and hybrid) can be compared to arrive at a conclusion.
Once the models are tested for an appropriate amount of time (3 to 6 months, for example), the best-performing model could also be used to do automated day trading to show the power of the algorithm. Of course, this would only be viable if at least one of the algorithms performs at least as well as a coin toss.
Conclusion
This project is extremely ambitious, and various parts of it will change over time as hurdles, roadblocks, and opportunities come up. Please refer to the most up-to-date posts when curious about the current status of the project.
Overall, this project is an effort to incorporate the mix of traditional data and cultural text data to predict outcomes. Although not every scenario necessitates both forms of data, this project builds diverse data wrangling as a skill set — good data with real value is never readily available. The project also includes the entire end-to-end flow of a data science project: live data scraping, cloud hosting of the data, cloud hosting of the ML model, and a live website accessible by anyone.