Tweeting Drives the Stock Market ft. Elon Musk and Tesla

July 20, 2019


Introduction

Recently Tesla has been in the news for upgrades to their electric car business and the CEO, Elon Musk, is known to be very active on the social media site, Twitter. He tweets frequently about Tesla, Inc., SpaceX missions as well as jokes. This sparked the question, which will also be the focus for my project: can a tweet with 280 characters influence the movement of a Fortune 500 company's stock price.

Phase 1 - Collecting Stock Price Data

To kickstart the project, I downloaded daily historical stock data from the past six months for Tesla into a CSV file from Yahoo! Finance1. By having the data in CSV format it can easily be imported into SQLiteStudio, a database manager I will be using to query the data collected and generate reports. I chose to only download data from the past six months as I felt it was a large enough time span to also have enough Twitter data. Also, six months would mean data from the beginning of 2019, which would be a fresh start for any plans Elon Musk has for Tesla in the upcoming year to be revealed through his tweets.4

Phase 2 - Categorizing Elon Musk's Tweets

The next phase of the inquiry involved categorizing Elon Musk’s tweets and retweets into the categories of Number of Tweets Relevant to Tesla and Not Relevant to Tesla from the past six months. This classification would help with trying to find a correlation between tweeting and the movement of the stock market. Aftering tallying up the categories, I inputted the data for: Dates, Tweets Relevant to Tesla, Not Relevant to Tesla and Total Tweets into an excel document, as shown in Figure 1, and then saved it as a CSV file.4

Figure 1: Recording Data

Phase 3 - Exporting Data into SQLiteStudio

Following the Twitter data tally, I imported the data from the CSV files into a new SQLite database in SQLiteStudio. In order to find the days where there could be a strong correlation, I decided to use a weighted formula. The formula I used was: [(0.3 * |Δ stock price|) + (0.7 * total tweets)]. The weighted formula consists of constants which place more weight on the number of tweets as it is more likely there was a day with significant stock movement but not enough importance was being placed on how active Elon Musk was on twitter. The top results should have both significant movement in the stock market and twitter activity in order to be investigated further. A weighted formula seemed to work best to help satisfy the criteria for the top results.

Phase 4 - Generating Reports

To find the top few results, I joined the two tables through an inner join on the common column Date, which would then display the data from the columns selected in the query based on the dates that are common in both tables. I performed the calculation through the query shown in Figure 1, and selected the columns: Date, Change in Stock Price and Total Tweets to be displayed alongside the values from the equation. Additionally, I calculated the average number of tweets per day and the average change in stock price per day to help narrow down the report generated. The filters I added made sure the report generated only included dates where the total number of tweets were greater than the average of 3 and the change in stock price was greater than the average of $0.04/day. This ensured a stronger and more condensed report making it easier to use the results for further analyzation. The query initially generated a report with 125 rows but was narrowed down to 40 rows after filters, shown in row 6 of Figure 2, were added to the query. I also ordered it to display the scores in descending order. From the report, shown in Figure 3, I chose the top 20 days, which is 50% of the data after filtering it, to be analyzed further.

As seen in Figure 3, the dates with the highest scores have a significant number of tweets, that are higher than the average of 3 tweets/day, and a drastic change in the stock price as well.

Figure 2: SQL Code
Figure 3: SQL Report

Phase 5 - Digging Deeper into Twitter Data

After finding out the dates with the top scores, I studied the data further by looking at the ratio of relevant to not relevant tweets about Tesla on the top 20 days. This helped look into the context of what Elon Musk is tweeting about to see whether it had an influence on the movement of the stock price. I executed a query, shown in Figure 4, to display the number of relevant and not relevant tweets about Tesla on the top 20 scoring days. The query report, shown in Figure 5, shows the breakdown of what Elon Musk is tweeting about alongside the change in the stock price.

Figure 4: SQL Code
Figure 5: SQL Report

Phase 6 - Twitter Sentiment Analysis

