Predicting stock market movements with Machine Learning and err... twitter?

Read Time:

In the fast-paced world of stock market forecasting, the fusion of machine learning techniques with insights from Twitter is creating new possibilities. Research has shown that analyzing sentiments from both social media and traditional news can significantly impact our understanding of market dynamics. So we at Ionio decided to implement this technology ourselves and report our findings.

The Power of Sentiment Analysis

Sentiment analysis has emerged as a key tool in financial forecasting. By evaluating the tone and sentiment of news headlines and social media posts, analysts can gain insights into public perception and its potential effects on market trends. This approach is based on the premise that the collective mood and opinions of investors, as reflected through these channels, can influence stock prices.

Integrating Machine Learning with Social Media

Our algorithm leverages LSTMs to digest and interpret vast amounts of data from Twitter and reputable news outlets like the New York Times. Tools such as Long Short-Term Memory (LSTM) networks, VADER (Valence Aware Dictionary and sEntiment Reasoner), and FinBERT (Financial Bidirectional Encoder Representations from Transformers) play critical roles in assessing sentiments related to the stock market.

# Normalize the data using MinMaxScaler
scaler_news = MinMaxScaler()
news_sentiment_data_normalize = scaler_news.fit_transform(news_sentiment_data)
news_sentiment_data_normalized = news_sentiment_data_normalize[:-5]
scaler_social = MinMaxScaler()
social_sentiment_data_normalize = scaler_social.fit_transform(social_sentiment_data)
social_sentiment_data_normalized = social_sentiment_data_normalize[:-5]
scaler_prev_adj_close = MinMaxScaler()
prev_adj_close_data_normalize = scaler_prev_adj_close.fit_transform(prev_adj_close_data)
prev_adj_close_data_normalized = prev_adj_close_data_normalize[:-5]
scaler_target = MinMaxScaler()
target_data_normalize = scaler_target.fit_transform(target_data)
target_data_normalized = target_data_normalize[:-5]

# Create input sequences for news sentiment
news_sentiment_input = []
for i in range(len(news_sentiment_data_normalized) - seq_length_news + 1):
news_sentiment_input.append(news_sentiment_data_normalized[i:i + seq_length_news])
news_sentiment_input = np.array(news_sentiment_input)

# Create input sequences for social sentiment
social_sentiment_input = []
for i in range(len(social_sentiment_data_normalized) - seq_length_social + 1):
social_sentiment_input.append(social_sentiment_data_normalized[i:i + seq_length_social])
social_sentiment_input = np.array(social_sentiment_input)

# Create input sequence for previous day's stock data
prev_adj_close_input = prev_adj_close_data_normalized[seq_length_news - 1:]
news_sentiment_input_layer = Input(shape=(seq_length_news, num_features_news), name='news_sentiment_input')
social_sentiment_input_layer = Input(shape=(seq_length_social, num_features_social), name='social_sentiment_input')
prev_adj_close_input_layer = Input(shape=(num_features_news,), name='prev_adj_close_input')

# Define LSTM layers for each input stream
news_sentiment_lstm = LSTM(50, dropout=0.1)(news_sentiment_input_layer)
social_sentiment_lstm = LSTM(50, dropout=0.1)(social_sentiment_input_layer)

# Concatenate the outputs of the LSTM layers
concatenated = concatenate([news_sentiment_lstm, social_sentiment_lstm, prev_adj_close_input_layer], axis=-1)

# Add Dense layers or any other layers as needed
dense_layer = Dense(64, activation='relu')(concatenated)
output_layer = Dense(1, activation='linear')(dense_layer)

