Build an Sentiment Twitter Bot for Financial Fundamental Analysis

Growthbotics
3 min readJan 3, 2020

Incredibly, Twitter has been a major indicator of overall stock performance. Namely, the US President Donald Trump and his tweets have consequential affect on the S&P 500 and NASDAQ, especially when Donald Trump tweets about the ongoing trade war.

Thus, identifying keywords from tweets and triggering trade transactions based on Tweet sentiments have been proven lucrative. For example, let’s say if Donald Trump tweeted something about “good meeting” and “tariff”, then we can assume that the trade talks may have been going well, which then means US stocks will rise. As such, one would place a trade on $SPY to ride on the upswing.

In this article, we will build a simple twitter bot that can identify keywords, using Twitter API and simple Python.

Requirements

First things first, we will need to install Python and register a Twitter Developer Account.

We need to get Python 3 (CentOS 6.5) container setup. You can install Python here. Download the correct file that is compatible with your computer.

Thereafter, we will need to get access to Twitter API. Let’s move over to the documentation page found here. Based on the documentation, we can use Tweepy API to meet our project requirements.

Let’s also install the necessary packages:

For MAC users, we need to install pip first with following code:

$sudo easy_install pip

For LINUX users, we would need to install pip first with the following code:

$sudo apt-get install python-pip

We would also need to install Django:

$pip install django
$pip install-r requirements.txt

Create A Twitter Account

Go to create a twitter account here. Thereafter, click on “Create new app”.

Fill out the required fields and make sure the website starts with http://:

Go to “Keys and access tokens” tab, there you will have your “Consumer key” and “Consumer secret”.

Thereafter, you will need to “Create your access token”, at the bottom of the screen.

Install Tweepy API and Recognize Sentiment

To install tweepy on your terminal/command line, let’s type the following:

$sudo pip install tweepy

A few people have been having issues with the installation, if the above code does not work, use the following code instead:

$sudo pip install tweepy --ignore-installed

For simplicity sake, let’s have the Twitter bot identify one negative keyword that might affect the stock market: “more tariffs”. Let’s go ahead and code it:

search = "\"more tariffs\""tweet_list = api.search(
q=search,
count=1,
lang="en"
)

The next step is to create a code block and import tweepy. Make sure the API credentials are included:

import tweepy#create API objectauth = tweepy.OAuthHandler(
keys["consumer_key"],
keys["consumer_secret"])
auth.set_access_token(
keys["access_token"],
keys["access_token_secret"])
api = tweepy.API(auth)

Create the Twitter bot

We will need to create a loop using Python and create a Python function where for each tweet in the predefined tweet list, it will trigger an action. In this article, we will trigger a bot message.

Let’s first write out the code that handles errors:

for tweet in tweet_list:
screen_name = tweet.user.screen_name
message = "@{username} {message}".format(
username=screen_name,
message="Sorry I don't understand"
)

The below script posts message if there is an error and if it encounters a situation that it cannot cope with:

try:
api.update_status(
status=message,
in_reply_to_status_id=tweet.id
)
print message
except tweepy.TweepError as e:
print(e.message[0]["code"])
print(e.args[0][0]["code"])

Launch Bot in Terminal

Let’s go ahead and type the following lines in your terminal/command line.

$conda create -n twitterbot python
$source activate twitterbot
$python

There you go, you’ve made your first Twitter bot!

Want to learn more about twitter bots? Be sure to send a message on Growthbotics!

--

--

Growthbotics

Co-Founder of Growthbotics.com. AI Chatbots Service for the Finance and Blockchain Industry. Contact me at wilson@growthbotics.com