adding files
Browse files- app.py +190 -0
- images/fourthbrain_logo.png +0 -0
- images/reddit_logo.png +0 -0
- images/tsla_logo.png +0 -0
- requirements.txt +5 -0
- sentiment_data.csv +8 -0
app.py
ADDED
@@ -0,0 +1,190 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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('./sentiment_data.csv')
|
17 |
+
|
18 |
+
dfSentiment['timestamp'] = [datetime.strptime(dt, '%Y-%m-%d') for dt in dfSentiment['timestamp'].tolist()]
|
19 |
+
|
20 |
+
# Multi-select columns to build chart
|
21 |
+
col_list = dfSentiment.columns.tolist()
|
22 |
+
|
23 |
+
r_sentiment = re.compile(".*sentiment")
|
24 |
+
sentiment_cols = list(filter(r_sentiment.match, col_list))
|
25 |
+
|
26 |
+
r_post = re.compile(".*post")
|
27 |
+
post_list = list(filter(r_post.match, col_list))
|
28 |
+
|
29 |
+
r_perc= re.compile(".*perc")
|
30 |
+
perc_list = list(filter(r_perc.match, col_list))
|
31 |
+
|
32 |
+
r_close = re.compile(".*close")
|
33 |
+
close_list = list(filter(r_close.match, col_list))
|
34 |
+
|
35 |
+
r_volume = re.compile(".*volume")
|
36 |
+
volume_list = list(filter(r_volume.match, col_list))
|
37 |
+
|
38 |
+
sentiment_cols = sentiment_cols + post_list
|
39 |
+
stocks_cols = close_list + volume_list
|
40 |
+
|
41 |
+
with st.sidebar:
|
42 |
+
# FourthBrain logo to sidebar
|
43 |
+
fourthbrain_logo = Image.open('./images/fourthbrain_logo.png')
|
44 |
+
st.image([fourthbrain_logo], width=300)
|
45 |
+
|
46 |
+
# Date selection filters
|
47 |
+
start_date_filter = st.date_input(
|
48 |
+
'Start Date',
|
49 |
+
min(dfSentiment['timestamp']),
|
50 |
+
min_value=min(dfSentiment['timestamp']),
|
51 |
+
max_value=max(dfSentiment['timestamp'])
|
52 |
+
)
|
53 |
+
|
54 |
+
|
55 |
+
end_date_filter = st.date_input(
|
56 |
+
'End Date',
|
57 |
+
max(dfSentiment['timestamp']),
|
58 |
+
min_value=min(dfSentiment['timestamp']),
|
59 |
+
max_value=max(dfSentiment['timestamp'])
|
60 |
+
)
|
61 |
+
|
62 |
+
sentiment_select = st.selectbox('Select Sentiment/Reddit Data', sentiment_cols)
|
63 |
+
stock_select = st.selectbox('Select Stock Data', stocks_cols)
|
64 |
+
|
65 |
+
# Banner with TSLA and Reddit images
|
66 |
+
tsla_logo = Image.open('./images/tsla_logo.png')
|
67 |
+
reddit_logo = Image.open('./images/reddit_logo.png')
|
68 |
+
st.image([tsla_logo, reddit_logo], width=200)
|
69 |
+
|
70 |
+
# dashboard title
|
71 |
+
st.title('TSLA Subreddit and Stock Price')
|
72 |
+
|
73 |
+
# dataframe filter
|
74 |
+
dfSentiment = dfSentiment[dfSentiment['timestamp'] >= datetime(start_date_filter.year, start_date_filter.month, start_date_filter.day)]
|
75 |
+
dfSentiment = dfSentiment[dfSentiment['timestamp'] <= datetime(end_date_filter.year, end_date_filter.month, end_date_filter.day)]
|
76 |
+
dfSentiment = dfSentiment.reset_index(drop=True)
|
77 |
+
|
78 |
+
|
79 |
+
# creating a single-element container
|
80 |
+
placeholder = st.empty()
|
81 |
+
|
82 |
+
# near real-time / live feed simulation
|
83 |
+
for i in range(1, len(dfSentiment)-1):
|
84 |
+
|
85 |
+
# creating KPIs
|
86 |
+
last_close = dfSentiment['close'][i]
|
87 |
+
last_close_lag1 = dfSentiment['close'][i-1]
|
88 |
+
last_sentiment = dfSentiment['sentiment_score'][i]
|
89 |
+
last_sentiment_lag1 = dfSentiment['sentiment_score'][i-1]
|
90 |
+
|
91 |
+
|
92 |
+
with placeholder.container():
|
93 |
+
|
94 |
+
# create columns
|
95 |
+
kpi1, kpi2 = st.columns(2)
|
96 |
+
|
97 |
+
# fill in those three columns with respective metrics or KPIs
|
98 |
+
kpi1.metric(
|
99 |
+
label='Sentiment Score',
|
100 |
+
value=round(last_sentiment, 3),
|
101 |
+
delta=round(last_sentiment_lag1, 3),
|
102 |
+
)
|
103 |
+
|
104 |
+
kpi2.metric(
|
105 |
+
label='Last Closing Price',
|
106 |
+
value=round(last_close),
|
107 |
+
delta=round(last_close - last_close_lag1)
|
108 |
+
)
|
109 |
+
|
110 |
+
|
111 |
+
# create two columns for charts
|
112 |
+
fig_col1, fig_col2 = st.columns(2)
|
113 |
+
|
114 |
+
with fig_col1:
|
115 |
+
# Add traces
|
116 |
+
fig=make_subplots(specs=[[{"secondary_y":True}]])
|
117 |
+
|
118 |
+
|
119 |
+
fig.add_trace(
|
120 |
+
go.Scatter(
|
121 |
+
x=dfSentiment['timestamp'][0:i],
|
122 |
+
y=dfSentiment[sentiment_select][0:i],
|
123 |
+
name=sentiment_select,
|
124 |
+
mode='lines',
|
125 |
+
hoverinfo='none',
|
126 |
+
)
|
127 |
+
)
|
128 |
+
|
129 |
+
if sentiment_select.startswith('perc') == True:
|
130 |
+
yaxis_label = '% Change Sentiment'
|
131 |
+
|
132 |
+
elif sentiment_select in sentiment_cols:
|
133 |
+
yaxis_label = 'Sentiment Score'
|
134 |
+
|
135 |
+
elif sentiment_select in post_list:
|
136 |
+
yaxis_label = 'Volume'
|
137 |
+
|
138 |
+
fig.layout.yaxis.title=yaxis_label
|
139 |
+
|
140 |
+
if stock_select.startswith('perc') == True:
|
141 |
+
fig.add_trace(
|
142 |
+
go.Scatter(
|
143 |
+
x=dfSentiment['timestamp'][0:i],
|
144 |
+
y=dfSentiment[stock_select][0:i],
|
145 |
+
name=stock_select,
|
146 |
+
mode='lines',
|
147 |
+
hoverinfo='none',
|
148 |
+
yaxis='y2',
|
149 |
+
)
|
150 |
+
)
|
151 |
+
fig.layout.yaxis2.title='% Change Stock Price ($US)'
|
152 |
+
|
153 |
+
elif stock_select == 'volume':
|
154 |
+
fig.add_trace(
|
155 |
+
go.Scatter(
|
156 |
+
x=dfSentiment['timestamp'][0:i],
|
157 |
+
y=dfSentiment[stock_select][0:i],
|
158 |
+
name=stock_select,
|
159 |
+
mode='lines',
|
160 |
+
hoverinfo='none',
|
161 |
+
yaxis='y2',
|
162 |
+
)
|
163 |
+
)
|
164 |
+
|
165 |
+
fig.layout.yaxis2.title="Shares Traded"
|
166 |
+
|
167 |
+
|
168 |
+
else:
|
169 |
+
fig.add_trace(
|
170 |
+
go.Scatter(
|
171 |
+
x=dfSentiment['timestamp'][0:i],
|
172 |
+
y=dfSentiment[stock_select][0:i],
|
173 |
+
name=stock_select,
|
174 |
+
mode='lines',
|
175 |
+
hoverinfo='none',
|
176 |
+
yaxis='y2',
|
177 |
+
)
|
178 |
+
)
|
179 |
+
|
180 |
+
fig.layout.yaxis2.title='Stock Price ($USD)'
|
181 |
+
|
182 |
+
|
183 |
+
fig.layout.xaxis.title='Timestamp'
|
184 |
+
|
185 |
+
st.write(fig)
|
186 |
+
|
187 |
+
|
188 |
+
st.markdown('### Detailed Data View')
|
189 |
+
st.dataframe(dfSentiment.iloc[:, 1:][0:i])
|
190 |
+
time.sleep(1)
|
images/fourthbrain_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
|
sentiment_data.csv
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
,timestamp,counter,close,volume,sentiment_score,close_lag1,perc_change_close,sentiment_score_lag1,perc_change_sentiment,sentiment_SMA3mo
|
2 |
+
1,2022-07-02,361,0.0,,0.8339518653719049,681.78998,-1.0,0.9120607078075409,-0.0856399598919222,0.0
|
3 |
+
2,2022-07-03,576,0.0,,0.8980068961779276,0.0,0.0,0.8339518653719049,0.07680902635484489,0.8813398231191245
|
4 |
+
3,2022-07-04,441,0.0,,0.9187162092753819,0.0,0.0,0.8980068961779276,0.023061418777068066,0.8835583236084048
|
5 |
+
4,2022-07-05,4,699.20001,28193700,0.9892416596412659,0.0,inf,0.9187162092753819,0.07676521830556299,0.9353215883648586
|
6 |
+
5,2022-07-06,324,695.20001,23951200,0.9028889437516531,699.20001,-0.0057208237168074405,0.9892416596412659,-0.08729183111933171,0.9369489375561003
|
7 |
+
6,2022-07-08,225,752.28998,33343700,0.9030517021814982,695.20001,0.08212020883026165,0.9028889437516531,0.00018026406345043536,0.9317274351914723
|
8 |
+
7,2022-07-09,144,0.0,,0.9530749370654424,752.28998,-1.0,0.9030517021814982,0.055393544758404537,0.9196718609995312
|