-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstock_dash.py
327 lines (272 loc) · 11.4 KB
/
stock_dash.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
##############################################################
#
# 500 Days of AAPL data used to predict 50 days in the future
#
#-------------------------------------------------------------
# Author : Dhruv Oberoi
# Dash App for Stock Price Prediction using
# Linear Regression, Ridge Regression, Lasso, SVR (RBF Kernel),
# Random Forest and XGBoost
#-------------------------------------------------------------
# Next Iterations:
#
# Fix date issue ; Dates are pushed forward 50 days
# Reduce overuse of df's
# Introduce range slider for date to alter training dataset
# Print and alter based on results from GridSearchCV
#
##############################################################
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, Event
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
from plotly import tools
import matplotlib.pyplot as plt
import datetime
import csv
import quandl, math
import numpy as np
import pandas as pd
import datetime
from sklearn import preprocessing, cross_validation, svm
from sklearn.svm import SVR
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import DotProduct, WhiteKernel
from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
from sklearn.linear_model import Ridge
from sklearn.linear_model import Lasso
from sklearn.linear_model import ARDRegression
from sklearn.decomposition import TruncatedSVD
from sklearn.random_projection import sparse_random_matrix
from matplotlib import style
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
#import plotly
#plotly.tools.set_credentials_file(username='dhruv.oberoi', api_key='API KEY')
quandl.ApiConfig.api_key = 'PASTE YOUR API KEY HERE'
df = quandl.get("EOD/AAPL")
###########################################################################
dataset = df[['Adj_Open','Adj_High', 'Adj_Low', 'Adj_Close', 'Adj_Volume']].copy()
dataset = dataset.iloc[pd.np.r_[:,-501:-1]]
dataset['HL_PCT'] = (dataset['Adj_High'] - dataset['Adj_Low']) / dataset['Adj_Low'] * 100.0
dataset['PCT_change'] = (dataset['Adj_Close'] - dataset['Adj_Open']) / dataset['Adj_Open'] * 100.0
dataset = dataset[['Adj_Close', 'HL_PCT', 'PCT_change', 'Adj_Volume']]
pred_feature = 'Adj_Close'
dataset.fillna(value=99999, inplace=True)
no_of_var = int(math.ceil(0.1 * len(dataset)))
dataset['label'] = dataset[pred_feature].shift(-no_of_var)
x = np.array(dataset.drop(['label'], 1))
x = preprocessing.scale(x)
x_small = x[-no_of_var:]
x_small = x[-no_of_var:]
x = x[:-no_of_var]
dataset.dropna(inplace=True)
y = np.array(dataset['label'])
x_train, x_test, y_train, y_test = cross_validation.train_test_split(x, y, test_size=0.2)
dataset_2 = df[['Adj_Open','Adj_High', 'Adj_Low', 'Adj_Close', 'Adj_Volume']].copy()
dataset_2 = dataset_2.iloc[pd.np.r_[:,-51:-1]]
df = df.iloc[pd.np.r_[:,-801:-1]]
###########################################################################
last_date = df.iloc[-1].name
#name of last date
last_unix = last_date.timestamp()
one_day = 86400
#number of seconds in a day
next_unix = last_unix + one_day
#next day...
###########################################################################
###########################################################################
###########################################################################
###########################################################################
###########################################################################
###########################################################################
model1 = svm.LinearSVR()
model1.fit(x_train, y_train)
confidence1 = model1.score(x_test, y_test)
predict_1 = model1.predict(x_small)
dataset['Predict_Linear'] = np.nan
print('Score for Linear Reg: :',confidence1)
print('\n')
for i in predict_1:
next_date = datetime.datetime.fromtimestamp(next_unix)
next_unix += one_day
dataset.loc[next_date] = [np.nan for _ in range(len(dataset.columns)-1)]+[i]
####################################################################################
model2 = svm.SVR(kernel = 'rbf', C= 100, gamma= 0.06)
model2.fit(x_train, y_train)
confidence2 = model2.score(x_test, y_test)
predict_2 = model2.predict(x_small)
dataset['Predict_RBF'] = np.nan
print('Score for RBF Reg: :',confidence2)
print('\n')
for i in predict_2:
next_date = datetime.datetime.fromtimestamp(next_unix)
next_unix += one_day
dataset.loc[next_date] = [np.nan for _ in range(len(dataset.columns)-1)]+[i]
####################################################################################
model5 = RandomForestRegressor(n_estimators = 150, random_state = 0)
model5.fit(x_train, y_train)
confidence5 = model5.score(x_test, y_test)
predict_5 = model5.predict(x_small)
dataset['Predict_RF'] = np.nan
print('Score for RF Reg: :',confidence5)
print('\n')
for i in (predict_5):
next_date = datetime.datetime.fromtimestamp(next_unix)
next_unix += one_day
dataset.loc[next_date] = [np.nan for _ in range(len(dataset.columns)-1)]+[i]
####################################################################################
model6 = XGBRegressor(max_depth=1, learning_rate=0.05, n_estimators=200,
objective="reg:linear", booster='gbtree', n_jobs=-1,
nthread=None, gamma=0, min_child_weight=1, max_delta_step=0,
subsample=1, colsample_bytree=1, random_state=0)
model6.fit(x_train, y_train)
confidence6 = model6.score(x_test, y_test)
predict_6 = model6.predict(x_small)
dataset['Predict_XGBR'] = np.nan
print('Score for XGBR Reg: :',confidence6)
print('\n')
for i in (predict_6):
next_date = datetime.datetime.fromtimestamp(next_unix)
next_unix += one_day
dataset.loc[next_date] = [np.nan for _ in range(len(dataset.columns)-1)]+[i]
####################################################################################
model7 = Ridge(alpha=1, fit_intercept = True, tol = 0.001, random_state = 0, solver = 'saga')
model7.fit(x_train, y_train)
confidence7 = model7.score(x_test, y_test)
predict_7 = model7.predict(x_small)
dataset['Predict_Ridge'] = np.nan
print('Score for Ridge Reg: :',confidence7)
print('\n')
for i in (predict_7):
next_date = datetime.datetime.fromtimestamp(next_unix)
next_unix += one_day
dataset.loc[next_date] = [np.nan for _ in range(len(dataset.columns)-1)]+[i]
####################################################################################
model8 = Lasso(alpha=0.1,fit_intercept = True, tol = 0.01, random_state = 0, selection = 'cyclic')
model8.fit(x_train, y_train)
confidence8 = model8.score(x_test, y_test)
predict_8 = model8.predict(x_small)
dataset['Predict_Lasso'] = np.nan
print('Score for Lasso Reg: :',confidence8)
print('\n')
for i in (predict_8):
next_date = datetime.datetime.fromtimestamp(next_unix)
next_unix += one_day
dataset.loc[next_date] = [np.nan for _ in range(len(dataset.columns)-1)]+[i]
###########################################################################
###########################################################################
###########################################################################
app = dash.Dash(__name__)
layout = go.Layout(
yaxis=dict(
domain=[1, 1]
),
legend=dict(
traceorder='reversed'
),
yaxis2=dict(
domain=[1, 1]
),
yaxis3=dict(
domain=[1, 1]
)
)
fig = go.Figure(layout = layout)
###########################################################################
###########################################################################
###########################################################################
all_options = {
'Linear Regression': ['lin'],
'Ridge Regression': ['rid'],
'Lasso Regression': ['las'],
'Random Forest Regression': ['rf'],
'XGBOOST Regression': ['xgb'],
'SVR Regression': ['rbf']
}
app.layout = html.Div([
html.Hr(),
dcc.Dropdown(
id='models-dropdown',
options=[{'label': k, 'value': k} for k in all_options.keys()],
value = 'Linear Regression',
multi = False),
html.Hr(),
html.Div(id='show-live'),
html.Div([dcc.Graph(id='plot', figure = fig)]),
dcc.Interval(id='interval-component', interval=1*10000) # in milliseconds
])
###########################################################################
###########################################################################
@app.callback(
dash.dependencies.Output(component_id = 'plot', component_property='figure'),
[dash.dependencies.Input(component_id = 'models-dropdown', component_property = 'value')],
events=[Event('interval-component', 'interval')]
)
def update_val(value):
trace1 = go.Scatter(x=df.index,y=df.Adj_Close)
# trace2 = go.Scatter(x=dataset.index,y=model_sel2)
# fig = go.Figure(data=[trace1,trace2],layout = layout)
if value == 'Ridge Regression':
print('{} is working'.format(value))
print("\n")
model_sel2 = dataset.Predict_Ridge
trace2 = go.Scatter(x=dataset.index,y=model_sel2)
fig = go.Figure(data=[trace1,trace2],layout = layout)
print(model_sel2.tail(2))
elif value == 'Lasso Regression':
print('{} is working'.format(value))
print("\n")
model_sel2 = dataset.Predict_Lasso
trace2 = go.Scatter(x=dataset.index,y=model_sel2)
fig = go.Figure(data=[trace1,trace2],layout = layout)
print(model_sel2.tail(2))
elif value == 'XGBOOST Regression':
print('{} is working'.format(value))
print("\n")
model_sel2 = dataset.Predict_XGBR
trace2 = go.Scatter(x=dataset.index,y=model_sel2)
fig = go.Figure(data=[trace1,trace2],layout = layout)
print(model_sel2.tail(2))
elif value == 'Random Forest Regression':
print('{} is working'.format(value))
print("\n")
model_sel2 = dataset.Predict_RF
trace2 = go.Scatter(x=dataset.index,y=model_sel2)
fig = go.Figure(data=[trace1,trace2],layout = layout)
print(model_sel2.tail(2))
elif value == 'SVR Regression':
print('{} is working'.format(value))
print("\n")
model_sel2 = dataset.Predict_RBF
trace2 = go.Scatter(x=dataset.index,y=model_sel2)
fig = go.Figure(data=[trace1,trace2],layout = layout)
print(model_sel2.tail(2))
else:
print('{} is working'.format(value))
print("\n")
model_sel2 = dataset.Predict_Linear
trace2 = go.Scatter(x=dataset.index,y=model_sel2)
fig = go.Figure(data=[trace1,trace2],layout = layout)
print(model_sel2.tail(1))
return fig
###########################################################################
@app.callback(dash.dependencies.Output(component_id = 'show-live', component_property='children'),
[dash.dependencies.Input(component_id = 'models-dropdown', component_property = 'value')],)
def set_display_children(modelsdropdown):
if modelsdropdown is None:
return ''
else:
return '{} has been selected-ish'.format(modelsdropdown)
###########################################################################
if __name__ == "__main__":
app.run_server(debug=True)
###########################################################################
###########################################################################