-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.py
289 lines (252 loc) · 11.6 KB
/
dashboard.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
from dash import Dash, html, dcc
import plotly.express as px
import pandas as pd
from dash.dependencies import Input, Output
import numpy as np
import json
import plotly.graph_objects as go
# Import de Font Awesome pour les icônes
external_scripts = [
{
'src': 'https://kit.fontawesome.com/fe63a2ff94.js',
'crossorigin': 'anonymous'
}
]
# Fonction pour paser les prix contenant des caractères spéciaux et supprimer les prix invalides
def mapFunction(x):
try:
return float(x.replace(' ', '').replace('$', '').replace(',', '')) if isinstance(x, str) else x
except:
return np.nan
# Fichiers contenant les prix
files = {
"ADA": "./data/coindesk_ada.txt",
"BTC": "./data/coinpaprika_btc.txt",
"ETH": "./data/coinmarketcap_eth.txt"
}
# Fonction de lecture des fichiers de prix scrappés
def readPrices(coin):
df = pd.read_csv(files[coin], sep=";", names=["date", coin])
df[coin] = df[coin].map(mapFunction)
df = df.dropna()
return df
def candlestickData(coin):
import pandas as pd
cs_df = pd.read_csv(files[coin], sep=";", names=["date", 'price'])
cs_df['price'] = cs_df['price'].map(mapFunction)
cs_df = cs_df.dropna()
# Converti les dates
cs_df['cs_date'] = pd.to_datetime(cs_df['date'])
cs_df['cs_date_only'] = cs_df['cs_date'].dt.date
cs_results_df = pd.DataFrame(columns=['cs_date', 'cs_open_price', 'cs_close_price', 'cs_max_price', 'cs_min_price'])
for cs_date in cs_df['cs_date_only'].unique():
cs_current_date_df = cs_df[cs_df['cs_date_only'] == cs_date]
cs_open_price = cs_current_date_df['price'].iloc[0]
cs_close_price = cs_current_date_df[cs_df['cs_date_only'] == cs_date]['price'].iloc[-1]
cs_max_price = cs_current_date_df['price'].max()
cs_min_price = cs_current_date_df['price'].min()
cs_results_df = cs_results_df.append({
'cs_date': cs_date,
'cs_open_price': cs_open_price,
'cs_close_price': cs_close_price,
'cs_max_price': cs_max_price,
'cs_min_price': cs_min_price
}, ignore_index=True)
return cs_results_df
##### TABLEAU DE BORD #####
app = Dash(__name__, external_scripts=external_scripts)
app.title = "Crypto Prices"
# Layout de la page
app.layout = html.Div(className="app", children=[
# Grand titre
html.H1(children='Cryptocurrency Prices'),
# Description
html.Div(className="description", children='''
A dashboard following the evolution of the price of some cryptocurrencies
'''),
# Ligne contenant deux colonnes
html.Div(className="row", children=[
# Colonne avec le sélecteur et le cours actuel
html.Div(className="col", children=[
html.Div(children='Cryptocurrency:', className="label"),
dcc.Dropdown(options=[
{'label': 'Bitcoin (BTC)', 'value': 'BTC'},
{'label': 'Ethereum (ETH)', 'value': 'ETH'},
{'label': 'Cardano (ADA)', 'value': 'ADA'},
], value='BTC', id='coin-dropdown', searchable=False, clearable=False),
html.Div(id="current-price", children=""),
]),
# Colonne avec la possibilité d'ajouter une moyenne mobile
html.Div(className="col", children=[
html.Div(children='Simple Moving Average (SMA) periods:', className="label"),
#html.Div(children=[
#html.Span('Increase SMA period below to display bollinger bands on the graph. Choose 0 to hide them.'),
#html.Br(),
#html.Span('Note: Lines can be hidden by clicking on the legend.')
#], className='help'),
dcc.Slider(min=0, max=200, step=1, value=100, className="slider", id="sma-slider", marks={0: '0', 200:'200'}, tooltip={"placement": "bottom", "always_visible": True}),
html.Div(children='Bollinger bands Δ (in formula μ ± Δ × σ):', className="label"),
dcc.Slider(min=0.4, max=5, step=0.01, value=2, className="slider", id="delta-slider", marks={0.4: '0.4', 5:'5'}, tooltip={"placement": "bottom", "always_visible": True})
])
]),
# Graph
dcc.Graph(
id='price-graph'
),
html.Div(className="description", children='''
Candlesticks showing open/close/max/min price of the selected cryptocurrency (made from 00:00 to 23:59 and not 8pm)
'''),
dcc.Graph(
id='candle-graph'
),
# Intervalle pour rafraîchir le contenu automatiquement
dcc.Interval(
id='interval-component',
interval=90*1000, # every 90 seconds
n_intervals=0
),
# Ligne contenant le tableau avec le rapport
html.Div(className="row", children=[
html.Div(className="col", children=[
# Description
html.Div(className="description", children=["Daily report, generated ", html.Span(id="reportDate", children="", className="bold"), " (24h period from 8pm to 8pm)"]),
# Notes
html.Div(children=[
html.Span('Since there is no opening and closing price for crypto-currencies, the daily report is generated with the price of the crypto-currencies over a 24 hour period since 8pm the previous day. Reports can be generated manually by running a Python script and it will still use 8pm as open and close time.')
], className='help'),
# Tableau
html.Table(id="reportTable",children=html.Tbody(children=[]))
])
])
])
# Callback mettant à jour toute la page
# Inputs : intervalle, slider, sélecteur de crypto
# Outputs : graph, tableau, prix live, ...
@app.callback(Output('price-graph', 'figure'),
Output('reportTable', 'children'),
Output('current-price', 'children'),
Output('reportDate', 'children'),
Output('candle-graph', 'figure'),
Input('interval-component', 'n_intervals'),
Input('coin-dropdown', 'value'),
Input('sma-slider', 'value'),
Input('delta-slider', 'value'))
def update_graph(n, value, sma, delta):
# Lecture des prix de la crypto choisie
#delta = 1
df = readPrices(value)
y = [value]
# Création du graph
fig = px.line(df, x='date', y=y)
# Ajout de la moyenne mobile (si voulue)
if sma > 0:
df['SMA'] = df[value].rolling(sma).mean()
if sma > 1:
df['STD'] = df[value].rolling(sma).std()
df['bb_low'] = df['SMA'] - delta * df['STD']
df['bb_up'] = df['SMA'] + delta * df['STD']
fig.add_trace(go.Scatter(x=df['date'], y=df['bb_up'],
fill=None,
mode='lines',
line_color='lightgreen', name="Upper band"
))
fig.add_trace(go.Scatter(
x=df['date'],
y=df['bb_low'],
fill='tonexty', # fill area between trace0 and trace1
mode='lines', line_color='lightgreen', name="Lower band"))
#fig.add_scatter(x=df['date'], y=df['bb_up'], mode='lines', name="Upper band")
#fig.add_scatter(x=df['date'], y=df['bb_low'], mode='lines', name="Lower band")
if sma == 1:
df.dropna(inplace=True)
fig.add_scatter(x=df['date'], y=df['SMA'], mode='lines', name="SMA", line_color="red")
# Paramètres du graph
fig.update_layout(legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1
))
# Affichage du prix live et l'évolution instantannée
previousPrice = df.iloc[-2][value]
currentPrice = df.iloc[-1][value]
liveEvol = 100*(currentPrice - previousPrice)/previousPrice
# Div pour la valeur live
livePriceDiv =[
html.Span("$" + str('{:,}'.format(currentPrice)), className="livePrice"),
html.Span(className="liveEvol " + ("green" if liveEvol > 0 else "red" if liveEvol < 0 else ""), children=[
html.I(className="fa-solid " + ("fa-caret-up" if liveEvol > 0 else "fa-caret-down" if liveEvol < 0 else "fa-minus")),
html.Span(str(round(liveEvol, 4)) + "%")
])
]
# Lecture des rapports quotidiens
f = open('/home/arthur/webscraping-project/data/reports.json')
report = json.load(f)[-1]
# Lecture de la date
reportDate = report['date']
# On met les données dans un tableau
# Je reconnais que c'est pas très lisible
# Chaque Tr contient 2 Td
# Le 1er Td contient - une balise "i" (icône FontAwesome) + la description de la ligne
# Le 2eme Td contient - la valeur du rapport dans un SPAN
# FontAwesome Icons
bitcoinIcon = html.I(className="fa-brands fa-bitcoin")
chartLineIcon = html.I(className="fa-solid fa-chart-line")
evolutionIcon = html.I(className="fa-solid " + ("fa-caret-up" if report[value]['evol'] > 0 else "fa-caret-down"))
backwardStepIcon = html.I(className="fa-solid fa-backward-step")
forwardStepIcon = html.I(className="fa-solid fa-forward-step")
volatilityIcon = html.I(className="fa-solid fa-arrow-down-up-across-line")
downIcon = html.I(className="fa-solid fa-down-long")
upIcon = html.I(className="fa-solid fa-up-long")
# Couleur pour l'évolution du prix
evolutionColor = "green" if report[value]['evol'] > 0 else "red"
# Raccourci pour crééer un span avec la classe "bold"
def boldSpan(content):
return html.Span(className="bold", children=content)
# Valeurs à afficher
openPrice = boldSpan("$" + str('{:,}'.format(report[value]['open']['val'])))
openPriceDate = (" ("+ report[value]['open']['date'] +")")
closePrice = boldSpan("$" + str('{:,}'.format(report[value]['close']['val'])))
closePriceDate = (" ("+ report[value]['close']['date'] +")")
minimumPrice = boldSpan("$" + str('{:,}'.format(report[value]['min'])))
maximumPrice = boldSpan("$" + str('{:,}'.format(report[value]['max'])))
# Tableau
tableContent = [
html.Tr(children=[
html.Td(children=[bitcoinIcon, "Currency"]),
html.Td(id="reportCurrency", children=boldSpan(value))]),
html.Tr(children=[
html.Td(children=[chartLineIcon, "Evolution"]),
html.Td(id="reportEvolution", className=evolutionColor, children=[
evolutionIcon,
boldSpan((str(report[value]['evol']) + "%"))
])]),
html.Tr(children=[
html.Td(children=[backwardStepIcon,"Open price"]),
html.Td(id="reportOpen", children=[openPrice, openPriceDate])]),
html.Tr(children=[
html.Td(children=[forwardStepIcon,"Close price"]),
html.Td(id="reportClose", children=[closePrice, closePriceDate])]),
html.Tr(children=[
html.Td(children=[volatilityIcon, "Volatility"]),
html.Td(id="reportVol", children=boldSpan(report[value]['std']))]),
html.Tr(children=[
html.Td(children=[downIcon, "Minimum"]),
html.Td(id="reportMin", children=minimumPrice)]),
html.Tr(children=[
html.Td(children=[upIcon, "Maximum"]),
html.Td(id="reportMax", children=maximumPrice)]),
]
candle = candlestickData(value)
figCandle = go.Figure(data=[go.Candlestick(x=candle['cs_date'],
open=candle['cs_open_price'],
high=candle['cs_max_price'],
low=candle['cs_min_price'],
close=candle['cs_close_price'])])
figCandle.update_layout(xaxis_rangeslider_visible=False)
# Renvoi des valeurs modifiées
return fig, tableContent, livePriceDiv, reportDate, figCandle
# Lancement du dashboard
if __name__ == '__main__':
app.run_server(debug=False, port=3000, host= '0.0.0.0')