Click here to Login




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.

- Download the following item: StockTwits
- Install the downloader (Instructions)
- Select it in QuantShare (Download -> Download Manager)
- Click on "Open Selected Downloader" then on "Start Downloading" button

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.

Here is how to measure the sentiment of a specific text:
http://access.alchemyapi.com/calls/text/TextGetTextSentiment?apikey=[API KEY]&text=[TEXT]

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)



Note: In the "sentiment" column, "1" means "Positive", "-1" means "Negative" and "0.1" means "Neutral".


QuantShare Script

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

score = GetData('stocktwits', 'score', Zero); // Access "score" data within the "stocktwits" database
buy = score > 0.5;


The second one consists of buying a stock if it has at least two positive scores in the last hour

score = GetData('stocktwits', 'score', Zero);
buy = sum(score > 0, 60) >= 2; // Sum of previous 60 bars; assuming you are backtesting the trading system with a 1-minute period



With minor tweaks, the same logic can be applied to any other data including stock news, commodities news, Forex commentaries and forum posts.












2 comments (Log in)

QuantShare Blog
QuantShare
Search Posts




QuantShare
Recent Posts QuantShare
Previous Posts

More Posts

Back







QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright 2014 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
Follow us on Google+
RSS Trading Items



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.