-
Notifications
You must be signed in to change notification settings - Fork 390
/
stock_prediction_sms.py
236 lines (172 loc) · 7.03 KB
/
stock_prediction_sms.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import numpy as np
import datetime
import smtplib
import os
from selenium import webdriver
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import preprocessing
from pandas_datareader import DataReader
from yahoo_fin import stock_info as si
import pandas as pd
from pypfopt.efficient_frontier import EfficientFrontier
from pypfopt import risk_models
from pypfopt import expected_returns
pd.set_option('display.max_rows', None)
def getStocks(n):
driver = webdriver.Chrome(executable_path=r'/Users/shashank/Downloads/chromedriver')
url = "https://finance.yahoo.com/screener/predefined/aggressive_small_caps?offset=0&count=202"
driver.get(url)
stock_list = []
n += 1
for i in range(1, n):
driver.implicitly_wait(10)
ticker = driver.find_element_by_xpath('//*[@id = "scr-res-table"]/div[1]/table/tbody/tr[' + str(i) + ']/td[1]/a')
stock_list.append(ticker.text)
number = 0
for i in stock_list:
predictData(i, 5)
number += 1
def sendMessage(text):
message = text
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
email = "[email protected]"
pas = "fantasyforlife3"
sms_gateway = '[email protected]'
smtp = "smtp.gmail.com"
port = 587
server = smtplib.SMTP(smtp,port)
server.starttls()
server.login(email,pas)
msg = MIMEMultipart()
msg['From'] = email
msg['To'] = sms_gateway
msg['Subject'] = "Stocks\n"
body = "{}\n".format(message)
msg.attach(MIMEText(body, 'plain'))
sms = msg.as_string()
server.sendmail(email,sms_gateway,sms)
server.quit()
print ('sent')
stock_list = []
predictions = []
confidence = []
error_list = []
def predictData(stock, days):
stock_list.append(stock)
start = datetime.datetime.now() - datetime.timedelta(days=365)
end = datetime.datetime.now()
df = DataReader(stock, 'yahoo', start, end)
if os.path.exists('./Exports'):
csv_name = ('Exports/' + stock + '_Export.csv')
else:
os.mkdir("Exports")
csv_name = ('Exports/' + stock + '_Export.csv')
df.to_csv(csv_name)
df['Prediction'] = df['Close'].shift(-1)
df.dropna(inplace=True)
forecast_time = int(days)
df['Prediction'] = df['Close'].shift(-1)
df1 = df['Prediction']
array = np.array(df['Close'])
array1 = np.array(df1)
array = array.reshape(-1, 1)
array1 = array1.reshape(-1, 1)
X = array
Y = array1
X = np.nan_to_num(X)
Y = np.nan_to_num(Y)
X = preprocessing.scale(X)
X_prediction = X[-forecast_time:]
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, train_size = 0.8, test_size=0.2)
clf = LinearRegression()
clf.fit(X_train, Y_train)
prediction = (clf.predict(X_prediction))
prediction = np.around(prediction, decimals = 3)
print (stock)
last_row = df.tail(1)
last_row = last_row.reset_index()
last_row = last_row['Close']
last_row = last_row.to_string(index=False)
print('Close: {}'.format(last_row))
print ('-'*80)
lr = LinearRegression()
lr.fit(X_train, Y_train)
lr_confidence = lr.score(X_test, Y_test)
lr_confidence = round(lr_confidence, 2)
# price
price = si.get_live_price('{}'.format(stock))
price = round(price, 2)
# volatility, momentum, beta, alpha, r_squared
df = DataReader(stock,'yahoo',start, end)
dfb = DataReader('^GSPC','yahoo',start, end)
rts = df.resample('M').last()
rbts = dfb.resample('M').last()
dfsm = pd.DataFrame({'s_adjclose' : rts['Adj Close'],
'b_adjclose' : rbts['Adj Close']},
index=rts.index)
dfsm[['s_returns','b_returns']] = dfsm[['s_adjclose','b_adjclose']]/\
dfsm[['s_adjclose','b_adjclose']].shift(1) -1
dfsm = dfsm.dropna()
covmat = np.cov(dfsm["s_returns"],dfsm["b_returns"])
beta = covmat[0,1]/covmat[1,1]
alpha= np.mean(dfsm["s_returns"])-beta*np.mean(dfsm["b_returns"])
ypred = alpha + beta * dfsm["b_returns"]
SS_res = np.sum(np.power(ypred-dfsm["s_returns"],2))
SS_tot = covmat[0,0]*(len(dfsm)-1) # SS_tot is sample_variance*(n-1)
r_squared = 1. - SS_res/SS_tot
volatility = np.sqrt(covmat[0,0])
momentum = np.prod(1+dfsm["s_returns"].tail(12).values) -1
prd = 12.
alpha = alpha*prd
volatility = volatility*np.sqrt(prd)
beta = round(beta, 2)
alpha = round(alpha, 2)
r_squared = round(r_squared, 2)
volatility = round(volatility, 2)
momentum = round(momentum, 2)
# Sharpe Ratio
x = 5000
y = (x)
stock_df = df
stock_df['Norm return'] = stock_df['Adj Close'] / stock_df.iloc[0]['Adj Close']
allocation = float(x/y)
stock_df['Allocation'] = stock_df['Norm return'] * allocation
stock_df['Position'] = stock_df['Allocation'] * x
pos = [df['Position']]
val = pd.concat(pos, axis=1)
val.columns = ['WMT Pos']
val['Total Pos'] = val.sum(axis=1)
val.tail(1)
val['Daily Return'] = val['Total Pos'].pct_change(1)
Sharpe_Ratio = val['Daily Return'].mean() / val['Daily Return'].std()
A_Sharpe_Ratio = (252**0.5) * Sharpe_Ratio
A_Sharpe_Ratio = round(A_Sharpe_Ratio, 2)
difference = float(prediction[4]) - float(last_row)
change = float(difference)/float(last_row)
predictions.append(change)
confidence.append(lr_confidence)
error = 1 - float(lr_confidence)
error_list.append(error)
if (float(prediction[4]) > (float(last_row)) and (float(lr_confidence)) > 0.8):
output = ("\nStock: " + str(stock) + "\nLast Close: " + str(last_row) + "\nPrediction in 1 Day: " + str(prediction[0]) + "\nPrediction in 5 Days: " + str(prediction[4]) + "\nConfidence: " + str(lr_confidence) + "\nCurrent Price : " + str(price) + "\n\nStock Data: " + "\nBeta: " + str(beta) + "\nAlpha: " + str(alpha) + "\nSharpe Ratio: " + str(A_Sharpe_Ratio) + "\nVolatility: " + str(volatility) + "\nMomentum: " + str(momentum))
sendMessage(output)
if __name__ == '__main__':
getStocks(40)
'''
combined = []
for i in range(0, len(predictions)):
combined.append(predictions[i] - error_list[i])
# Create a dataframe with each company and their corressponding beta/alpha values
dataframe = pd.DataFrame(list(zip(stock_list, predictions, error_list, combined)), columns =['Company', 'Prediction', 'Error', 'Combined'])
# Sorting the dataframe from highest sharpe values to lowest
df = dataframe.sort_values('Combined', ascending = False)
df = df.drop(df.columns[df.columns.str.contains('unnamed',case = False)],axis = 1, inplace = True)
df = dataframe.dropna()
df.to_csv(r'/Users/shashank/Downloads/portfolio/predictions.csv')
df = pd.read_csv('/Users/shashank/Downloads/portfolio/predictions.csv', index_col=0)
df = df.set_index(['Company'])
df = df.sort_values('Prediction', ascending = False)
print (df)
'''