-
Notifications
You must be signed in to change notification settings - Fork 1
/
interest.py
131 lines (108 loc) · 4.59 KB
/
interest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import pandas as pd
from pytrends.request import TrendReq
import seaborn as sns
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from pandas_datareader import data
import requests
import yfinance as yf
from pandas import DataFrame
#input... can be array but need flask functionality
#stock_ticker = ["MSFT","AAPL","WORK","AMD"]
#nothing
#stock_name = []
#for x in stock_ticker:
# tick = yf.Ticker(x)
# stock_name.append(tick.info['shortName'])
def get_searches(key_word):
pytrends = TrendReq(hl='en-US', tz=360)
pytrends.build_payload([key_word], cat=0, timeframe='2020-01-01 2021-01-15', gprop='',geo='')
df = pytrends.interest_over_time()
print(df.head())
sns.set()
df['timestamp'] = pd.to_datetime(df.index)
sns.lineplot(x=df['timestamp'], y=df[key_word])
plt.title("Normalized Searches for {}".format(key_word))
plt.ylabel("Number of Searches")
plt.xlabel("Date")
plt.savefig("template/static/images/search.png")
plt.close()
def get_finance(key_word):
df = yf.download(key_word, start='2020-01-01', end='2021-01-15')['Adj Close']
pd.plotting.register_matplotlib_converters()
# Load the data
# Set the style to seaborn for plotting
plt.style.use('seaborn')
fig, ax = plt.subplots(figsize=(12, 6))
# Plot the cumulative returns fot each symbol
ax.plot(df)
plt.title('Adjusted Close Price - {}'.format(key_word), fontsize=16)
# Define the labels for x-axis and y-axis
plt.ylabel('Adjusted Close Price', fontsize=14)
plt.xlabel('Date', fontsize=14)
plt.savefig('template/static/images/finance.png')
plt.close()
def prediction(key_word):
pytrends = TrendReq(hl='en-US', tz=360)
pytrends.build_payload([key_word], cat=0, timeframe='2021-01-01 2021-01-15', gprop='',geo='')
df = pytrends.interest_over_time()
std = pd.DataFrame.from_dict(df)
std['Moving Average'] = std[key_word].rolling(2).mean()
std[[key_word,'Moving Average']].plot(figsize=(10,4))
plt.grid(True)
plt.title(key_word +" Google Trends" ' Moving Averages')
plt.axis('tight')
plt.ylabel('Searches')
plt.savefig('template/static/images/prediction.png')
plt.close()
def actual_prediction(key_word):
pytrends = TrendReq(hl='en-US', tz=360)
pytrends.build_payload([key_word], cat=0, timeframe='2021-01-01 2021-01-15', gprop='',geo='')
df = pytrends.interest_over_time()
std = pd.DataFrame.from_dict(df)
std['Moving Average'] = std[key_word].rolling(2).mean()
close = float(std.loc['2021-01-15','Moving Average'])
l=[]
l.append(float(std.loc['2021-01-15','Moving Average']))
l.append(float(std.loc['2021-01-13','Moving Average']))
l.append(float(std.loc['2021-01-14','Moving Average']))
l.append(float(std.loc['2021-01-12','Moving Average']))
l.append(float(std.loc['2021-01-11','Moving Average']))
l.append(float(std.loc['2021-01-10','Moving Average']))
l.append(float(std.loc['2021-01-09','Moving Average']))
average =0
count = 0
for x in l:
average = average + x
count +=1
average = average /count
ender = (float(std.loc['2021-01-15','Moving Average'])/average)
print(ender)
if ender >1.10:
ender = ender*0.90
elif ender >1.15:
ender = ender *0.85
elif ender <1.00:
ender = ender *1.02
df = yf.download(key_word, start='2021-01-01', end='2021-01-16')['Adj Close']
eat = pd.DataFrame.from_dict(df)
fire = float(eat.loc['2021-01-15','Adj Close'])
print(fire)
fire = fire*ender
print(fire)
df1 = pd.DataFrame({"a":["01-04","01-05","01-06","01-07",
"01-08","01-11","01-12","01-13",
"01-14","01-15","01-22"],
"b":[float(eat.loc['2021-01-04','Adj Close']),float(eat.loc['2021-01-05','Adj Close']),
float(eat.loc['2021-01-06','Adj Close']),float(eat.loc['2021-01-07','Adj Close']),
float(eat.loc['2021-01-08','Adj Close']),float(eat.loc['2021-01-11','Adj Close']),
float(eat.loc['2021-01-12','Adj Close']),float(eat.loc['2021-01-13','Adj Close']),
float(eat.loc['2021-01-14','Adj Close']),float(eat.loc['2021-01-15','Adj Close']), fire]})
sns.set()
sns.lineplot(x=df1['a'], y=df1['b'])
plt.title("Predicted Stock Price {}".format(key_word))
plt.ylabel("Price")
plt.xlabel("Date")
plt.savefig('template/static/images/actual.png')
plt.close()