To see if a pattern existed to form a conclusion, I wanted to find at least 5 days out of the top 20 that showed tweeting having an effect on the movement of the stock price. I conducted a twitter sentiment analysis on the tweets and retweets for the top 20 scoring days to see if there were at least 5 days with a correlation. By categorizing the mood of the tweets or retweets about Tesla in the categories of: positive, negative and neutral I tried to find a correlation to the direction of the change in the stock price. The article, “Twitter Sentiment analysis using fastText” by Sanket Doshi2, provided me with the categories to place the sentiment present in the tweets or retweets by Elon Musk. I recorded the data in an excel document and downloaded it as a CSV file.4 After importing the data into the project's database, I joined the sentiment table with the previous two tables, as shown in Figure 6, to generate the report shown in Figure 7.

The report in Figure 7 displays the change in the stock price alongside the number of positive, negative or neutral tweets about Tesla on the top 20 scoring days. Through this report we can see that on the dates April 23, 2019, January 7, 2019, January 10, 2019, May 31, 2019, and April 22, 2019 Elon Musk could have tweeted or retweeted something to influence the movement of the stock. I then decided to take a deeper look into the tweets that could have had an influence on the movement of the stock price on those 5 days.

Figure 6: SQL Code
Figure 7: SQL Report

Day 1 of 5

Inside look on the tweets for the 5 days:
On April 23, 2019, Elon Musk tweeted about the new Tesla Model S upgrades quite a lot and a video which showed the depth of information the Tesla cameras can collect. This might have caused the stock to rise.

Figure 8
Figure 9

Day 2 of 5

On January 7, 2019, Elon Musk retweeted a thread about an article by Tom Randall, a reporter at Bloomberg, which talked about Tesla, Inc.’s cash flows increasing drastically and Model 3 bringing in the most revenue recorded in history for cars in the U.S. This article shined a light on Tesla’s improvements and how U.S. citizens have increased their purchases of Tesla's electric cars.

Figure 10
Figure 11

Day 3 of 5

On January 10, 2019, Elon Musk retweeted a tweet about Tesla’s car loyalty and tweeted about how safe Tesla is.

Figure 12
Figure 13

Day 4 of 5

On May 31, 2019, Elon Musk retweeted a tweet about how Tesla’s in car cameras helped the police catch a felon. It caught the eye of many twitter accounts as seen from the 16,000 favourites. He also tweeted the link to an article written on drivetest.com which touched on how the Model 3 performance is stellar.

Figure 14
Figure 15

Day 5 of 5

On April 22, 2019, Elon Musk tweeted a link to a video which was about a car in auto-pilot mode. The tweet did not receive a lot of positive replies, one of which caught my eye. Tianyi Zhang mentioned an incident of a parked Tesla exploding that very same day in an underground parking lot in Shanghai, China.

The explosion was reported by many well known news publishers such as The Washington Post, CNBC and The Guardian as well which caused an uproar about Tesla’s cars around the world, as shown by Youp De Boer’s reply in Figure 17 below. It could have influenced the drop in the stock price that day.

Figure 16
Figure 17

Conclusion

To conclude this inquiry, on the days where the stock price rose there were a significant amount of positive tweets about Tesla, such as about upgrades to Model S, it's safety or brand loyalty. On the days where the stock price fell, such as on April 22, 2019, the tweets about Tesla were not always positive or have positive replies, as seen by Tianyi Zhang drawing attention to the fateful car explosion. This leads me to conclude that tweeting can have an influence on the movement of the stock price.


________________________________
1 Tesla, Inc. (TSLA) Stock Historical Prices&Data. (n.d.). Retrieved from https://finance.yahoo.com/quote/TSLA/history?period1=1546405200&period2=1562040000&interval=1d&filter=history&frequency=1d
2 Doshi, S. (2019, March 05). Twitter Sentiment Analysis using fastText - Towards Data Science. Retrieved from https://towardsdatascience.com/twitter-sentiment-analysis-using-fasttext-9ccd04465597
3 Goh, B., & Sun, Y. (2019, April 22). Tesla investigating video appearing to show Model S bursting into flames. Retrieved from https://globalnews.ca/news/5190988/tesla-fire-video/
4 Data Files: Github