Elon’s Golden Gift: Predicting The Stock Price Of Tesla With Twitter And Machine Learning
The complete guide on predicting the price of Tesla with more than 80% accuracy using NLP and Machine Learning

“ What if public sentiment for Elon Musk can be used as a primary indicator for forecasting future prices of Teslas’ stock?”
Announcement: After many requests, I have made a Telegram Groupchat where I will be sharing weekly signals for FREE. You can instantly join from THIS link.
Predicting the markets is undeniably one of the biggest challenges in the world of machine learning and finance. Despite the difficulty of the task, this endeavor is undeniably on the top list of every Data Scientist out there. The problem, though, is that it is nearly impossible… or at least that's what they say.
In the majority of cases, it is true. It is impossible to be able to accurately predict the movements of the financial markets, as they are simply unpredictable. A practical way to view the markets is as a living organism. Just like humans, they are asked to make a multitude of decisions each day. The problem is that most of the time, they do not know what decision they are going to make beforehand. If the markets themselves do not know the decisions they make in the immediate future, how can we?
Having the aforementioned in mind, I personally try not to make frivolous assumptions concerning the markets and price forecasting in general.
At the same time though, I can not stay idle when a golden opportunity such as this one is presented to me.
Elon Musk — A Global Phenomenon
Elon Musk is one of the most influential figures of the 21st century. The tail of his success is undeniably breath-taking and demonstrates how the digital era has transformed the nature of entrepreneurship forever.
All of this fame and success has created an unprecedented phenomenon. In the eyes of the populous, he has become a real-life Tony Stark. I can not think of a mainstream public figure with a fanbase more fanatic than that of Elon Musk. This is mostly due to the fact that many see him as a mentor and aspire to become like him in the future.
In other words, he has become what is known as a “Cult of Personality”. His actions directly reflect on the lives of millions of people across the globe.
This “Cult of Personality” is partly the reason for which his side-ventures (supposing that Space-X is his primary objective) have managed to become so successful. In a sense, the success of his companies is directly related to Elon as a persona.
This was further proven on May 1st, 2020 when Musk decided to go on a twitter rampage, where among other tweets, he posted:

That last tweet cost Tesla a staggering $14 bn of its value.
This is nothing new for Tesla. On September 7th, 2018 Tesla’s stock plummeted uncontrollably. Why? It was a direct consequence of Elon smoking marijuana live on a Joe Rogan podcast.

Again, this is a mere example. There are many more such happenings that have started to showcase a clear trend. An illustration of the immediate consequences of Musk’s actions on the stock price of Tesla can be seen below.

For more information on Elon’s tweets I recommend the following article:
If you like this article and are interested in receiving exclusive monthly content for free, subscribe to my exclusive mailing list at the end of this article (can be also accessed directly from here).
The Golden Opportunity
It must have become apparent by now that the stock price of Tesla is directly connected to Elon Musk. The question is, “what can I gain by knowing this?”.
As highlighted in the introduction, the stock-market is at-best described as unpredictable, due to the harsh reality that much of its movements are random.
Another way of viewing the stock market is as a map. A map that provides insights into the societal trends of all areas of the world. If people like something, they will buy it. If, on the other hand, they dislike it, they will want to have nothing to do with it and, hence, sell it.

By knowing that the price of teslas’ stock is directly connected to Elon (as well as the above) a question arises:
What if public sentiment for Elon Musk can be used as a primary indicator for forecasting future prices of Teslas’ stock?
Project Blueprint
Before being able to lay out a blueprint, a concise objective is needed.
The objective is clear, I will be building a Machine Learning model that will automatically decide when to buy/sell/hold stocks of tesla, by identifying the public sentiment for Elon Musk on Twitter.
The steps I will be taking are the following:
- Fetch as many tweets as I can that contain the keywords “Musk” and “Elon Musk”.
- Fetch as many tweets as I can that contain the keyword “Tesla”.
- Combine all of the tweets for each day into one huge string.
- Add the daily price of Tesla’s stock to the dataframe.
- Perform sentiment analysis on the tweets of each day and assign a sentiment score that corresponds to public opinion for Elon.
- Train machine learning models to combine time-series and sentiment analysis, as a means of predicting the closing price of the next day (by finding the sentiment score of the previous day).
The goal is not to forecast the exact market price at a specific time-stamp, but to identify whether the closing price of the next day is going to be higher/lower/equal to the closing price of the previous day.
In order for the model to be considered accurate, I will set an artificial threshold of 45% accuracy.
(The entirety of the code will be uploaded on GitHub once the official research-paper has been published)
Dataset
Having the right dataset is undoubtedly one of the most important aspects of any data science project. In this case scenario, four different datasets are required:
- The historical closing price of the Tesla stock since 2015/12/31.
- Top 300–500 daily tweets containing the key-word “Tesla” since 2015/12/31.
- Top 300–500 daily tweets containing the key-word “Musk” since 2015/12/31.
- Top 300–500 daily tweets containing the key-word “Elon Musk” since 2015/12/31.
Fortunately, acquiring historical prices for a stock is not that difficult. Yahoo Finance and iexfinance are both great ways to retrieve financial information in python, using their respected APIs. For this instance, I will be manually installing the CSV file of the data from the Yahoo Finance website.
The tweets, on the other hand, require a completely different approach. Unfortunately, twitter’s API allows the retrieval of tweets that are up to one week old. In order to bypass the limitations of the Twitter API, I made a small scrapper with the capability of fetching all of the information I need.
(I will be soon creating a Kaggle dataset, where I will publish the entirety of the CSVs)
Performing The Analysis
The first step is to import all of the required libraries. The majority of them will be either of two types: Natural Language Processing (NLP) libraries or Machine Learning libraries.















