-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
1234 lines (1156 loc) · 54.2 KB
/
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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import requests
import altair as alt
import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px
from datetime import datetime
from PIL import Image
from streamlit_float import *
from supabase import create_client, Client
from nexbit_utils import predict_btc, predict_eth, predict_sol
from preprocess import final_transform
# [STREAMLIT] PAGE CONFIGURATION
icon = Image.open("assets/nexbit-icon.png")
st.set_page_config(page_title="Nexbit: Analytics & Forecasting", page_icon=icon, layout="wide")
st.logo("assets/nexbit-logo.svg")
# [SUPABASE] SECRETS CONFIGURATION
SUPABASE_URL = st.secrets["SUPABASE_URL"]
SUPABASE_KEY = st.secrets["SUPABASE_KEY"]
# [STREAMLIT] COLOR PALETTE
color1_dark = "#8DFB4E"
color1_light = "#AFFD86"
color2_dark = "#3a1a1d"
color2_light = "#e2352f"
text_light = "#becbdc"
text_dark = "#8293a7"
black_dark = "#0d1216"
black_light = "#1b242d"
# [STREAMLIT] HIDE MENU
hide_menu = """
<style>
#MainMenu {
visibility: hidden;
}
footer {
visibility: hidden;
}
div[data-testid="stDecoration"] {
visibility: hidden;
height: 0%;
position: fixed;
}
div[data-testid="stStatusWidget"] {
visibility: hidden;
height: 0%;
position: fixed;
}
[data-testid="stToolbar"] {
display: none;
}
</style>
"""
st.markdown(hide_menu, unsafe_allow_html=True)
# [STEAMLIT] CHANGE FONT STYLE
with open( "style.css" ) as css:
st.markdown( f'<style>{css.read()}</style>' , unsafe_allow_html= True)
# [STREAMLIT] ADJUST HEADER
header = """
<style>
[data-testid="stHeader"] {
height: 5.3rem;
width: auto;
z-index: 1;
}
</style>
"""
st.markdown(header, unsafe_allow_html=True)
# [STREAMLIT] HIDE TEXT ANCHOR
hide_anchor = """
<style>
[data-testid="stHeaderActionElements"] {
display: none;
}
</style>
"""
st.markdown(hide_anchor, unsafe_allow_html=True)
# [STREAMLIT] HIDE IMAGE ZOOM
hide_zoom = """
<style>
[data-testid="stBaseButton-elementToolbar"] {
display: none;
}
</style>
"""
st.markdown(hide_zoom, unsafe_allow_html=True)
# [STREAMLIT] ADJUST TOP PADDING
top = """
<style>
.block-container {
padding-top: 0rem;
padding-bottom: 3rem;
margin-top: -5rem;
}
</style>
"""
st.markdown(top, unsafe_allow_html=True)
# [STREAMLIT] ADJUST LOGO SIZE
logo = """
<style>
[data-testid="stLogo"] {
width: 10rem;
height: auto;
}
</style>
"""
st.markdown(logo, unsafe_allow_html=True)
# [STREAMLIT] PRIMARY BUTTON COLOR
primary = """
<style>
[data-testid="stBaseButton-primary"] {
color: """ + color1_dark + """;
}
</style>
"""
st.markdown(primary, unsafe_allow_html=True)
# [STREAMLIT] FIXED IMAGE HEIGHT
set_height = """
<style>
[data-testid="stImageContainer"] {
height: 28px;
}
</style>
"""
st.markdown(set_height, unsafe_allow_html=True)
# [STREAMLIT] ADJUST SETTINGS BUTTON
set_btn = """
<style>
[data-testid="stBaseButton-secondary"] {
border-radius: 5rem;
border: 2px solid #FFFFFF;
width: 2.5rem;
height: 2.5rem;
}
</style>
"""
st.markdown(set_btn, unsafe_allow_html=True)
# [STREAMLIT] HOVER EFFECT
hover_card = """
<style>
.news-card {
display: block;
width: auto;
height: auto;
padding-top: 12px;
padding-bottom: 12px;
padding-left: 15px;
padding-right: 15px;
margin: 0px;
margin-bottom: 15px;
border-radius: 0.8rem;
background-color: """ + black_light + """;
transition: background-color 0.3s ease, transform 0.2s ease;
text-decoration: none;
color: #FFFFFF;
}
.news-card:hover {
background-color: """ + black_light + """;
transform: scale(1.02);
text-decoration: none;
color: #FFFFFF;
cursor: pointer;
}
.news-card span {
display: block;
}
.news-card .title {
text-align: justify;
font-size: 1rem;
font-weight: 600;
color: #FFFFFF;
margin-bottom: 8px;
}
.news-card .summary {
text-align: justify;
font-size: 0.8rem;
font-weight: 300;
color: """ + text_light + """;
margin-bottom: 10px;
}
.news-card .meta-info {
display: flex;
justify-content: space-between;
align-items: center;
font-size: 0.7rem;
font-weight: 400;
color: """ + text_dark + """;
}
</style>
"""
st.markdown(hover_card, unsafe_allow_html=True)
# [STREAMLIT] INFO EFFECT
info_hover = """
<style>
.info-icon {
position: relative;
cursor: default;
}
.info-tooltip {
display: none;
cursor: default;
position: absolute;
top: 85%;
left: 50%;
transform: translateX(-50%);
background-color: """ + black_light + """;
color: """ + text_dark + """;
padding-top: 20px;
padding-bottom: 15px;
padding-left: 20px;
padding-right: 20px;
border-radius: 6px;
font-size: 0.8rem;
box-shadow: 0px 8px 8px rgba(0, 0, 0, 0.35);
z-index: 10;
white-space: normal;
width: 250px;
word-wrap: break-word;
text-align: left;
}
.info-icon:hover .info-tooltip {
display: block;
cursor: default;
}
.info-icon2 {
position: relative;
cursor: default;
}
.info-tooltip2 {
display: none;
cursor: default;
position: absolute;
top: 120%;
left: 50%;
transform: translateX(-50%);
background-color: """ + black_light + """;
color: """ + text_dark + """;
padding-top: 20px;
padding-bottom: 15px;
padding-left: 20px;
padding-right: 20px;
border-radius: 6px;
font-size: 0.8rem;
box-shadow: 0px 8px 8px rgba(0, 0, 0, 0.35);
z-index: 10;
white-space: normal;
width: 250px;
word-wrap: break-word;
text-align: left;
}
.info-icon2:hover .info-tooltip2 {
display: block;
cursor: default;
}
.info-icon3 {
position: relative;
cursor: default;
}
.info-tooltip3 {
display: none;
cursor: default;
position: absolute;
top: 85%;
left: 50%;
transform: translateX(-100%);
background-color: """ + black_light + """;
color: """ + text_dark + """;
padding-top: 20px;
padding-bottom: 15px;
padding-left: 20px;
padding-right: 20px;
border-radius: 6px;
font-size: 0.8rem;
box-shadow: 0px 8px 8px rgba(0, 0, 0, 0.35);
z-index: 10;
white-space: normal;
width: 250px;
word-wrap: break-word;
text-align: left;
}
.info-icon3:hover .info-tooltip3 {
display: block;
cursor: default;
}
.info-icon4 {
position: relative;
cursor: default;
}
.info-tooltip4 {
display: none;
cursor: default;
position: absolute;
top: 85%;
left: 50%;
transform: translateX(-100%);
background-color: """ + black_light + """;
color: """ + text_dark + """;
padding-top: 20px;
padding-bottom: 15px;
padding-left: 20px;
padding-right: 20px;
border-radius: 6px;
font-size: 0.8rem;
box-shadow: 0px 8px 8px rgba(0, 0, 0, 0.35);
z-index: 10;
white-space: normal;
width: 300px;
word-wrap: break-word;
text-align: left;
}
.info-icon4:hover .info-tooltip4 {
display: block;
cursor: default;
}
</style>
"""
st.markdown(info_hover, unsafe_allow_html=True)
remove_underline = """
<style>
.st-emotion-cache-1cvow4s a {
text-decoration: none;
}
</style>
"""
st.markdown(remove_underline, unsafe_allow_html=True)
# [SUPABASE] FETCHING DATA FROM THE DATABASE
def fetch_data(table, url, key):
supabase: Client = create_client(url, key)
response = supabase.table(table).select('*').execute()
if response.data:
data = pd.DataFrame(response.data)
return data
crypto_info = fetch_data('Cryptocurrency', SUPABASE_URL, SUPABASE_KEY)
crypto_price = fetch_data('Price', SUPABASE_URL, SUPABASE_KEY)
crypto_news = fetch_data('News', SUPABASE_URL, SUPABASE_KEY)
#ml_data_btc = final_transform(crypto_news, crypto_price, 1)
#ml_data_eth = final_transform(crypto_news, crypto_price, 2)
#ml_data_sol = final_transform(crypto_news, crypto_price, 3)
# [STREAMLIT] CATEGORIZE SCORE FOR NEWS CARD
def categorize_score(score, color=False):
if color:
if score > 0.5:
return color1_light
elif 0 < score <= 0.5:
return color1_light
elif score == 0:
return text_light
elif -0.5 <= score < 0:
return color2_light
else:
return color2_light
else:
if score > 0.5:
return 'Strong Positive'
elif 0 < score <= 0.5:
return 'Moderate Positive'
elif score == 0:
return 'Neutral'
elif -0.5 <= score < 0:
return 'Moderate Negative'
else:
return 'Strong Negative'
# [STREAMLIT] SESSION STATE FOR CRYPTO SELECTED
if "price" not in st.session_state:
st.session_state.price = crypto_price[crypto_price["crypto_id"]==1]["close_price"].iloc[-1]
if "price_data" not in st.session_state:
st.session_state.price_data = crypto_price[crypto_price["crypto_id"]==1]
if "crypto" not in st.session_state:
st.session_state.crypto = crypto_info["name"].iloc[0]
if "symbol" not in st.session_state:
st.session_state.symbol = crypto_info["symbol"].iloc[0]
if "market_cap" not in st.session_state:
st.session_state.market_cap = crypto_info["market_cap"].iloc[0]
if "total_supply" not in st.session_state:
st.session_state.total_supply = crypto_info["total_supply"].iloc[0]
if "website" not in st.session_state:
st.session_state.website = crypto_info["website"].iloc[0]
if "news" not in st.session_state:
st.session_state.news = crypto_news[crypto_news["crypto_id"]==1]
if "predictions" not in st.session_state:
st.session_state.predictions = [[1,60], [1,81], [0,60]]
info, chart = st.columns([1,2])
with info:
# CRYPTO LOGO AND NAME
if st.session_state.crypto == "Bitcoin":
st.image("assets/btc-logo.svg")
elif st.session_state.crypto == "Ethereum":
st.image("assets/eth-logo.svg")
else:
st.image("assets/sol-logo.svg")
# CRYPTO PRICE
price_df = st.session_state.price_data
pct_change = ((st.session_state.price - price_df["close_price"].iloc[-2]) / price_df["close_price"].iloc[-2]) * 100
if pct_change > 0:
color = color1_light
margin = "-6px"
arrow = "arrow_drop_up"
else:
color = color2_light
margin = "-2px"
arrow = "arrow_drop_down"
price_change = f"""
<div style="display: flex; justify-content: flex-start; align-items: center;">
<h1 style="font-size: 3.5rem; font-weight: 600; line-height: 0.8; padding-top: 3px;">
{"${:,.1f}".format(float(st.session_state.price))}
</h1>
<span>
<i class="material-icons" style="font-size: 2rem; position: relative; top: {margin}; color: {color};">{arrow}</i>
</span>
<h4 style="font-size: 1.2rem; font-weight: 700; margin: 0; position: relative; top: -5px; color: {color};">{"{:.2f}".format(float(pct_change))}%</h4>
</div>
<style>
@import url('https://fonts.googleapis.com/icon?family=Material+Icons');
</style>
"""
st.markdown(price_change, unsafe_allow_html=True)
# MODEL PREDICTION
if st.session_state.symbol == "BTC":
date_acc = f"""
<div style='display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;'>
<span style='text-align: left; font-size: 0.7rem; font-weight: 500; color: {text_light};'>Date: {datetime.now().strftime("%b %d, %Y")}</span>
<span style='text-align: center; font-size: 0.7rem; font-weight: 500; color: {text_light};'>Accuracy: 52.40%</span>
<span style='text-align: right; font-size: 0.7rem; font-weight: 500; color: {text_light};'>Confidence: {st.session_state.predictions[0][1]}%</span>
</div>
"""
elif st.session_state.symbol == "ETH":
date_acc = f"""
<div style='display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;'>
<span style='text-align: left; font-size: 0.7rem; font-weight: 500; color: {text_light};'>Date: {datetime.now().strftime("%b %d, %Y")}</span>
<span style='text-align: center; font-size: 0.7rem; font-weight: 500; color: {text_light};'>Accuracy: 53.11%</span>
<span style='text-align: right; font-size: 0.7rem; font-weight: 500; color: {text_light};'>Confidence: {st.session_state.predictions[1][1]}%</span>
</div>
"""
else:
date_acc = f"""
<div style='display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;'>
<span style='text-align: left; font-size: 0.7rem; font-weight: 500; color: {text_light};'>Date: {datetime.now().strftime("%b %d, %Y")}</span>
<span style='text-align: center; font-size: 0.7rem; font-weight: 500; color: {text_light};'>Accuracy: 54.39%</span>
<span style='text-align: right; font-size: 0.7rem; font-weight: 500; color: {text_light};'>Confidence: {st.session_state.predictions[2][1]}%</span>
</div>
"""
st.markdown(date_acc, unsafe_allow_html=True)
if st.session_state.symbol == "BTC":
if st.session_state.predictions[0][0] == 1:
model_prediction = f"""
<div style='width: auto; height: auto; padding-top: 12px; padding-bottom: 12px; padding-left: 15px; padding-right: 15px; margin: 0px; margin-bottom: 15px; border: 2px solid {color1_light}; border-radius: 0.8rem; background-color: {color1_dark}1A;'>
<span style='text-align: left; font-size: 1rem; font-weight: 500;'>The model predicts a price increase today.</span>
</div>
"""
else:
model_prediction = f"""
<div style='width: auto; height: auto; padding-top: 12px; padding-bottom: 12px; padding-left: 15px; padding-right: 15px; margin: 0px; margin-bottom: 15px; border: 2px solid {color2_light}; border-radius: 0.8rem; background-color: {color2_dark}80;'>
<span style='text-align: left; font-size: 1rem; font-weight: 500;'>The model predicts a price decrease today.</span>
</div>
"""
elif st.session_state.symbol == "ETH":
if st.session_state.predictions[1][0] == 1:
model_prediction = f"""
<div style='width: auto; height: auto; padding-top: 12px; padding-bottom: 12px; padding-left: 15px; padding-right: 15px; margin: 0px; margin-bottom: 15px; border: 2px solid {color1_light}; border-radius: 0.8rem; background-color: {color1_dark}1A;'>
<span style='text-align: left; font-size: 1rem; font-weight: 500;'>The model predicts a price increase today.</span>
</div>
"""
else:
model_prediction = f"""
<div style='width: auto; height: auto; padding-top: 12px; padding-bottom: 12px; padding-left: 15px; padding-right: 15px; margin: 0px; margin-bottom: 15px; border: 2px solid {color2_light}; border-radius: 0.8rem; background-color: {color2_dark}80;'>
<span style='text-align: left; font-size: 1rem; font-weight: 500;'>The model predicts a price decrease today.</span>
</div>
"""
else:
if st.session_state.predictions[2][0] == 1:
model_prediction = f"""
<div style='width: auto; height: auto; padding-top: 12px; padding-bottom: 12px; padding-left: 15px; padding-right: 15px; margin: 0px; margin-bottom: 15px; border: 2px solid {color1_light}; border-radius: 0.8rem; background-color: {color1_dark}1A;'>
<span style='text-align: left; font-size: 1rem; font-weight: 500;'>The model predicts a price increase today.</span>
</div>
"""
else:
model_prediction = f"""
<div style='width: auto; height: auto; padding-top: 12px; padding-bottom: 12px; padding-left: 15px; padding-right: 15px; margin: 0px; margin-bottom: 15px; border: 2px solid {color2_light}; border-radius: 0.8rem; background-color: {color2_dark}80;'>
<span style='text-align: left; font-size: 1rem; font-weight: 500;'>The model predicts a price decrease today.</span>
</div>
"""
st.markdown(model_prediction, unsafe_allow_html=True)
# CRYPTO INFO
market_cap = f"""
<div style='display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;'>
<div style='display: flex; align-items: center; gap: 6px;'>
<span style='text-align: left; font-size: 1rem; font-weight: 500; color: {text_dark};'>Market Cap</span>
<span class="info-icon2" style="cursor: default; display: flex; align-items: center;">
<i class="material-symbols-outlined" style="font-size: 1rem; color: {text_dark}; cursor: default;">info</i>
<div class="info-tooltip2">
Refers to the total market value of a cryptocurrency’s circulating supply.
<br>
<br>
Market Cap = Current Price x Circulating Supply
</div>
</span>
<style>
@import url('https://fonts.googleapis.com/icon?family=Material+Symbols+Outlined');
</style>
</div>
<span style='text-align: right; font-size: 1rem; font-weight: 500;'>{"${:,.2f}".format(float(st.session_state.market_cap))}</span>
</div>
"""
st.markdown(market_cap, unsafe_allow_html=True)
total_supply = f"""
<div style='display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;'>
<div style='display: flex; align-items: center; gap: 6px;'>
<span style='text-align: left; font-size: 1rem; font-weight: 500; color: {text_dark};'>Total Supply</span>
<span class="info-icon2" style="cursor: default; display: flex; align-items: center;">
<i class="material-symbols-outlined" style="font-size: 1rem; color: {text_dark}; cursor: default;">info</i>
<div class="info-tooltip2">
The amount of coins that have already been created, minus any coins that have been burned (removed from circulation).
<br>
<br>
Total Supply = Onchain supply - Burned Tokens
</div>
</span>
<style>
@import url('https://fonts.googleapis.com/icon?family=Material+Symbols+Outlined');
</style>
</div>
<span style='text-align: right; font-size: 1rem; font-weight: 500;'>{"${:,.2f}".format(float(st.session_state.total_supply))}</span>
</div>
"""
st.markdown(total_supply, unsafe_allow_html=True)
website = f"""
<div style='display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px;'>
<span style='text-align: left; font-size: 1rem; font-weight: 500; color: {text_dark};'>Website</span>
<span style='text-align: left; font-size: 1rem; font-weight: 500; text-align: right'>
<a href='{st.session_state.website}' style='text-decoration: none; color: {color1_light};'>{st.session_state.website.replace("https://", "").replace("/", "").replace("www.","")}
</a>
</span>
</div>
"""
st.markdown(website, unsafe_allow_html=True)
with chart:
# PRICE CHART
df = st.session_state.price_data
df = df[['date', 'close_price']]
df = df.copy()
df['date'] = pd.to_datetime(df['date'])
pct_change = ((st.session_state.price - df["close_price"].iloc[-2]) / df["close_price"].iloc[-2]) * 100
if (st.session_state.symbol == 'BTC') and (st.session_state.predictions[0][0] == 1):
color_line = color1_dark
color_fill = "#0a3c21"
elif (st.session_state.symbol == 'BTC') and (st.session_state.predictions[0][0] == 0):
color_line = color2_light
color_fill = color2_dark
elif (st.session_state.symbol == 'ETH') and (st.session_state.predictions[1][0] == 1):
color_line = color1_dark
color_fill = "#0a3c21"
elif (st.session_state.symbol == 'ETH') and (st.session_state.predictions[1][0] == 0):
color_line = color2_light
color_fill = color2_dark
elif (st.session_state.symbol == 'SOL') and (st.session_state.predictions[2][0] == 1):
color_line = color1_dark
color_fill = "#0a3c21"
else:
color_line = color2_light
color_fill = color2_dark
price_chart = alt.Chart(df).mark_area(
line={'color': f'{color_line}'},
color=alt.Gradient(
gradient='linear',
stops=[alt.GradientStop(color=f'{black_dark}', offset=0),
alt.GradientStop(color=f'{color_fill}', offset=1)],
x1=1,
x2=1,
y1=1,
y2=0
)
).encode(
alt.X('date:T', title=None),
alt.Y('close_price:Q', title=None, axis=alt.Axis(orient='right', grid=True, gridColor=f'{text_dark}')),
tooltip=[
alt.Tooltip("date:T", title="Date"),
alt.Tooltip("close_price:Q", title="Closing Price")]
).properties(
height=315,
padding={'top': 20, 'bottom': 20, 'left': 2, 'right': 2}
).configure_axis(
labelColor=f'{text_dark}',
gridWidth=0.2
)
st.altair_chart(price_chart, use_container_width=True)
sentiment_section, news_section = st.columns([3,2])
with sentiment_section:
# DAILY AVERAGE SENTIMENT
news_df = st.session_state.news
news_df = news_df.copy()
news_df['date'] = pd.to_datetime(news_df['date'])
news_df['day_of_week'] = news_df['date'].dt.dayofweek
avg_sentiment_by_day = news_df.groupby('day_of_week')['sentiment'].mean().reset_index()
avg_sentiment_by_day['day_name'] = avg_sentiment_by_day['day_of_week'].map({
0: 'Monday', 1: 'Tuesday', 2: 'Wednesday', 3: 'Thursday', 4: 'Friday', 5: 'Saturday', 6: 'Sunday'})
max_score_day = avg_sentiment_by_day.loc[avg_sentiment_by_day['sentiment'].idxmax(), 'day_name']
max_score = avg_sentiment_by_day.loc[avg_sentiment_by_day['sentiment'].idxmax(), 'sentiment']
ave_sentiment_title = f"""
<div style='display: flex; justify-content: space-between; align-items: center; margin-top: -15px;'>
<div style='display: flex; align-items: center; gap: 6px;'>
<h4 style='text-align: left; font-size: 1rem; font-weight: 600; color: {text_light};'>
DAILY AVERAGE SENTIMENT
</h4>
<span class="info-icon" style="cursor: default;">
<i class="material-symbols-outlined" style="font-size: 1rem; color: {text_light}; cursor: default;">info</i>
<div class="info-tooltip">
The daily aggregated sentiment scores are sourced from Alpha Vantage.
<br>
<br>
Positive Sentiment > 0
<br>
Negative Sentiment < 0
</div>
</span>
<style>
@import url('https://fonts.googleapis.com/icon?family=Material+Symbols+Outlined');
</style>
</div>
<span class="info-icon3" style="cursor: default;">
<i class="material-symbols-outlined" style="font-size: 1rem; color: {text_light}; cursor: default;">help</i>
<div class="info-tooltip3"">
This graph shows that <strong>{max_score_day}</strong> has the highest average sentiment score of <strong>{"{:,.2f}".format(float(max_score))}</strong>.
</div>
</span>
<style>
@import url('https://fonts.googleapis.com/icon?family=Material+Symbols+Outlined');
</style>
</div>
"""
st.markdown(ave_sentiment_title, unsafe_allow_html=True)
ave_sent_chart = alt.Chart(avg_sentiment_by_day).mark_bar(
opacity=0.7,
cornerRadiusTopLeft=5,
cornerRadiusTopRight=5,
color=alt.Gradient(
gradient='linear',
stops=[alt.GradientStop(color=f'{black_dark}', offset=0),
alt.GradientStop(color='#0a2f1e', offset=1)],
x1=1,
x2=1,
y1=1,
y2=0
)
).encode(
x=alt.X('day_name:N',
sort=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
title=None,
axis=alt.Axis(labelAngle=0)),
y=alt.Y('sentiment:Q',
title=None,
axis=alt.Axis(grid=True, gridColor=f'{text_dark}')),
tooltip=[
alt.Tooltip("day_name:N", title="Day"),
alt.Tooltip("sentiment:Q", title="Avg Score")]
).properties(
height=300,
width='container'
)
highlighted_bar = alt.Chart(avg_sentiment_by_day).mark_bar(
cornerRadiusTopLeft=5,
cornerRadiusTopRight=5,
color=alt.Gradient(
gradient='linear',
stops=[alt.GradientStop(color=f'{black_dark}', offset=0),
alt.GradientStop(color=f'{color1_light}', offset=1)],
x1=1,
x2=1,
y1=1,
y2=0
)
).encode(
x=alt.X('day_name:N',
sort=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
title=None,
axis=alt.Axis(labelAngle=0)),
y=alt.Y('sentiment:Q',
title=None,
axis=alt.Axis(grid=True, gridColor=f'{text_dark}')),
tooltip=[
alt.Tooltip("day_name:N", title="Day"),
alt.Tooltip("sentiment:Q", title="Avg Score")]
).transform_filter(
alt.datum.day_name == max_score_day
).properties(
height=300,
width='container'
)
text_format = alt.Chart(avg_sentiment_by_day).mark_text(
align='center',
baseline='bottom',
fontSize=14,
font='Roboto',
fontWeight='bold',
dy=-5,
color=f'{color1_light}'
).transform_filter(
alt.datum.day_name == max_score_day
).encode(
x=alt.X('day_name:N',
sort=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']),
y=alt.Y('sentiment:Q'),
text=alt.Text('sentiment:Q', format='.2f'),
tooltip=[
alt.Tooltip("day_name:N", title="Day"),
alt.Tooltip("sentiment:Q", title="Avg Score")]
)
final_ave_sent_chart = alt.layer(ave_sent_chart, highlighted_bar, text_format).resolve_scale(
color='independent'
).configure_axis(
labelColor=f'{text_dark}',
gridWidth=0.2
)
st.altair_chart(final_ave_sent_chart, use_container_width=True)
# SENTIMENT STATISTIC
if st.session_state.symbol == "BTC":
sent_count_data = pd.read_excel('btc.xlsx')
elif st.session_state.symbol == "ETH":
sent_count_data = pd.read_excel('eth.xlsx')
else:
sent_count_data = pd.read_excel('sol.xlsx')
sent_count_AV = sent_count_data[['AV_sentiment_category_Strong Positive',
'AV_sentiment_category_Moderate Positive',
'AV_sentiment_category_Neutral',
'AV_sentiment_category_Moderate Negative',
'AV_sentiment_category_Strong Negative']]
sent_count_AV.rename(columns={'AV_sentiment_category_Strong Positive': 'Strong Positive',
'AV_sentiment_category_Moderate Positive': 'Moderate Positive',
'AV_sentiment_category_Neutral': 'Neutral',
'AV_sentiment_category_Moderate Negative': 'Moderate Negative',
'AV_sentiment_category_Strong Negative': 'Strong Negative'}, inplace=True)
sent_count_TB = sent_count_data[['TB_sentiment_category_Strong Positive',
'TB_sentiment_category_Moderate Positive',
'TB_sentiment_category_Neutral',
'TB_sentiment_category_Moderate Negative',
'TB_sentiment_category_Strong Negative']]
sent_count_TB.rename(columns={'TB_sentiment_category_Strong Positive': 'Strong Positive',
'TB_sentiment_category_Moderate Positive': 'Moderate Positive',
'TB_sentiment_category_Neutral': 'Neutral',
'TB_sentiment_category_Moderate Negative': 'Moderate Negative',
'TB_sentiment_category_Strong Negative': 'Strong Negative'}, inplace=True)
sent_count_AV = sent_count_AV.sum(axis=0)
sent_count_TB = sent_count_TB.sum(axis=0)
sentiment_counts_AV = sent_count_AV.reset_index()
sentiment_counts_TB = sent_count_TB.reset_index()
sentiment_counts_AV.columns = ['sentiment', 'count']
sentiment_counts_TB.columns = ['sentiment', 'count']
max_count_AV = sentiment_counts_AV.loc[sentiment_counts_AV['count'].idxmax(), 'sentiment']
max_count_TB = sentiment_counts_TB.loc[sentiment_counts_TB['count'].idxmax(), 'sentiment']
sentiment_stat_title = f"""
<div style='display: flex; justify-content: space-between; align-items: center;'>
<h4 style='text-align: left; font-size: 1rem; font-weight: 600; margin-top: -10px; color: {text_light};'>
SENTIMENT STATISTIC
</h4>
<span class="info-icon4" style="cursor: default;">
<i class="material-symbols-outlined" style="font-size: 1rem; color: {text_light}; cursor: default;">help</i>
<div class="info-tooltip4"">
The two graphs show the difference in category counts between sentiment scores sourced from Alpha Vantage and those derived using TextBlob.
<br>
<br>
<strong>{max_count_AV}</strong> ⟶ Alpha Vantage
<br>
<strong>{max_count_TB}</strong> ⟶ TextBlob
</div>
</span>
<style>
@import url('https://fonts.googleapis.com/icon?family=Material+Symbols+Outlined');
</style>
</div>
"""
st.markdown(sentiment_stat_title, unsafe_allow_html=True)
chart_1, chart_2 = st.columns(2)
with chart_1:
av_title = f"<h4 style='text-align: left; font-size: 0.9rem; font-weight: 500; margin-top: -15px; color: {text_dark};'>Alpha Vantage Sentiment Score</h4>"
st.markdown(av_title, unsafe_allow_html=True)
AV_chart = alt.Chart(sentiment_counts_AV).mark_bar(
opacity=0.7,
cornerRadiusBottomRight=5,
cornerRadiusTopRight=5,
color=alt.Gradient(
gradient='linear',
stops=[
alt.GradientStop(color=f'{black_dark}', offset=0),
alt.GradientStop(color=f'{black_light}', offset=1)
],
x1=0,
x2=1,
y1=0,
y2=0)
).encode(
x=alt.X('count:Q', axis=alt.Axis(grid=True, gridColor=f'{text_dark}')),
y=alt.Y('sentiment:O', title=None, sort=['Strong Positive', 'Moderate Positive', 'Neutral', 'Moderate Negative', 'Strong Negative']),
tooltip=[
alt.Tooltip("count:Q", title="Count"),
alt.Tooltip("sentiment:O", title="Category")]
).properties(
height=300,
width='container'
)
highlighted_bar_AV = alt.Chart(sentiment_counts_AV).mark_bar(
cornerRadiusBottomRight=5,
cornerRadiusTopRight=5,
color=alt.Gradient(
gradient='linear',
stops=[
alt.GradientStop(color=f'{black_dark}', offset=0),
alt.GradientStop(color='#4a6382', offset=1)
],
x1=0,
x2=1,
y1=0,
y2=0)
).encode(
x=alt.X('count:Q', axis=alt.Axis(grid=True, gridColor=f'{text_dark}')),
y=alt.Y('sentiment:O', title=None, sort=['Strong Positive', 'Moderate Positive', 'Neutral', 'Moderate Negative', 'Strong Negative']),
tooltip=[
alt.Tooltip("count:Q", title="Count"),
alt.Tooltip("sentiment:O", title="Category")]
).transform_filter(
alt.datum.sentiment == max_count_AV
).properties(
height=300,
width='container'
)
final_AV_chart = alt.layer(AV_chart, highlighted_bar_AV).configure_axis(
labels=False,
ticks=False,
title=None,
offset=0,
gridWidth=0.2
).configure_legend(
labelFontSize=0,
symbolSize=0,
title=None,
offset=0
).configure_view(
step=0
)
st.altair_chart(final_AV_chart, use_container_width=True)
# TOTAL SENTIMENT COUNT (AV)
total_sentiment_count_AV = f"""
<div style='margin-top: -30px; margin-bottom: 25px;'>
<div style='display: flex; justify-content: space-between; align-items: center;'>
<span style='text-align: left; font-size: 1rem; font-weight: 500; color: {text_dark};'>Strong Positive Count</span>
<span style='text-align: right; font-size: 1rem; font-weight: 500;'>{sentiment_counts_AV[sentiment_counts_AV['sentiment'] == 'Strong Positive']['count'].iloc[0]}</span>
</div>
<div style='display: flex; justify-content: space-between; align-items: center;'>
<span style='text-align: left; font-size: 1rem; font-weight: 500; color: {text_dark};'>Moderate Positive Count</span>
<span style='text-align: right; font-size: 1rem; font-weight: 500;'>{sentiment_counts_AV[sentiment_counts_AV['sentiment'] == 'Moderate Positive']['count'].iloc[0]}</span>
</div>
<div style='display: flex; justify-content: space-between; align-items: center;'>
<span style='text-align: left; font-size: 1rem; font-weight: 500; color: {text_dark};'>Neutral Count</span>
<span style='text-align: right; font-size: 1rem; font-weight: 500;'>{sentiment_counts_AV[sentiment_counts_AV['sentiment'] == 'Neutral']['count'].iloc[0]}</span>
</div>
<div style='display: flex; justify-content: space-between; align-items: center;'>
<span style='text-align: left; font-size: 1rem; font-weight: 500; color: {text_dark};'>Moderate Negative Count</span>
<span style='text-align: right; font-size: 1rem; font-weight: 500;'>{sentiment_counts_AV[sentiment_counts_AV['sentiment'] == 'Moderate Negative']['count'].iloc[0]}</span>
</div>
<div style='display: flex; justify-content: space-between; align-items: center;'>
<span style='text-align: left; font-size: 1rem; font-weight: 500; color: {text_dark};'>Strong Negative Count</span>
<span style='text-align: right; font-size: 1rem; font-weight: 500;'>{sentiment_counts_AV[sentiment_counts_AV['sentiment'] == 'Strong Negative']['count'].iloc[0]}</span>
</div>
</div>
"""
st.markdown(total_sentiment_count_AV, unsafe_allow_html=True)
with chart_2:
tb_title = f"<h4 style='text-align: left; font-size: 0.9rem; font-weight: 500; margin-top: -15px; color: {text_dark};'>TextBlob Sentiment Score</h4>"
st.markdown(tb_title, unsafe_allow_html=True)
TB_chart = alt.Chart(sentiment_counts_TB).mark_bar(
opacity=0.7,
cornerRadiusBottomRight=5,
cornerRadiusTopRight=5,
color=alt.Gradient(
gradient='linear',
stops=[
alt.GradientStop(color=f'{black_dark}', offset=0),
alt.GradientStop(color=f'{black_light}', offset=1)
],
x1=0,
x2=1,
y1=0,
y2=0)
).encode(
x=alt.X('count:Q', axis=alt.Axis(grid=True, gridColor=f'{text_dark}')),
y=alt.Y('sentiment:O', title=None, sort=['Strong Positive', 'Moderate Positive', 'Neutral', 'Moderate Negative', 'Strong Negative']),
tooltip=[
alt.Tooltip("count:Q", title="Count"),
alt.Tooltip("sentiment:O", title="Category")]
).properties(
height=300,
width='container'
)
highlighted_bar_TB = alt.Chart(sentiment_counts_TB).mark_bar(
cornerRadiusBottomRight=5,
cornerRadiusTopRight=5,
color=alt.Gradient(
gradient='linear',
stops=[
alt.GradientStop(color=f'{black_dark}', offset=0),
alt.GradientStop(color='#4a6382', offset=1)
],
x1=0,
x2=1,
y1=0,
y2=0)
).encode(
x=alt.X('count:Q', axis=alt.Axis(grid=True, gridColor=f'{text_dark}')),
y=alt.Y('sentiment:O', title=None, sort=['Strong Positive', 'Moderate Positive', 'Neutral', 'Moderate Negative', 'Strong Negative']),
tooltip=[
alt.Tooltip("count:Q", title="Count"),
alt.Tooltip("sentiment:O", title="Category")]
).transform_filter(
alt.datum.sentiment == max_count_TB
).properties(
height=300,
width='container'
)
final_TB_chart = alt.layer(TB_chart, highlighted_bar_TB).configure_axis(
labels=False,
ticks=False,
title=None,
offset=0,
gridWidth=0.2
).configure_legend(
labelFontSize=0,
symbolSize=0,
title=None,
offset=0
).configure_view(
step=0
)
st.altair_chart(final_TB_chart, use_container_width=True)
# TOTAL SENTIMENT COUNT (TB)
total_sentiment_count_TB = f"""
<div style='margin-top: -30px; margin-bottom: 25px;'>
<div style='display: flex; justify-content: space-between; align-items: center;'>
<span style='text-align: left; font-size: 1rem; font-weight: 500; color: {text_dark};'>Strong Positive Count</span>
<span style='text-align: right; font-size: 1rem; font-weight: 500;'>{sentiment_counts_TB[sentiment_counts_TB['sentiment'] == 'Strong Positive']['count'].iloc[0]}</span>
</div>
<div style='display: flex; justify-content: space-between; align-items: center;'>
<span style='text-align: left; font-size: 1rem; font-weight: 500; color: {text_dark};'>Moderate Positive Count</span>
<span style='text-align: right; font-size: 1rem; font-weight: 500;'>{sentiment_counts_TB[sentiment_counts_TB['sentiment'] == 'Moderate Positive']['count'].iloc[0]}</span>
</div>
<div style='display: flex; justify-content: space-between; align-items: center;'>
<span style='text-align: left; font-size: 1rem; font-weight: 500; color: {text_dark};'>Neutral Count</span>
<span style='text-align: right; font-size: 1rem; font-weight: 500;'>{sentiment_counts_TB[sentiment_counts_TB['sentiment'] == 'Neutral']['count'].iloc[0]}</span>
</div>
<div style='display: flex; justify-content: space-between; align-items: center;'>