Detecting Bots On Twitter Using Botometer – Part II


Visit: QuantInsti

See part I to learn more about sentiment analysis and bots for use in trading.

Temporal features

The category temporal features analyze the tweet rate, timing patterns of tweeting and retweeting, etc. For example, if the account tweets at the same time intervals, then it is most likely a bot.

Election Tweets: Identifying the bots

India recently conducted its general election. And there are many bots on Twitter supporting their political parties. It is important to know which tweets were made by bots. But before that let us check if the botometer is working correctly. Let us take known Twitter handles of real political leaders and check if it is anticipating correctly.

First, we install the botometer library using the pip install as shown below and then instantiate a botometer object.

!pip install botometer

Python Botometer for Twitter Sentiment

Now let us try with the known handle of U.S. President Donald Trump and see what the botometer says.

Python Botometer Score

@realDonaldTrump is the Twitter handle of the American President. After fetching the bot score for this handle we can print and check it using the following command.

Python Botometer for Twitter Sentiment Categories

All the bot scores’ values are very low on a scale of 0-1, indicating that most likely the user is a human. We can also get the final score or the aggregate using the following command.

Python Code Botometer

Since most of the tweets made by Mr Doanld Trump are in English, we can consider the English score of 0.03 on a scale of 0-1 as the final indicator. In general, we consider a score of more than 0.6 as an indication that an account is being controlled by a bot. So if any of you who had doubts whether the tweets made by the account @realDonaldTrump were really made by the President himself, then these scores should settle that doubt now.

Now let us get back to the Indian election and check how the Twitter handles of the two top candidates performed.

First, let us run the analysis for the current Prime Minister of India: Mr Narender Modi. His Twitter handle is @narendramodi

Python Code Sentiment Trading

And his bot scores are

Python for Twitter Sentiment

Since some of Mr Narendra Modi’s tweets are in Hindi, we need to consider the universal score in this case. Which again is very low indicating a human user.

Now let us perform the same exercise for the main opposition Prime Minister Candidate: Mr Rahul Gandhi.

Detecting Bots On Twitter Using Botometer
Detecting Bots On Twitter Using Botometer

Again the score here is very low, indicating a human user. The purpose of this exercise is to check the sanity of the outlooks made by the botometer and they clearly look satisfactory and as expected.

But we have not yet identified the bot followers for these leaders. We can do this by importing the Twitter IDs using the tweepy library and then running the botometer check on each of them. The process of separating bots and calculating the market sentiment might seem like a very difficult task, but it can be done easily with proper preprocessing. In our new course on Sentiment Analysis in Trading we show you how to fetch the tweets related to a stock using conditional statements and then perform the pre-processing to identify and remove the unrelated tweets. We then generate the market sentiment and create a trading strategy based on this.


The botometer library is built specifically for analyzing the Twitter feed. It is a very important library in the preprocessing of Twitter data before the data can be used to create a trading strategy. Building the gauging of market sentiment is a very difficult task and it needs data from various sources. And all data cannot be treated the same way. So, you need to develop new techniques to preprocess data from blogs and news articles.

Disclosure: Interactive Brokers

Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.