forked from lambda-stockly/build-stockly-DS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
186 lines (135 loc) · 5.57 KB
/
app.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from flask import Flask , request
import pandas as pd
import numpy as np
import json
import pickle
from sklearn.preprocessing import StandardScaler
import os
import requests
from google.cloud import language_v1
from google.cloud.language_v1 import enums
import six
from os import getenv
from bs4 import BeautifulSoup
from dotenv import load_dotenv
from sentiment import TwitterSentiment
load_dotenv()
def sample_analyze_sentiment(content):
client = language_v1.LanguageServiceClient()
#content = 'Your text to analyze, e.g. Hello, world!'
if isinstance(content, six.binary_type):
content = content.decode('utf-8')
type_ = enums.Document.Type.PLAIN_TEXT
document = {'type': type_, 'content': content}
response = client.analyze_sentiment(document)
sentiment = response.document_sentiment
#print('Score: {}'.format(sentiment.score))
#print('Magnitude: {}'.format(sentiment.magnitude))
return sentiment.score
def get_news(ticker):
url = "http://finance.yahoo.com/rss/headline?s="
feed = requests.get(url + ticker)
soup = BeautifulSoup(feed.text)
news = soup.find_all('description')
text = []
scores =[]
for new in news:
text.append(new.text)
for new in text[1:10]:
score = sample_analyze_sentiment(new)
scores.append(score)
df = pd.DataFrame(text[1:10])
df['score']=scores
return df
APP = Flask(__name__)
model = pickle.load( open( "model.p", "rb" ) )
#sentiment_model = TwitterSentiment("TSLA")
#twitter_sentiment = sentiment_model.output_twitter()
#print(twitter_sentiment)
def generate_df(ticker):
macd = 'https://www.alphavantage.co/query?function=MACD&symbol=' + ticker + '&interval=daily&series_type=open&apikey=SXG08DL4S2EW8SKC'
response1 = requests.get(macd)
if "Note" in response1.json().keys():
return (response1.json()["Note"])
df_macd = pd.DataFrame.from_dict(response1.json()['Technical Analysis: MACD']).T
stoch = 'https://www.alphavantage.co/query?function=STOCH&symbol=' + ticker + '&interval=daily&apikey=SXG08DL4S2EW8SKC'
response2 = requests.get(stoch)
if "Note" in response2.json().keys():
return (response2.json()["Note"])
df_stoch = pd.DataFrame.from_dict(response2.json()['Technical Analysis: STOCH']).T
# rsi = 'https://www.alphavantage.co/query?function=RSI&symbol='+ticker+'&interval=daily&time_period=10&series_type=open&apikey=NXAA2P2XI1GQSYPG'
# response3 = requests.get(rsi)
# df_rsi = pd.DataFrame.from_dict(response3.json()['Technical Analysis: RSI']).T
aroon = 'https://www.alphavantage.co/query?function=AROONOSC&symbol=' + ticker + '&interval=daily&time_period=10&apikey=SXG08DL4S2EW8SKC'
response4 = requests.get(aroon)
if "Note" in response4.json().keys():
return (response4.json()["Note"])
df_aroon = pd.DataFrame.from_dict(response4.json()['Technical Analysis: AROONOSC']).T
dx = 'https://www.alphavantage.co/query?function=DX&symbol=' + ticker + '&interval=daily&time_period=10&apikey=SXG08DL4S2EW8SKC'
response5 = requests.get(dx)
if "Note" in response5.json().keys():
return (response5.json()["Note"])
df_dx = pd.DataFrame.from_dict(response5.json()['Technical Analysis: DX']).T
url = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=' + ticker + '&interval=5min&outputsize=full&apikey=SXG08DL4S2EW8SKC'
response6 = requests.get(url)
if "Note" in response6.json().keys():
return (response6.json()["Note"])
df = pd.DataFrame.from_dict(response6.json()['Time Series (Daily)']).T
# Join all the dataset
df = df.join(df_macd)
df = df.join(df_stoch)
# df = df.join(df_rsi)
df = df.join(df_aroon)
df = df.join(df_dx)
return df
def signal_class(p):
if p>=0.05:
r = 1
elif p<=-0.05:
r = -1
else:
r=0
return r
def generate_target(df):
df2 =df.copy()
df2 = df.astype(float)
df2['next_10day_close']=df2['4. close'].shift(10)
df2['percentage_change']=(df2['next_10day_close']-df2['4. close'])/df2['4. close']
df2['signal']=df2['percentage_change'].apply(signal_class)
return df2
@APP.route('/')
@APP.route('/api',methods=['POST'])
def recommendation():
input ="AAPL"
if request.method == 'POST':
input = request.values['ticker']
market_df = generate_df(input)
if type(market_df)==str:
return market_df
market_df = market_df.dropna()
X = market_df[['5. volume', 'MACD', 'AROONOSC','MACD_Hist', 'MACD_Signal', 'DX', 'SlowD', 'SlowK']]
sc = StandardScaler()
X = sc.fit_transform(X)
y_prebro = model.predict_proba(X[0].reshape(1, -1))
sentiment_model = TwitterSentiment(input)
twitter_sentiment = sentiment_model.output_twitter()
# print(twitter_sentiment)
dict1 = {'TA': {'sell':y_prebro[0][0],'hold':y_prebro[0][1],'buy':y_prebro[0][2]}, 'Sentiment':{'sell':twitter_sentiment['Sell'],'hold':twitter_sentiment['Hold'],'buy':twitter_sentiment['Buy']}}
json1 = json.dumps(dict1)
response=json1
print(response)
return response
@APP.route('/sentiment')
@APP.route('/sentiment',methods=['POST'])
def sentiment():
input ="AAPL"
if request.method == 'POST':
input = request.values['ticker']
market_df = generate_df(input)
if type(market_df)==str:
return market_df
#json1 = json.dumps(dict1)
score=sample_analyze_sentiment("The bad news seems to keep on coming for Tesla Inc., and one expert says this is the year the electric-car company “comes undone” — and maybe gets bought by Apple Inc.")
json1 = json.dumps({"sentiment":score})
response=json1
return response