Spaces:
Runtime error
Runtime error
Nick Bolton
commited on
Commit
•
6b2a6c2
1
Parent(s):
d101da9
first commit
Browse files- app.py +200 -0
- data/sentiment_data.csv +18 -0
- images/fourthbrain_logo.png +0 -0
- images/nvidia_logo.png +0 -0
- images/reddit_logo.png +0 -0
- images/tsla_logo.png +0 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,200 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import date
|
2 |
+
from datetime import datetime
|
3 |
+
import re
|
4 |
+
|
5 |
+
import numpy as np
|
6 |
+
import pandas as pd
|
7 |
+
from PIL import Image
|
8 |
+
import plotly.express as px
|
9 |
+
import plotly.graph_objects as go
|
10 |
+
import streamlit as st
|
11 |
+
import time
|
12 |
+
|
13 |
+
from plotly.subplots import make_subplots
|
14 |
+
|
15 |
+
# Read CSV file into pandas and extract timestamp data
|
16 |
+
dfSentiment = pd.read_csv('data/sentiment_data.csv')
|
17 |
+
dfSentiment['timestamp'] = [datetime.strptime(dt, '%Y-%m-%d') for dt in dfSentiment['timestamp'].tolist()]
|
18 |
+
|
19 |
+
# Multi-select columns to build chart
|
20 |
+
col_list = dfSentiment.columns.tolist()
|
21 |
+
|
22 |
+
r_sentiment = re.compile(".*sentiment")
|
23 |
+
sentiment_cols = list(filter(r_sentiment.match, col_list))
|
24 |
+
|
25 |
+
r_post = re.compile(".*post")
|
26 |
+
post_list = list(filter(r_post.match, col_list))
|
27 |
+
|
28 |
+
r_perc= re.compile(".*perc")
|
29 |
+
perc_list = list(filter(r_perc.match, col_list))
|
30 |
+
|
31 |
+
r_close = re.compile(".*close")
|
32 |
+
close_list = list(filter(r_close.match, col_list))
|
33 |
+
|
34 |
+
r_volume = re.compile(".*volume")
|
35 |
+
volume_list = list(filter(r_volume.match, col_list))
|
36 |
+
|
37 |
+
sentiment_cols = sentiment_cols + post_list
|
38 |
+
stocks_cols = close_list + volume_list
|
39 |
+
|
40 |
+
# Config for page
|
41 |
+
st.set_page_config(
|
42 |
+
page_title="Tesla Reddit Sentiment",
|
43 |
+
page_icon='✅',
|
44 |
+
layout='wide',
|
45 |
+
)
|
46 |
+
|
47 |
+
with st.sidebar:
|
48 |
+
# FourthBrain logo to sidebar
|
49 |
+
fourthbrain_logo = Image.open('./images/fourthbrain_logo.png')
|
50 |
+
st.image([fourthbrain_logo], width=300)
|
51 |
+
|
52 |
+
# Date selection filters
|
53 |
+
start_date_filter = st.date_input(
|
54 |
+
'Start Date',
|
55 |
+
min(dfSentiment['timestamp']),
|
56 |
+
min_value=min(dfSentiment['timestamp']),
|
57 |
+
max_value=max(dfSentiment['timestamp'])
|
58 |
+
)
|
59 |
+
|
60 |
+
|
61 |
+
end_date_filter = st.date_input(
|
62 |
+
'End Date',
|
63 |
+
max(dfSentiment['timestamp']),
|
64 |
+
min_value=min(dfSentiment['timestamp']),
|
65 |
+
max_value=max(dfSentiment['timestamp'])
|
66 |
+
)
|
67 |
+
|
68 |
+
sentiment_select = st.selectbox("Sentiment", sentiment_cols)
|
69 |
+
stock_select = st.selectbox("Stock", stocks_cols)
|
70 |
+
|
71 |
+
# Banner with TSLA and Reddit images
|
72 |
+
company_logo = Image.open('./images/tsla_logo.png')
|
73 |
+
reddit_logo = Image.open('./images/reddit_logo.png')
|
74 |
+
st.image([company_logo, reddit_logo], width=200)
|
75 |
+
|
76 |
+
# dashboard title
|
77 |
+
st.title("Tesla Reddit Sentiment and Stock Price")
|
78 |
+
|
79 |
+
## dataframe filter
|
80 |
+
# start date
|
81 |
+
dfSentiment = dfSentiment[dfSentiment['timestamp'] >= datetime(start_date_filter.year, start_date_filter.month, start_date_filter.day)]
|
82 |
+
|
83 |
+
# end date
|
84 |
+
dfSentiment = dfSentiment[dfSentiment['timestamp'] <= datetime(end_date_filter.year, end_date_filter.month, end_date_filter.day)]
|
85 |
+
dfSentiment = dfSentiment.reset_index(drop=True)
|
86 |
+
|
87 |
+
|
88 |
+
# creating a single-element container
|
89 |
+
placeholder = st.empty()
|
90 |
+
|
91 |
+
# near real-time / live feed simulation
|
92 |
+
for i in range(1, len(dfSentiment)-1):
|
93 |
+
|
94 |
+
# creating KPIs
|
95 |
+
last_close = dfSentiment['close'][i]
|
96 |
+
last_close_lag1 = dfSentiment['close'][i-1]
|
97 |
+
last_sentiment = dfSentiment['sentiment_score'][i-1]
|
98 |
+
last_sentiment_lag1 = dfSentiment['sentiment_score'][i-1]
|
99 |
+
|
100 |
+
|
101 |
+
with placeholder.container():
|
102 |
+
|
103 |
+
# create columns
|
104 |
+
kpi1, kpi2, kpi3 = st.columns(3)
|
105 |
+
|
106 |
+
# fill in those three columns with respective metrics or KPIs
|
107 |
+
kpi1.metric(
|
108 |
+
label='Sentiment Score',
|
109 |
+
value=round(last_sentiment, 3),
|
110 |
+
delta=round(last_sentiment_lag1, 3),
|
111 |
+
)
|
112 |
+
|
113 |
+
kpi2.metric(
|
114 |
+
label='Last Closing Price',
|
115 |
+
value=round(last_close, 3),
|
116 |
+
delta=round(last_close_lag1, 3),
|
117 |
+
)
|
118 |
+
|
119 |
+
|
120 |
+
# create two columns for charts
|
121 |
+
fig_col1, fig_col2 = st.columns(2)
|
122 |
+
|
123 |
+
with fig_col1:
|
124 |
+
# Add traces
|
125 |
+
fig=make_subplots(specs=[[{"secondary_y":True}]])
|
126 |
+
|
127 |
+
|
128 |
+
fig.add_trace(
|
129 |
+
go.Scatter(
|
130 |
+
x=dfSentiment['timestamp'][0:i],
|
131 |
+
y=dfSentiment[sentiment_select][0:i],
|
132 |
+
name=sentiment_select,
|
133 |
+
mode='lines',
|
134 |
+
hoverinfo='none',
|
135 |
+
)
|
136 |
+
)
|
137 |
+
|
138 |
+
if sentiment_select.startswith('perc') == True:
|
139 |
+
y_axis_label = 'Percentage'
|
140 |
+
|
141 |
+
elif sentiment_select in sentiment_cols:
|
142 |
+
y_axis_label = 'Sentiment'
|
143 |
+
|
144 |
+
elif sentiment_select in post_list:
|
145 |
+
y_axis_label = 'Volume'
|
146 |
+
|
147 |
+
fig.layout.yaxis.title=y_axis_label
|
148 |
+
|
149 |
+
if stock_select.startswith('perc') == True:
|
150 |
+
fig.add_trace(
|
151 |
+
go.Scatter(
|
152 |
+
x=dfSentiment['timestamp'][0:i],
|
153 |
+
y=dfSentiment[stock_select][0:i],
|
154 |
+
name=stock_select,
|
155 |
+
mode='lines',
|
156 |
+
hoverinfo='none',
|
157 |
+
yaxis='y2',
|
158 |
+
)
|
159 |
+
)
|
160 |
+
fig.layout.yaxis2.title='% Change Stock Price ($US)'
|
161 |
+
|
162 |
+
elif stock_select == 'volume':
|
163 |
+
fig.add_trace(
|
164 |
+
go.Scatter(
|
165 |
+
x=dfSentiment['timestamp'][0:i],
|
166 |
+
y=dfSentiment[stock_select][0:i],
|
167 |
+
name=stock_select,
|
168 |
+
mode='lines',
|
169 |
+
hoverinfo='none',
|
170 |
+
yaxis='y2',
|
171 |
+
)
|
172 |
+
)
|
173 |
+
|
174 |
+
fig.layout.yaxis2.title="Shares Traded"
|
175 |
+
|
176 |
+
|
177 |
+
else:
|
178 |
+
fig.add_trace(
|
179 |
+
go.Scatter(
|
180 |
+
x=dfSentiment['timestamp'][0:i],
|
181 |
+
y=dfSentiment[stock_select][0:i],
|
182 |
+
name=stock_select,
|
183 |
+
mode='lines',
|
184 |
+
hoverinfo='none',
|
185 |
+
yaxis='y2',
|
186 |
+
)
|
187 |
+
)
|
188 |
+
|
189 |
+
fig.layout.yaxis2.title='Stock Price ($USD)'
|
190 |
+
|
191 |
+
|
192 |
+
fig.layout.xaxis.title='Timestamp'
|
193 |
+
|
194 |
+
# write the figure throught streamlit
|
195 |
+
st.write(fig)
|
196 |
+
|
197 |
+
|
198 |
+
st.markdown('### Detailed Data View')
|
199 |
+
st.dataframe(dfSentiment.iloc[:, 1:][0:i], width=500)
|
200 |
+
time.sleep(1)
|
data/sentiment_data.csv
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
,timestamp,counter,close,volume,sentiment_score,close_lag1,perc_change_close,sentiment_score_lag1,perc_change_sentiment,sentiment_SMA3mo
|
2 |
+
1,2021-03-01,3519376,667.92999,942452400,0.8834596920305732,675.5,-0.011206528497409362,0.8913090358317738,-0.008806534530277223,0.0
|
3 |
+
2,2021-04-01,682276,709.44,678539700,0.886186366941392,667.92999,0.06214724689933459,0.8834596920305732,0.003086360289456685,0.8869850316012463
|
4 |
+
3,2021-05-01,619369,625.21997,625175800,0.8932396112130588,709.44,-0.1187133936626072,0.886186366941392,0.007959098147730066,0.8876285567283414
|
5 |
+
4,2021-06-01,56169,679.70001,519921900,0.8921351734595963,625.21997,0.08713739581926667,0.8932396112130588,-0.0012364406365305593,0.890520383871349
|
6 |
+
5,2021-07-01,19044,687.20001,448449800,0.8929486063079558,679.70001,0.011034279666996032,0.8921351734595963,0.0009117820623585036,0.8927744636602037
|
7 |
+
6,2021-08-01,64,735.71997,381324900,0.8806141242384911,687.20001,0.07060529582937575,0.8929486063079558,-0.01381320490600653,0.8885659680020144
|
8 |
+
7,2021-09-01,7921,775.47998,390171300,0.8813562647680219,735.71997,0.054042314496370085,0.8806141242384911,0.0008427533798331643,0.8849729984381564
|
9 |
+
8,2021-10-01,155236,1114.0,528934600,0.8845597780900558,775.47998,0.4365296703081878,0.8813562647680219,0.0036347541284875644,0.8821767223655229
|
10 |
+
9,2021-11-01,1343281,1144.76001,649111500,0.8937287590337067,1114.0,0.027612217235188478,0.8845597780900558,0.010365586555889553,0.8865482672972614
|
11 |
+
10,2021-12-01,122500,1056.78003,510055900,0.890013679265976,1144.76001,-0.07685451905329928,0.8937287590337067,-0.004156831398988991,0.8894340721299128
|
12 |
+
11,2022-01-01,498436,936.71997,638668800,0.9025504858399248,1056.78003,-0.11360931943424396,0.890013679265976,0.014086083018733339,0.8954309747132024
|
13 |
+
12,2022-02-01,96100,870.42999,463708900,0.8847938576052266,936.71997,-0.07076819340149225,0.9025504858399248,-0.01967383377803372,0.8924526742370423
|
14 |
+
13,2022-03-01,38416,1077.59998,576424300,0.8887287633759635,870.42999,0.23800879149396034,0.8847938576052266,0.004447257106177405,0.8920243689403716
|
15 |
+
14,2022-04-01,198916,870.76001,506986600,0.8823608437995739,1077.59998,-0.19194503882600294,0.8887287633759635,-0.007165200271228026,0.8852944882602546
|
16 |
+
15,2022-05-01,320356,758.26001,649407200,0.9085272860190051,870.76001,-0.1291974811750944,0.8823608437995739,0.029655035582443468,0.8932056310648475
|
17 |
+
16,2022-06-01,107584,673.41998,670166000,0.8913517594337463,758.26001,-0.11188778107921049,0.9085272860190051,-0.01890479994334424,0.8940799630841084
|
18 |
+
17,2022-07-01,19600,752.28998,137580300,0.8903145917824337,673.41998,0.11711859217482677,0.8913517594337463,-0.00116358961581179,0.8967312124117283
|
images/fourthbrain_logo.png
ADDED
images/nvidia_logo.png
ADDED
images/reddit_logo.png
ADDED
images/tsla_logo.png
ADDED
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy==1.23.0
|
2 |
+
pandas==1.4.2
|
3 |
+
Pillow==9.2.0
|
4 |
+
plotly==5.9.0
|
5 |
+
streamlit==1.10.0
|