# Create the model with multiple inputs and single output
model = Model(inputs=[news_sentiment_input_layer, social_sentiment_input_layer, prev_adj_close_input_layer],

So, sentiment analysis is the superstar of the day, right? It's all about extracting opinions and vibes from what's being said online.

Now, there's this other dude called judgment analysis. It's pretty straightforward, it's just about figuring out if something is objective or subjective. Sentiment analysis, on the other hand, has got three things going on: it's either good vibes (positive), bad vibes (negative), or just meh (neutral).

Twitter is like this massive pool of thoughts, and every day, it's bombarded with a ton of tweets. We decided to dive right in and look at tweets that talk about #SPX500, #SP500, SPX500, SP500, $SPX, #stocks, $MSFT... you get the idea. Anyways, newspaper headlines are also a great source of info about the stock market and can be used for making predictions.

Check out Fig 1, where you can see how the Dow Jones Index vibes with the sentiment analysis of tweets mentioning these stock symbols.

There's this study by Mendoza and crew (2022), and they found that negative sentiments hit prices harder than the positive ones. It's like this thing in behavioral economics called loss aversion, where the pain of losing just hits different compared to the joy of winning the same amount.

Now, you can also use regression techniques to guess the stock market's mood swings using sentiments from social media. Saravanos and their team found that there's a tight connection between the closing prices in the U.S. market, the sentiment in a tweet, and the tweeter's activity and network.

We're also checking out the sentiment in news from the New York Time’s Business Day sections. We took the matching articles for each row in our data, and then we let FinBERT do its thing on the abstracts.

The big idea here is to use sentiment analysis on news articles to guess stock trends. It's about pulling out the sentiment from financial news using NLP, linking this sentiment data with old stock prices, and training models to maybe find connections between sentiment and stock movements. But remember, stock trends are a tricky business, influenced by a gazillion factors. So, you gotta have a broad approach that includes different indicators and expertise for solid predictions.

Why Twitter?

Twitter acts as a real-time pulse on public opinion, offering immediate insights into how current events, corporate actions, and broader economic indicators might sway investor sentiment. When combined with ML, this data can uncover patterns and trends not immediately evident through traditional analysis methods.

The Role of Traditional News

While social media offers immediacy, traditional news outlets provide depth, authority, and a structured perspective on events impacting the financial markets. The integration of sentiment analysis from these sources enriches the predictive model, offering a balanced view between rapid social media reactions and considered journalistic reports.

FinBert, VADER and Sentiment Analysis

So, FinBERT uses this really cool sentiment analysis dataset that comes from Financial PhraseBank5. It was made by Malo and his team back in 2014. Basically, it's a collection of 4,845 English sentences that were randomly picked from financial news in the LexisNexis database. Then, 16 finance and business whizzes checked these sentences and labeled them based on how they thought the info could affect the mentioned company's stock price.

The dataset also tells us how much the annotators agreed on the sentences. To make BERT even better, FinBERT uses a financial-focused group of documents called TRC2-financial. This comes from Reuters' TRC24, which is made up of 1.8 million news articles written between 2008 and 2010. They used specific financial keywords to fine tune the documents, making it fit with the computational resources they had. The final TRC2-financial corpus has 46,143 documents, over 29 million words, and almost 400,000 sentences. Pretty cool, right?

So here's the deal - we've got two sentiment analysis methods, VADER and FinBERT, and they're really good at understanding the vibes in financial data. VADER, which is all about rules, is great at handling Twitter data. FinBERT, on the other hand, has been trained to get the gist of New York Times headlines. And here's the cool part - we're also using the Yahoo Finance API to bring in historical stock prices, which gives us a way to match up the news and tweets with what was happening in the stock market at the time, and helps us make predictions. Oh, and we don't bother with weekend financial records because we're all about keeping the data clean.

What we end up with is a dataset that's been normalized, which makes machine learning a whole lot easier. We've been really thoughtful about the design of the model's input layers, mixing together the average sentiment scores from news and social media and the previous day's closing stock price. And this isn't just a random mix - it's a strong base for making predictions.

Our model has two dense layers with specific activation functions, and the output layer is set up to make some pretty accurate predictions. We chose Mean Absolute Error (MAE) as the loss function because we wanted a model that can handle outliers, which is really important when you're trying to predict the stock market.

So, in a nutshell, we're using sentiment analysis and financial data to build a model that can predict the stock market. It's got a lot going on, but we've got it all under control, handling the ups and downs of sentiment and the nitty-gritty of finances.

Feature Importance

The linear regression model revealed that news sentiment has a higher coefficient (34.60) compared to social sentiment (20.95), indicating that changes in news sentiment have a relatively stronger influence on stock market movements. Conversely, the decision tree regressor identified news sentiment as the most important feature with a feature importance score of 0.75, followed by social sentiment with a score of 0.25. This suggests that while both sentiments play a role, the decision tree model places more emphasis on news sentiment in predicting stock market fluctuations. The discrepancy in feature importance between the two models underscores the importance of considering multiple regression techniques to gain a comprehensive understanding of the factors influencing stock market behavior.

1. Linear regression

2. Decision Tree Regressor


Our prediction model combines two powerful tools to forecast stock market trends: VADER sentiment analysis and FinBERT. VADER analyzes 90 days of stock data to predict market trends, while FinBERT assesses financial and business articles for each corresponding date, providing a mean sentiment value. By comparing the sentiment values from these two sources, we gain insight into the volatility of market sentiment. This unique approach sets our model apart, as it accurately incorporates the discrepancies between the two data sources. This precision is a key factor in our model's superior performance compared to others, as it ensures we capture and account for the nuances in market sentiment across platforms.

We used Dropout during training to keep our model from overfitting. It randomly turns off some neurons in a layer during the training. We initially ran the model for 100 rounds, but we noticed that the loss was dropping gradually only for the first 40 rounds. After that, it kind of hit a plateau. This means running it for more than 40 rounds might not make a huge difference. The training and validation losses after 40 rounds were 0.0427 and 0.0528 respectively.

Our model did a good job of recognizing patterns in the validation data, which is a good sign. That means it's learning to apply what it's learned to new data. But, there were still some differences between the actual and predicted values. That means our model hasn't quite figured out all the patterns in the data, or there might be some noise or variability that's hard to model.

The predicted values line up pretty well, which shows that our model did a pretty good job of predicting the Dow Jones Index's stock price.

This just goes to show how good Long Short-Term Memory (LSTM) models are at remembering important information over a long time. In simpler words, a low loss means our LSTM model did a really good job.

The figure below shows the results from the LSTM. The RSME of the validation set was 2.65.

Beyond the Basics

So, this method isn't your usual stock market prediction model. It's got a bit more going on. It takes a look at the whole picture of sentiment data. It checks out the mood of Twitter hashtags and dives deep into news headlines. The research shows it's a more detailed way to guess where the market's headed, especially during wild times like the COVID-19 pandemic.

So, the model did a pretty good job of getting the big picture right with the validation data. But, there were a few spots where what it predicted and what actually happened didn't quite match up. It's like, while it got the major trends in the data, some of the finer details in the crazy world of finance slipped through the cracks. These hiccups could be from random noise or sudden changes that even the smartest models can't predict.

Still, the LSTM model did a solid job predicting the Dow Jones stock price. It's good at remembering important things over time while ignoring the noise. It shows that LSTM models can really shine in finance, picking up on the subtle patterns over time and turning them into pretty insightful predictions.


As the financial industry continues to evolve, the combination of machine learning, Twitter sentiment analysis, and traditional news sentiment analysis presents a compelling new frontier. This methodology not only offers insights into immediate market reactions but also provides a deeper understanding of the underlying sentiments driving these movements, proving essential for investors and analysts alike in navigating the complexities of today's markets.

Book an AI consultation

Looking to build AI solutions? Let's chat.

Schedule your consultation today - this not a sales call, feel free to come prepared with your technical queries.

You'll be meeting Rohan Sawant, the Founder.
Book a Call

Let us help you.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Behind the Blog 👀
Srihari Unnikrishnan

Srihari is an NLP Engineer at Ionio. That's about it really.