Created
June 10, 2020 18:19
-
-
Save DerekHawkins/0de9b84ad9bb27d7df1dbacc020f4a05 to your computer and use it in GitHub Desktop.
Example of how to take search interest around multiple keywords and align it to stock activity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from time import sleep | |
from random import randint | |
from tqdm import notebook as tqdm | |
### Import Modules and Set Perimeters for Pytrends ### | |
from pytrends.request import TrendReq | |
### For Ticker Information | |
import yfinance as yf | |
### For Data Visualization ### | |
import plotly.graph_objects as go | |
from plotly.subplots import make_subplots | |
pytrends = TrendReq(hl='en-US', tz=360) | |
df = pd.read_excel('htz_stock.xlsx') #Contained 45 Relevant Search Terms | |
kw_list = df.Keyword.to_list() | |
### Establish Keyword List and Build Payload ### | |
frame = [] | |
for i in tqdm.tqdm(kw_list): | |
try: | |
pytrends.build_payload([i], cat=0, geo='US', gprop='') | |
interest = pytrends.get_historical_interest([i], year_start=2020, month_start=5, | |
day_start=10, hour_start=0, year_end=2020, | |
month_end=6, day_end=10, hour_end=0, cat=0, | |
geo='US', gprop='', sleep=20) | |
gr = interest | |
hold = gr.columns[0] | |
name = gr.columns[0] | |
gr = gr.rename(columns={name: 'Search Popularity'}) | |
gr = gr[['Search Popularity']] | |
gr['Term'] = hold | |
sleep(randint(5,20)) | |
frame.append(gr) | |
except: | |
pass | |
final = pd.concat(frame) | |
final.index = pd.MultiIndex.from_arrays([final.index.date, final.index.time], names=['Date','Time']) | |
pivot = final.pivot_table(index=['Date'], | |
values=['Search Popularity'], | |
aggfunc=np.mean) | |
htz = yf.Ticker("HTZ") | |
df_htz = pd.DataFrame(htz.history(period="1mo")) | |
fig = make_subplots(specs=[[{"secondary_y": True}]]) | |
fig.add_trace(go.Candlestick(x=df_htz.index, | |
open=df_htz['Open'], | |
high=df_htz['High'], | |
low=df_htz['Low'], | |
close=df_htz['Close'], | |
name='HTZ Stock'), | |
secondary_y=False) | |
fig.update(layout_xaxis_rangeslider_visible=False) | |
fig.add_trace(go.Scatter(x=pivot.index, y=pivot['Search Popularity'], | |
name='Search Interest', | |
line=dict(color='royalblue')), | |
secondary_y=True) | |
fig.update_layout( | |
title='Hertz Market Activity vs. Retail Investor Search Interest', | |
yaxis_title='HTZ Stock', | |
yaxis_tickprefix = '$', yaxis_tickformat = ',.', | |
shapes = [dict( | |
x0='2020-05-22', x1='2020-05-22', y0=0, y1=1, xref='x', yref='paper', | |
line_width=2)], | |
annotations=[dict( | |
x='2020-05-22', y=0.05, xref='x', yref='paper', | |
showarrow=False, xanchor='right', text='<b>Hertz Files for Bankruptcy</b>')] | |
) | |
# Set x-axis title | |
fig.update_xaxes(title_text="Date") | |
# Set y-axes titles | |
fig.update_yaxes(title_text="HTZ Stock", secondary_y=False) | |
fig.update_yaxes(title_text="Search Interest", secondary_y=True) | |
fig.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment