I've recently launched one of my weekend projects - a Twitter bot that posts a daily sentiment analysis, along with some stats, for the S&P500 Index ($SPX on Twitter).

The bot's Twitter account is was @spxfeelslike and, before I continue to explain what I did there, here's one of its actual tweets:

https://twitter.com/spxfeelslike/status/832907537188057090

While my bot queries Twitter and CNN's Fear & Greed Index, the heart of the code is the part that analyzes the sentiment of recent tweets. As it turns out - it's a very small piece of code :-)

The two main libraries used here are:

  1. TweetPy - for querying / posting to Twitter
  2. TextBlob - for text processing

I won't go into too much details on the Twitter querying part of the code as it is pretty self-explanatory.

Rather, I'll focus on the sentiment part, which is basically 1 line of code (not counting the import section):

from textblob import TextBlob
sentiment = TextBlob("Gotta love Python").sentiment

Once ran, sentiment will have 2 properties:

  1. subjectivity - indicates whether a sentiment was even detected a sentiment in the provided text
  2. polarity - ranks the positive sentiment of the text, and ranges from -1 (extremely netagive) to 1 (extremely positive)

As you can see, once we have a list of tweets, all that's left to do is to count the total number of tweets with determined sentiment and the total number of positive tweets, and then simply calculate the ratio of positive tweets to total valid tweets.

Here's a full working code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tweepy
from textblob import TextBlob

# your Twitter app credentials
_CONSUMER_KEY = "..."
_CONSUMER_SECRET = "..."
_ACCESS_TOKEN = "..."
_ACCESS_TOKEN_SECRET = "..."

# keyword to query
keyword = "$SPX"

# -----------------------------------
if __name__ == "__main__":

    # twitter authentication
    oauth = tweepy.OAuthHandler(_CONSUMER_KEY, _CONSUMER_SECRET)
    oauth.set_access_token(_ACCESS_TOKEN, _ACCESS_TOKEN_SECRET)
    twitter_api = tweepy.API(oauth)

    # get latest 100 tweets containing the phrase $SPX
    tweets = twitter_api.search(keyword, count=1000)

    # parse sentiment
    valid = 0 # counter for tweets with a determined sentiment
    positive = 0 # counter for positive tweets

    for tweet in tweets:
        text = TextBlob(tweet.text).sentiment
        if text.subjectivity != 0:
            valid += 1
            positive += (text.polarity > 0)

    # sentiment ratio
    sentiment = positive / valid

    # construct tweet
    tweet = "Sentiment for %s is %.2f%%" % (keyword, (sentiment * 100))

    # to actually tweet, uncomment this line:
    # twitter_api.update_status(status=tweet)

    # for now, just print out the tweet
    print(tweet)

Output would look something like this:

Sentiment for $SPX is 52.12%

* Use the above code with caution and enjoy...