Sentiment Analysis on Stock News

Rahman Taufik
3 min readJan 7, 2021

Sentiment analysis is a text mining process that extract opinions in text. It refers to natural language processing (NLP) and is widely applied to get opinion in social media, customer reviews and survey responses.

Basically, sentiment analysis has three sentiments, which are positive, negative and neutral. Here is the example of sentiment analysis from monkeylearn.com

In this article, the sentiment analysis is used to get sentiment from stock market news. We review the news whether it has a positive, negative or neutral.

The tool we use is TextBlob which is a Python library for natural language processing textual data. TextBlob performs well for formal language and returns sentiment property in the form of polarity and subjectivity. The polarity returns polarity score (i.e. actual sentiment polarity) as a float within the range [-1.0, 1.0], where -1 is near to negative and 1 to positive. While, the subjectivity measure subjectivity of text within [0, 1], where 0 is very objective (i.e. not subjective) and 1 is very subjective.

We use polarity score to get the stock market news whether is positive, negative or neutral. We can set the threshold and the classification as follows

if news.sentiment.polarity >= 0.05:
score1 = 'positive'
elif -0.05 < news.sentiment.polarity < 0.05:
score1 = 'neutral'
else:
score1 = 'negative'

Actually, the threshold score is a default score, so we can set the threshold you want to set or according to the data

Let’s look at an example of sentiment analysis regarding stock market news

The code
The result

As we seen in above, there are three examples news about stock market news. Each of them has different polarity, subjectivity and score. These results are obtained from the context of the words contained in the news and text-processing operations behind TextBlob.

If the results refers to common news, maybe this is very useful as a reference, but since the news is for stock market, the results are not optimal, which can be improved in terms of model, threshold or stock term that can be used as sentiment words.

This article was intended to demonstrate the experience and interest in analyzing news related to the stock market using the sentiment analysis approach. It can be used in other fields or developed to better model related to the stock market.

If you’re interested in this thing, here are some references that you can follow

--

--