Sentiment Analysis: How to measure the sentiment score of your stock tweets

Updated on 2012-11-01

This post is for advanced traders. Today, I will show you how to measure the sentiment of stock tweets (or any other asset's tweets) and to give them a score depending on how positive or negative they are.

The sentiment of each tweet and its score will be saved in a custom database so that you can use them later to create charts, composites, trading systems or neural network models.

For example, let us take the following tweets:
GOOG stock is bullish
GOOG stock is bearish

The former tweet will get these results: Positive (score: 0.46918)
The latter tweet will get: Negative (score: -0.55188)

Downloading Stock Tweets

First, let us download some stock tweets to analyze them and give them a sentiment score.

Once the data is downloaded, you can display it by selecting "Tools -> database data" then "Stocktwits" database.
The tweets displayed there are the ones that belongs to the active symbol (Symbol from the selected chart)

Alchemy API

Alchemy API is an application capable of identifying positive and negative sentiment within a text. It supports both English and German content.

Before using this API, you should open a free account @ www.alchemyapi.com to get the API Key. You can use that key free of cost and make a maximum of 1000 API calls a day, which should be sufficient to measure the sentiment of our tweets. You may also choose the commercial option to get more API calls.

Replace "[API KEY]" by your API key (sent to you by email) and "[TEXT]" by the text you want to analyze

Updating the Stocktwits database

After measuring the sentiment of each tweet, we would like to store the sentiment and its score in a custom database. We can do that by creating a new database or we can add two new fields to the existing "stocktwits" database.

Here, we will implement the second solution:

- Select "Data -> Edit Databases"
- Select "Custom" next to "Choose database" then choose the "stocktwits" custom database
- At the bottom of the control, click on "Add Field" then add the following fields:

-> Field Name: Sentiment
-> Field Type: Double

-> Field Name: Score
-> Field Type: Double

Updating the Stocktwits downloader

In order to access the newly created "sentiment" and "score" fields, we must update the "stocktwits" downloader so that it updates these fields with empty values ("0").

- Select "Download -> Download Manager" then select "StockTwits" item
- Click on "Update" at the top of the control
- Click on "Parser" button (under "Parser" column)
- Click on "Next" then add two columns by clicking twice on the "Add Column" button
- In the column grid, update the last two columns we have just created by setting the following values:

=> Database -> Field -> Default

stocktwits -> sentiment -> 0
stocktwits -> score -> 0

- Click on "Next" twice then on "Finish" to close the parser control
- Click on "OK" to save the download item
- Run the downloader (It is important you run the downloader after performing these changes)

Now that we have downloaded tweets data, updated the database and opened a free account at Alchemy API, let us create a script that will loop through all our tweets and measure the sentiment score of each one.

- Open the script editor by selecting "Tools -> Script Editor -> File -> New" then enter a name for the script
- Copy the script from here then paste it in the script editor

Brief explanation of how this script works:

- Get all symbols and loop through each one
- For each symbol, get stock tweets data
- Loop through each tweet
- Perform the API call to get the sentiment analysis score of each tweet
- Parse the returned data
- Save the data

Note:
- When the number of API calls exceeds 1000, the script will automatically stop. In this case, you have reached your API call limit and thus you should run the script the next day to calculate the sentiment measure of the remaining tweets.
- You must update the first line of the script by entering your own API (see above for how to get an API key)
- The script output can be found under "View -> List Output".

Charting the Sentiment Score

Everything is in place; our tweets as well as their sentiment score are stored in a custom database. This database can be accessed easily from the QuantShare and C# languages.

- Open a new intraday chart then add a pane
- Select a symbol for which you have tweets and sentiment score data
- Click on the first icon to add an indicator
- Select "Databases / Fields" tab to add custom databases data
- Select "StockTwits" database then "score" field
- In the right panel, under "Get a value", select "Set missing values to zero" then click on "OK"

Trading System based on Tweets Sentiment

If you can access a custom database data with the QuantShare language then you can use that data to create charts, composites, screens, watchlists, trading systems, neural network models...

Here is how to create basic trading systems with the tweets sentiment data:

The first strategy consists of buying a stock if its score is higher than 0.5

Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.