-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock_app.py
executable file
·294 lines (249 loc) · 8.92 KB
/
stock_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
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
"""
This file demonstrates a bokeh applet, which can either be viewed
directly on a bokeh-server, or embedded into a flask application.
See the README.md file in this directory for instructions on running.
"""
import logging
logging.basicConfig(level=logging.DEBUG)
from os import listdir
from os.path import dirname, join, splitext
import numpy as np
import pandas as pd
from bokeh.models import ColumnDataSource, Plot
from bokeh.plotting import figure, curdoc
from bokeh.properties import String, Instance
from bokeh.server.app import bokeh_app
from bokeh.server.utils.plugins import object_page
from bokeh.models.widgets import HBox, VBox, VBoxForm, PreText, Select
import statsmodels.api as sm
# Generate some data from an ARMA process
from statsmodels.tsa.arima_process import arma_generate_sample
# build up list of stock data in the daily folder
data_dir = join(dirname(__file__), "daily")
try:
tickers = listdir(data_dir)
except OSError as e:
print('Stock data not available, see README for download instructions.')
raise e
tickers = [splitext(x)[0].split("table_")[-1] for x in tickers]
# cache stock data as dict of pandas DataFrames
pd_cache = {}
def get_ticker_data(ticker):
fname = join(data_dir, "table_%s.csv" % ticker.lower())
data = pd.read_csv(
fname,
names=['date', 'foo', 'open', 'high', 'low', 'c', 'volume'],
header=False,
parse_dates=['date']
)
data = data.set_index('date')
data = pd.DataFrame({ticker: data.c, ticker + "_returns": data.c.diff()})
return data
def get_ticker_data_2(ticker1,ticker):
fname = join(data_dir, "table_%s.csv" % ticker1.lower())
data = pd.read_csv(
fname,
names=['date', 'foo', 'open', 'high', 'low', 'c', 'volume'],
header=False,
parse_dates=['date']
)
data = data.set_index('date')
if (ticker=="Arma"):
arma =sm.tsa.ARMA(data['c'].values,order=(3,0))
arma_res=arma.fit()
result=arma_res.predict()
data['c']=result
data =pd.DataFrame({ticker: data.c, ticker+ "_returns": data.c.diff()})
return data
if (ticker=="Arima"):
arma =sm.tsa.ARIMA(data['c'].values,order=(3,0,0))
arma_res=arma.fit()
result=arma_res.predict()
data['c']=result
data =pd.DataFrame({ticker: data.c, ticker+ "_returns": data.c.diff()})
return data
if(ticker=="Anfis"):
a=pd.DataFrame.from_csv("/home/elisha/stock_applet/lstmdata/"+ticker1+".csv")
a=a.ix[1:]
a.index=data.index
a.columns=['c']
data=pd.DataFrame({ticker: a.c, ticker + "_returns": a.c.diff()})
return data
def get_data(ticker1, ticker2):
if pd_cache.get((ticker1, ticker2)) is not None:
return pd_cache.get((ticker1, ticker2))
data1 = get_ticker_data(ticker1)
data2 = get_ticker_data_2(ticker1,ticker2)
data = pd.concat([data1, data2], axis=1)
data = data.dropna()
pd_cache[(ticker1, ticker2)] = data
return data
class StockApp(VBox):
extra_generated_classes = [["StockApp", "StockApp", "VBox"]]
jsmodel = "VBox"
# text statistics
pretext = Instance(PreText)
# plots
plot = Instance(Plot)
line_plot1 = Instance(Plot)
line_plot2 = Instance(Plot)
hist1 = Instance(Plot)
hist2 = Instance(Plot)
# data source
source = Instance(ColumnDataSource)
# layout boxes
mainrow = Instance(HBox)
histrow = Instance(HBox)
statsbox = Instance(VBox)
# inputs
ticker1 = String(default="A")
ticker2 = String(default="Arima")
ticker1_select = Instance(Select)
ticker2_select = Instance(Select)
input_box = Instance(VBoxForm)
def __init__(self, *args, **kwargs):
super(StockApp, self).__init__(*args, **kwargs)
self._dfs = {}
@classmethod
def create(cls):
"""
This function is called once, and is responsible for
creating all objects (plots, datasources, etc)
"""
# create layout widgets
obj = cls()
obj.mainrow = HBox()
obj.histrow = HBox()
obj.statsbox = VBox()
obj.input_box = VBoxForm()
# create input widgets
obj.make_inputs()
# outputs
obj.pretext = PreText(text="", width=500)
obj.make_source()
obj.make_plots()
obj.make_stats()
# layout
obj.set_children()
return obj
def make_inputs(self):
#
#please add any other stocks you have downloaded using stock_data.py
#
self.ticker1_select = Select(
name='ticker1',
value='Stock',
options=['A','AES','AN','BAX','BRCM','CCL','CMS', 'CTSH', 'DISCA', 'ECL', 'EW'])
self.ticker2_select = Select(
name='ticker2',
value='Algorithm',
options=['Arima','Arma','Anfis']
)
@property
def selected_df(self):
pandas_df = self.df
selected = self.source.selected['1d']['indices']
if selected:
pandas_df = pandas_df.iloc[selected, :]
return pandas_df
def make_source(self):
self.source = ColumnDataSource(data=self.df)
def line_plot(self, ticker, x_range=None,color="#01FDC1"):
p = figure(
title=ticker,
x_range=x_range,
x_axis_type='datetime',
plot_width=1000, plot_height=200,
title_text_font_size="10pt",
tools="pan,wheel_zoom,box_select,reset")
p.circle(
'date', ticker,
size=2,
source=self.source,
nonselection_alpha=0.02,color=color
)
return p
def hist_plot(self, ticker,color="#01FDC1"):
global_hist, global_bins = np.histogram(self.df[ticker + "_returns"], bins=50)
hist, bins = np.histogram(self.selected_df[ticker + "_returns"], bins=50)
width = 0.7 * (bins[1] - bins[0])
center = (bins[:-1] + bins[1:]) / 2
start = global_bins.min()
end = global_bins.max()
top = hist.max()
p = figure(
title="%s hist" % ticker,
plot_width=500, plot_height=200,
tools="",
title_text_font_size="10pt",
x_range=[start, end],
y_range=[0, top],
)
p.rect(center, hist / 2.0, width, hist,color=color)
return p
def make_plots(self):
ticker1 = self.ticker1
ticker2 = self.ticker2
p = figure(
title="%s vs %s" % (ticker1, ticker2),
plot_width=400, plot_height=400,
tools="pan,wheel_zoom,box_select,reset",
title_text_font_size="10pt",
)
p.asterisk(ticker1 + "_returns",ticker2 + "_returns",
size=2,color="olive",
nonselection_alpha=0.02,
source=self.source
)
self.plot = p
self.line_plot1 = self.line_plot(ticker1)
self.line_plot2 = self.line_plot(ticker2, self.line_plot1.x_range,"#FD0101")
self.hist_plots()
def hist_plots(self):
ticker1 = self.ticker1
ticker2 = self.ticker2
self.hist1 = self.hist_plot(ticker1)
self.hist2 = self.hist_plot(ticker2,"#FD0101")
def set_children(self):
self.children = [self.mainrow, self.histrow, self.line_plot1, self.line_plot2]
self.mainrow.children = [self.input_box, self.plot, self.statsbox]
self.input_box.children = [self.ticker1_select, self.ticker2_select]
self.histrow.children = [self.hist1, self.hist2]
self.statsbox.children = [self.pretext]
def input_change(self, obj, attrname, old, new):
if obj == self.ticker2_select:
self.ticker2 = new
if obj == self.ticker1_select:
self.ticker1 = new
self.make_source()
self.make_plots()
self.set_children()
curdoc().add(self)
def setup_events(self):
super(StockApp, self).setup_events()
if self.source:
self.source.on_change('selected', self, 'selection_change')
if self.ticker1_select:
self.ticker1_select.on_change('value', self, 'input_change')
if self.ticker2_select:
self.ticker2_select.on_change('value', self, 'input_change')
def make_stats(self):
stats = self.selected_df.describe()
self.pretext.text = str(stats)
def selection_change(self, obj, attrname, old, new):
self.make_stats()
self.hist_plots()
self.set_children()
curdoc().add(self)
@property
def df(self):
return get_data(self.ticker1, self.ticker2)
# The following code adds a "/bokeh/stocks/" url to the bokeh-server. This URL
# will render this StockApp. If you don't want serve this applet from a Bokeh
# server (for instance if you are embedding in a separate Flask application),
# then just remove this block of code.
@bokeh_app.route("/bokeh/stocks/")
@object_page("stocks")
def make_stocks():
app = StockApp.create()
return app