Finding top trending stocks using python
By: Samurai Strider
Tags: Machine Learning, Python, Finance, Trends, Data Science

Google trends is a service provided by Google, which shows the trend of a specific term in the required period. This trend can be used to predict which stocks will be active in the stock market by analyzing the most searched companies share price on google trends. In the given post, I will be explaining how I wrote the code, which can show the top trending stocks at any moment by using the google trend module.

First you have to download and install the module pytrends on your computer. You can download the pytrends module from this link . This is an unofficial module for retrieving data from google trends website.

Now we will start coding in the python enviroment. We will first import all the relevant libraries for our code. They are as follows:

#importing relevant libraries to use in the project import pytrends

from pytrends.request import TrendReq

import pandas as pd

Now we will use the pytrend module to retrieve the trends of the words related to the financial markets. In the first line, tz=360 relates to US timing and hl= ‘en-US’ refers to language english in USA . I have selected the keywords such as “share price” and “stock price” as these are the terms which are mostly used by general public to check the market status of any stocks on google. In the below code, I have selected the geographical location as “US” which is for USA. You can tweak the code as per the country you are interested in. Also, the timeframe can be modified to see the trends from last hour by “now 1-H” to last four hours by “now 4-H”. This can also range to several years, the description of that is provided in pytrend documentation

#code for finding the frequencies of term in google trends and related queries trending_terms = TrendReq(hl=’en-US’, tz=360)

keywords = [‘share price’,’stock price’]

trending_terms.build_payload(

kw_list=keywords,

cat=0,

timeframe=’now 1-H’,

geo=’US’,

gprop=’’)

term_interest_over_time = trending_terms.interest_over_time()

related_queries= pytrend.related_queries()

Here in the above code , interest_over_time captures how frequently the given keyword is searched relatively in the country. For the above the code , the interest_over_time() gives the following output in ipython.

Relative frequency of the search on respective keyword

Thus share price is relatively more searched term at any time duration. You can experiment with various keywords and modify the keyword list as per your requirements. The related queries capture the term related to the keywords which were more searched. In our case, it will contain the names of companies whose share price was most sought. This is in a dictionary format and should be cleaned for usage. The code for cleaning the related queries is as follows:

# Code for cleaning related queries to find top searched queries and rising queries top_queries=[]

rising_queries=[]

for key, value in related_queries.items():

for k1, v1 in value.items():

if(k1=="top"):

top_queries.append(v1)

elif(k1=="rising"):

rising_queries.append(v1)

top_searched=pd.DataFrame(top_queries[1])

top_searched

rising_searched=pd.DataFrame(rising_queries[1])

rising_searched

Now we have divided the queries into top queries i.e queries which are the most searched term related to keywords. In our case companies whose share price was most searched during that hour with relevant score value.

Top stock price searched in last hour in USA with relative Score

Similarly we can access the stocks whose search is rising as compared to its previous search due to some news or recent happeing. This value is stored in rising_seached variable . The data stored in that variable is given below.

Rising stock price searched in last hour in USA with relative Score

Hence , we were able to extract the top trending stocks at any given time in python by writing a few lines of codes. The advantage of this pytrends is that it does not require any authentication like twitter api and can be used any number of times without restrictions. Thus by tweaking the above code you can find the trending stocks in any country at any given time.