1
+ import dash
2
+ import dash_core_components as dcc
3
+ import dash_html_components as html
4
+ import pandas as pd
5
+ import plotly .graph_objs as go
6
+ from dash .dependencies import Input , Output
7
+ from keras .models import load_model
8
+ from sklearn .preprocessing import MinMaxScaler
9
+ import numpy as np
10
+
11
+
12
+ app = dash .Dash ()
13
+ server = app .server
14
+
15
+ scaler = MinMaxScaler (feature_range = (0 ,1 ))
16
+
17
+
18
+
19
+ df_nse = pd .read_csv ("./NSE-TATA.csv" )
20
+
21
+ df_nse ["Date" ]= pd .to_datetime (df_nse .Date ,format = "%Y-%m-%d" )
22
+ df_nse .index = df_nse ['Date' ]
23
+
24
+
25
+ data = df_nse .sort_index (ascending = True ,axis = 0 )
26
+ new_data = pd .DataFrame (index = range (0 ,len (df_nse )),columns = ['Date' ,'Close' ])
27
+
28
+ for i in range (0 ,len (data )):
29
+ new_data ["Date" ][i ]= data ['Date' ][i ]
30
+ new_data ["Close" ][i ]= data ["Close" ][i ]
31
+
32
+ new_data .index = new_data .Date
33
+ new_data .drop ("Date" ,axis = 1 ,inplace = True )
34
+
35
+ dataset = new_data .values
36
+
37
+ train = dataset [0 :987 ,:]
38
+ valid = dataset [987 :,:]
39
+
40
+ scaler = MinMaxScaler (feature_range = (0 ,1 ))
41
+ scaled_data = scaler .fit_transform (dataset )
42
+
43
+ x_train ,y_train = [],[]
44
+
45
+ for i in range (60 ,len (train )):
46
+ x_train .append (scaled_data [i - 60 :i ,0 ])
47
+ y_train .append (scaled_data [i ,0 ])
48
+
49
+ x_train ,y_train = np .array (x_train ),np .array (y_train )
50
+
51
+ x_train = np .reshape (x_train ,(x_train .shape [0 ],x_train .shape [1 ],1 ))
52
+
53
+ model = load_model ("saved_model.h5" )
54
+
55
+ inputs = new_data [len (new_data )- len (valid )- 60 :].values
56
+ inputs = inputs .reshape (- 1 ,1 )
57
+ inputs = scaler .transform (inputs )
58
+
59
+ X_test = []
60
+ for i in range (60 ,inputs .shape [0 ]):
61
+ X_test .append (inputs [i - 60 :i ,0 ])
62
+ X_test = np .array (X_test )
63
+
64
+ X_test = np .reshape (X_test ,(X_test .shape [0 ],X_test .shape [1 ],1 ))
65
+ closing_price = model .predict (X_test )
66
+ closing_price = scaler .inverse_transform (closing_price )
67
+
68
+ train = new_data [:987 ]
69
+ valid = new_data [987 :]
70
+ valid ['Predictions' ]= closing_price
71
+
72
+
73
+
74
+ df = pd .read_csv ("./stock_data.csv" )
75
+
76
+ app .layout = html .Div ([
77
+
78
+ html .H1 ("Stock Price Analysis Dashboard" , style = {"textAlign" : "center" }),
79
+
80
+ dcc .Tabs (id = "tabs" , children = [
81
+
82
+ dcc .Tab (label = 'NSE-TATAGLOBAL Stock Data' ,children = [
83
+ html .Div ([
84
+ html .H2 ("Actual closing price" ,style = {"textAlign" : "center" }),
85
+ dcc .Graph (
86
+ id = "Actual Data" ,
87
+ figure = {
88
+ "data" :[
89
+ go .Scatter (
90
+ x = train .index ,
91
+ y = valid ["Close" ],
92
+ mode = 'markers'
93
+ )
94
+
95
+ ],
96
+ "layout" :go .Layout (
97
+ title = 'scatter plot' ,
98
+ xaxis = {'title' :'Date' },
99
+ yaxis = {'title' :'Closing Rate' }
100
+ )
101
+ }
102
+
103
+ ),
104
+ html .H2 ("LSTM Predicted closing price" ,style = {"textAlign" : "center" }),
105
+ dcc .Graph (
106
+ id = "Predicted Data" ,
107
+ figure = {
108
+ "data" :[
109
+ go .Scatter (
110
+ x = valid .index ,
111
+ y = valid ["Predictions" ],
112
+ mode = 'markers'
113
+ )
114
+
115
+ ],
116
+ "layout" :go .Layout (
117
+ title = 'scatter plot' ,
118
+ xaxis = {'title' :'Date' },
119
+ yaxis = {'title' :'Closing Rate' }
120
+ )
121
+ }
122
+
123
+ )
124
+ ])
125
+
126
+
127
+ ]),
128
+ dcc .Tab (label = 'Facebook Stock Data' , children = [
129
+ html .Div ([
130
+ html .H1 ("Stocks High vs Lows" ,
131
+ style = {'textAlign' : 'center' }),
132
+
133
+ dcc .Dropdown (id = 'my-dropdown' ,
134
+ options = [{'label' : 'Tesla' , 'value' : 'TSLA' },
135
+ {'label' : 'Apple' ,'value' : 'AAPL' },
136
+ {'label' : 'Facebook' , 'value' : 'FB' },
137
+ {'label' : 'Microsoft' ,'value' : 'MSFT' }],
138
+ multi = True ,value = ['FB' ],
139
+ style = {"display" : "block" , "margin-left" : "auto" ,
140
+ "margin-right" : "auto" , "width" : "60%" }),
141
+ dcc .Graph (id = 'highlow' ),
142
+ html .H1 ("Stocks Market Volume" , style = {'textAlign' : 'center' }),
143
+
144
+ dcc .Dropdown (id = 'my-dropdown2' ,
145
+ options = [{'label' : 'Tesla' , 'value' : 'TSLA' },
146
+ {'label' : 'Apple' ,'value' : 'AAPL' },
147
+ {'label' : 'Facebook' , 'value' : 'FB' },
148
+ {'label' : 'Microsoft' ,'value' : 'MSFT' }],
149
+ multi = True ,value = ['FB' ],
150
+ style = {"display" : "block" , "margin-left" : "auto" ,
151
+ "margin-right" : "auto" , "width" : "60%" }),
152
+ dcc .Graph (id = 'volume' )
153
+ ], className = "container" ),
154
+ ])
155
+
156
+
157
+ ])
158
+ ])
159
+
160
+
161
+
162
+
163
+
164
+
165
+
166
+ @app .callback (Output ('highlow' , 'figure' ),
167
+ [Input ('my-dropdown' , 'value' )])
168
+ def update_graph (selected_dropdown ):
169
+ dropdown = {"TSLA" : "Tesla" ,"AAPL" : "Apple" ,"FB" : "Facebook" ,"MSFT" : "Microsoft" ,}
170
+ trace1 = []
171
+ trace2 = []
172
+ for stock in selected_dropdown :
173
+ trace1 .append (
174
+ go .Scatter (x = df [df ["Stock" ] == stock ]["Date" ],
175
+ y = df [df ["Stock" ] == stock ]["High" ],
176
+ mode = 'lines' , opacity = 0.7 ,
177
+ name = f'High { dropdown [stock ]} ' ,textposition = 'bottom center' ))
178
+ trace2 .append (
179
+ go .Scatter (x = df [df ["Stock" ] == stock ]["Date" ],
180
+ y = df [df ["Stock" ] == stock ]["Low" ],
181
+ mode = 'lines' , opacity = 0.6 ,
182
+ name = f'Low { dropdown [stock ]} ' ,textposition = 'bottom center' ))
183
+ traces = [trace1 , trace2 ]
184
+ data = [val for sublist in traces for val in sublist ]
185
+ figure = {'data' : data ,
186
+ 'layout' : go .Layout (colorway = ["#5E0DAC" , '#FF4F00' , '#375CB1' ,
187
+ '#FF7400' , '#FFF400' , '#FF0056' ],
188
+ height = 600 ,
189
+ title = f"High and Low Prices for { ', ' .join (str (dropdown [i ]) for i in selected_dropdown )} Over Time" ,
190
+ xaxis = {"title" :"Date" ,
191
+ 'rangeselector' : {'buttons' : list ([{'count' : 1 , 'label' : '1M' ,
192
+ 'step' : 'month' ,
193
+ 'stepmode' : 'backward' },
194
+ {'count' : 6 , 'label' : '6M' ,
195
+ 'step' : 'month' ,
196
+ 'stepmode' : 'backward' },
197
+ {'step' : 'all' }])},
198
+ 'rangeslider' : {'visible' : True }, 'type' : 'date' },
199
+ yaxis = {"title" :"Price (USD)" })}
200
+ return figure
201
+
202
+
203
+ @app .callback (Output ('volume' , 'figure' ),
204
+ [Input ('my-dropdown2' , 'value' )])
205
+ def update_graph (selected_dropdown_value ):
206
+ dropdown = {"TSLA" : "Tesla" ,"AAPL" : "Apple" ,"FB" : "Facebook" ,"MSFT" : "Microsoft" ,}
207
+ trace1 = []
208
+ for stock in selected_dropdown_value :
209
+ trace1 .append (
210
+ go .Scatter (x = df [df ["Stock" ] == stock ]["Date" ],
211
+ y = df [df ["Stock" ] == stock ]["Volume" ],
212
+ mode = 'lines' , opacity = 0.7 ,
213
+ name = f'Volume { dropdown [stock ]} ' , textposition = 'bottom center' ))
214
+ traces = [trace1 ]
215
+ data = [val for sublist in traces for val in sublist ]
216
+ figure = {'data' : data ,
217
+ 'layout' : go .Layout (colorway = ["#5E0DAC" , '#FF4F00' , '#375CB1' ,
218
+ '#FF7400' , '#FFF400' , '#FF0056' ],
219
+ height = 600 ,
220
+ title = f"Market Volume for { ', ' .join (str (dropdown [i ]) for i in selected_dropdown_value )} Over Time" ,
221
+ xaxis = {"title" :"Date" ,
222
+ 'rangeselector' : {'buttons' : list ([{'count' : 1 , 'label' : '1M' ,
223
+ 'step' : 'month' ,
224
+ 'stepmode' : 'backward' },
225
+ {'count' : 6 , 'label' : '6M' ,
226
+ 'step' : 'month' ,
227
+ 'stepmode' : 'backward' },
228
+ {'step' : 'all' }])},
229
+ 'rangeslider' : {'visible' : True }, 'type' : 'date' },
230
+ yaxis = {"title" :"Transactions Volume" })}
231
+ return figure
232
+
233
+
234
+
235
+ if __name__ == '__main__' :
236
+ app .run_server (debug = True )
0 commit comments