Skip to content

Instantly share code, notes, and snippets.

@x213212
Created November 29, 2018 14:14
import numpy as np
import pandas as pd
import csv
import matplotlib.pyplot as plt
import scipy.stats as scs
from matplotlib import animation
from sklearn import preprocessing
from pytrends.request import TrendReq
import datetime
date_time_str = '2018-06-29'
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d')
df = pd.read_csv('googletrend_stock_data.csv', index_col='date', parse_dates=['date'])
price = df.squeeze()
price.head()
df = pd.read_csv('googletrend_stock_data2.csv', index_col='date', parse_dates=['date'])
all_price = df.squeeze()
all_price.head()
# print('Date:',date_time_str[5:7])
#Login to Google. Only need to run this once, the rest of requests will use the same session.
# pytrend = TrendReq()
# tk = pytrend.get_historical_interest2(['stock'], year_start=2004, month_start=1, day_start=1, year_end=2018, month_end=8, day_end=1, cat=0, geo='', gprop='', sleep=0)
# print(price)
# #tk.drop(['isPartial'], axis=1)
# #del tk['isPartial']
# print (tk.head())
# type(price)
def get_months( test):
value = ""
for i in range (0,len(all_price),1):
if(test == str(all_price.index[i])[:7]+"-01 00:00:00"):
value = all_price[str(all_price.index[i])[:7]+"-01 00:00:00"]
return value
plt.figure(figsize=(16,8))
fig, ax = plt.subplots()
#preprocessing.scale(tk['stock'])
ax.plot(price)
plt.show()
# print( str(price.index[0])[:7] )
# print (price [price.index[0]])
s = pd.Series(price)
fuck = []
print (price.index)
for x in price.index:
print (x)
get_month = get_months(str(x)[:7]+"-01 00:00:00" )
if(type(price[x]) == type(pd.Series(1))):
fuck.append( [str(x),(np.float64(price[x][0])*(int(get_month)/100))])
else:
fuck.append( ([str(x),(np.float64(price[x])*(int(get_month)/100))]))
#print (get_month/100)
#print (price[x]*(get_month/100))
#print (get_months(str(price.index[str(x)])[:7]+"-01 00:00:00"))
df2 = pd.DataFrame(fuck, columns = ['date', 'price'] )
df2['date'] = pd.to_datetime(df2['date'])
price2 = df2.set_index('date')
plt.figure(figsize=(16,8))
fig, ax = plt.subplots()
#preprocessing.scale(tk['stock'])
ax.plot(price2)
plt.gcf().autofmt_xdate()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment