Add more functionality to the script on getting stocks tweets using Twython and python. Add in a class StockTweetsReader that inherited the base class TweetsReader.
The StockTweetReader class is able to take in a series of stock name (as in company name) and incorporate the different search phrases such as ( <stockname> stock, <stockname> sentiment, <stockname> buy) to form a combined twitter query.
This search phrases are joined together by the “OR” keywords and the twitter search is based on the series of queries. Below is part of code showing the joining of stock name to the additional parts and which the phrases will eventually be joined with the “OR” operator. The final query will look something like <stockname> OR <stockname> shares OR <stockname> stock etc based on the modified part of the list as [”,’shares’,’stock’, ‘Sentiment’, ‘buy’, ‘sell’]
self.modified_part_search_list = ['','shares','stock', 'Sentiment', 'buy', 'sell'] def set_search_list_and_form_search_query(self): """ Set the search list for individual stocks. Set to self.search_list and self.twitter_search_query. """ self.search_list = ['"' + self.target_stock + ' ' + n + '"'for n in self.modified_part_search_list] self.form_seach_str_query()
After iterating through the series of stocks symbols, it will compute the number of tweets, group by date, for each company or stock name to see any sudden spike in interest of the particular stock at any given date. Sample of the tweets count results from a series of Singapore stocks are shown below:
Processing stock: Sembcorp Ind
Processing stock: Mapletree Com Tr
Processing stock: Riverstone
Processing stock: NeraTel
Processing stock: Amtek Engg
Processing stock: Fortune Reit HKD
Processing stock: SATS
Processing stock: UOB Kay Hian
Processing stock: CapitaR China Tr
Processing stock: LantroVision
Processing stock: Sim Lian
There are currently limitation of the results due to API limitation. One is that the query is limited to 100 results and that it is limited to recent tweets (maybe capped within a month or two period). The other is that for short form stock name it may get other tweets having the same short form as the stockname or it might get stuff irrelevant of the stock news eg SATS which has 100 tweets in a single day.
The updated script is found in GitHub. It may need certain workaround to resolve some of the limitations observed.