-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdash_server.py
296 lines (243 loc) · 10.7 KB
/
dash_server.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
import os
import pytz
import datetime
import pandas as pd
import dash_bootstrap_components as dbc
from dash import Dash, html, dcc, dash_table, Input, Output
from src.utils import root_dir
import warnings
warnings.filterwarnings("ignore")
IST = pytz.timezone('Asia/Kolkata')
today_date = datetime.datetime.now(IST).strftime('%d-%m-%Y')
app = Dash(__name__)
app.title = 'Stock Alert Dashboard(v1.1)'
def get_filtered_data():
df = pd.read_csv(os.path.join(root_dir, 'data/stock.csv'), header=None)
df.rename(columns={0: 'Stock Name',
1: 'Previous Close',
2: 'Current Price',
3: 'Minimum(Day)',
4: 'Maximum(Day)',
5: 'Minimum(Year)',
6: 'Maximum(Year)',
7: 'Minimum(Threshold)',
8: 'Maximum(Threshold)',
9: 'Last Update',
10: 'difference',
11: 'difference(%)',
12: 'buy',
13: 'market',
14: 'currency'}, inplace=True)
columns = ['Stock Name', 'Previous Close', 'Current Price', 'difference', 'difference(%)',
'Minimum(Day)', 'Maximum(Day)', 'Minimum(Year)', 'Maximum(Year)', 'Minimum(Threshold)', 'Maximum(Threshold)']
df = df.round(2)
df['Last Update'] = pd.to_datetime(df['Last Update'])
lastest_date = df.groupby(['Stock Name'])[
'Last Update'].max().reset_index()['Last Update']
filtered_df = df[df['Last Update'].isin(lastest_date)]
filtered_df.sort_values(by=['difference(%)'], inplace=True, ascending=False)
filtered_df['difference(%)'] = filtered_df['difference(%)'].apply(lambda x: '{} %'.format(x))
filtered_df["Last Update"] = filtered_df["Last Update"].apply(lambda x: x.strftime('%H:%M:%S'))
df["Last Update"] = df["Last Update"].apply(lambda x: x.strftime('%H:%M:%S'))
indian_stocks = filtered_df[filtered_df['market'] == 'IN']
buy_table = indian_stocks[indian_stocks['buy'] == True]
watch_table = indian_stocks[indian_stocks['buy'] == False]
buy_table = buy_table[columns]
watch_table = watch_table[columns]
us_stocks = filtered_df[filtered_df['market'] == 'US']
us_stocks = us_stocks[columns]
return df, buy_table, watch_table, us_stocks
def overall_market_data():
market_data = pd.read_csv(os.path.join(root_dir, 'data/market.csv'), header=None)
columns = ['Stock Name', 'Previous Close', 'Current Price', 'difference', 'difference(%)',
'Minimum(Day)', 'Maximum(Day)', 'Minimum(Year)', 'Maximum(Year)']
market_data.rename(columns={0: 'Stock Name',
1: 'Previous Close',
2: 'Current Price',
3: 'Minimum(Day)',
4: 'Maximum(Day)',
5: 'Minimum(Year)',
6: 'Maximum(Year)',
7: 'Last Update',
8: 'difference',
9: 'difference(%)'}, inplace=True)
market_data = market_data.round(2)
market_data['Last Update'] = pd.to_datetime(market_data['Last Update'])
lastest_date = market_data.groupby(['Stock Name'])['Last Update'].max().reset_index()['Last Update']
filtered_df = market_data[market_data['Last Update'].isin(lastest_date)]
filtered_df = filtered_df[columns]
return filtered_df, lastest_date[0]
df, buy_table, watch_table, us_stocks = get_filtered_data()
market_data, latest_date = overall_market_data()
min_time = '09:00:00'
max_time = '16:00:00'
min_time = datetime.datetime.strptime(min_time, '%H:%M:%S')
max_time = datetime.datetime.strptime(max_time, '%H:%M:%S')
def get_dash_table(table_id, df):
return dash_table.DataTable(
id=table_id,
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict('records'),
style_cell={'textAlign': 'center'},
style_data={
'border': '1px solid black',
},
style_header={
'border': '1px solid black',
'backgroundColor': 'white',
'color': 'black',
'fontWeight': 'bold',
'textAlign': 'center',
'font-family': 'Courier New',
},
style_data_conditional=[
{
'if': {
'filter_query': '{difference} >= 0'},
'backgroundColor': 'rgb(0, 102, 51)',
'color': 'white',
'fontWeight': 'bold'
},
{
'if': {
'filter_query': '{difference} < 0'},
'backgroundColor': 'rgb(102, 0, 0)',
'color': 'white',
'fontWeight': 'bold'
}
],
style_table={
'width': '100%',
'height': '100%',
'overflowY': 'scroll',
'overflowX': 'scroll',
'textAlign': 'center',
},
)
app.layout = html.Div([
html.H1('Stock Alert Dashboard(v1.1)',
style={'textAlign': 'center', 'color': '#0099ff', 'font-family': 'Courier New',
'font-size': '30px', 'font-weight': 'bold', 'margin-top': '20px'}),
# update dbc badge
html.H3(id='last-update-badge', style={'textAlign': 'center', 'color': '#0099ff',
'font-family': 'Courier New', 'font-size': '20px', 'font-weight': 'bold'}),
# overall market table
html.H2('OverAll Market Status', style={
'textAlign': 'center', 'color': '#0099ff', 'font-family': 'Courier New', 'font-size': '20px', 'font-weight': 'bold'}),
get_dash_table('overall-table', market_data),
dcc.Interval(
id='interval-component',
interval=5*1000,
n_intervals=0),
# buy table and watch table
dbc.Row([
html.H2('Buy Stocks(INR)', style={'textAlign': 'center', 'color': '#0099ff',
'font-family': 'Courier New', 'font-size': '20px', 'font-weight': 'bold', 'margin-top': '20px'}),
get_dash_table('buy-table', buy_table),
html.H2('Watch Stocks(INR)', style={'textAlign': 'center', 'color': '#0099ff',
'font-family': 'Courier New', 'font-size': '20px', 'font-weight': 'bold', 'margin-top': '20px'}),
get_dash_table('watch-table', watch_table),
html.H2('US Stocks($)', style={'textAlign': 'center', 'color': '#0099ff',
'font-family': 'Courier New', 'font-size': '20px', 'font-weight': 'bold', 'margin-top': '20px'}),
get_dash_table('us-table', us_stocks),
]),
# graph by stock name select box
html.Div([
html.H3('Graph for Stock Data', style={'textAlign': 'center', 'color': '#0099ff', 'font-family': 'Courier New',
'font-size': '30px', 'font-weight': 'bold', 'margin-top': '20px'}),
dcc.Dropdown(
id='stock-name-select',
options=[{'label': i, 'value': i} for i in df['Stock Name'].unique()],
value='Steel Authority of India Limited',
style={'width': '100%', 'margin-top': '20px', 'color': '#0099ff',
'font-family': 'Courier New', 'font-size': '20px'}
)
]),
html.Div([
dcc.Graph(id='stock-graph'),
]),
])
# callback for interval component
@app.callback(
Output('buy-table', 'data'),
Input('interval-component', 'n_intervals'))
def update_buy_table(n):
_, buy_table, _, _ = get_filtered_data()
return buy_table.to_dict('records')
@app.callback(
Output('watch-table', 'data'),
Input('interval-component', 'n_intervals'))
def update_watch_table(n):
_, _, watch_table, _ = get_filtered_data()
return watch_table.to_dict('records')
@app.callback(
Output('us-table', 'data'),
Input('interval-component', 'n_intervals'))
def update_watch_table(n):
_, _, _, us_stocks = get_filtered_data()
return us_stocks.to_dict('records')
@app.callback(
Output('overall-table', 'data'),
Input('interval-component', 'n_intervals'))
def update_index_table(n):
market_data, _ = overall_market_data()
return market_data.to_dict('records')
@app.callback(
Output('last-update-badge', 'children'),
Input('interval-component', 'n_intervals'))
def update_badge(n):
_, latest_date = overall_market_data()
return "Dashboard last updated at {}".format(latest_date.strftime('%d-%m-%Y %H:%M:%S'))
@app.callback(
Output('stock-graph', 'figure'),
Input('stock-name-select', 'value'),
Input('interval-component', 'n_intervals'))
def update_graph(stock_name, n):
df, _, _, _= get_filtered_data()
filtered_df = df[df['Stock Name'] == stock_name]
return {
'data': [{
'x': filtered_df['Last Update'],
'y': filtered_df['Current Price'],
'name': 'Current Price',
'mode': 'lines',
'line': {'width': 1}
},
{
'x': filtered_df['Last Update'],
'y': filtered_df['Minimum(Threshold)'],
'name': 'Min Threshold',
'mode': 'lines',
'line': {'width': 1, 'dash': 'dash', 'color': 'red'},
},
{
'x': filtered_df['Last Update'],
'y': filtered_df['Maximum(Threshold)'],
'name': 'Max Threshold',
'mode': 'lines',
'line': {'width': 1, 'dash': 'dash', 'color': 'green'},
}],
'layout': {
'title': '{}- ({}) @ {}'.format(stock_name, filtered_df['market'].iloc[-1], filtered_df['Current Price'].iloc[-1]),
'xaxis': {'title': 'Date',
'autorange': True,
'showgrid': True,
'zeroline': True,
'showline': True,
'mirror': True,
'ticks': '',
'showticklabels': True,
'tickangle': 90,
'tickfont': {'size': 10},
'range': [min_time, max_time]},
'yaxis': {'title': 'Price(Per Stock)-{}'.format(filtered_df['currency'].iloc[-1]),
'range': [min(filtered_df['Minimum(Threshold)']) - 10, max(filtered_df['Maximum(Threshold)']) + 10]},
'height': 600,
'margin': {'l': 60, 'r': 10},
'hovermode': 'closest',
'showlegend': True,
'legend': {'x': 0.8, 'y': 1.1, 'orientation': 'h'},
}
}
if __name__ == '__main__':
app.run_server(host='0.0.0.0', port=5000, debug=True)