Borja Gómez Muñoz commited on
Commit
0949c3d
1 Parent(s): 47fe8f6

Application file + csv

Browse files
Files changed (2) hide show
  1. app.py +206 -0
  2. sentiment_data.csv +8 -0
app.py ADDED
@@ -0,0 +1,206 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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') ### YOUR LINE OF CODE HERE
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() ### YOUR LINE OF CODE HERE #### Extract columns into a list
21
+
22
+ r_sentiment = re.compile(".*sentiment")
23
+ sentiment_cols = list(filter(r_sentiment.match, col_list)) ### YOUR LINE OF CODE HERE
24
+
25
+ r_post = re.compile(".*post")
26
+ post_list = list(filter(r_post.match, col_list)) ### YOUR LINE OF CODE HERE
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 = perc_list + close_list + volume_list ### YOUR LINE OF CODE HERE
39
+
40
+ # Config for page
41
+ st.set_page_config(
42
+ page_title= 'Sentiment Stock Experiment', ### YOUR LINE OF CODE HERE
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
+ ### YOUR LINE OF CODE HERE
55
+ 'Start Date',
56
+ min(dfSentiment['timestamp']),
57
+ min_value=min(dfSentiment['timestamp']),
58
+ max_value=max(dfSentiment['timestamp'])
59
+ )
60
+
61
+
62
+ end_date_filter = st.date_input(
63
+ 'End Date',
64
+ max(dfSentiment['timestamp']),
65
+ min_value=min(dfSentiment['timestamp']),
66
+ max_value=max(dfSentiment['timestamp'])
67
+ )
68
+
69
+ sentiment_select = st.selectbox('Select Sentiment/Reddit Data', sentiment_cols) ### YOUR LINE OF CODE HERE
70
+ stock_select =st.selectbox('Select Stock Data', stocks_cols) ### YOUR LINE OF CODE HERE
71
+
72
+ # Banner with TSLA and Reddit images
73
+ tsla_logo = Image.open('./images/tsla_logo.png') ### YOUR LINE OF CODE HERE
74
+ reddit_logo = Image.open('./images/reddit_logo.png')
75
+ st.image([tsla_logo, reddit_logo], width=200)
76
+
77
+ # dashboard title
78
+ ### YOUR LINE OF CODE HERE
79
+ st.title('Sentiment Stock Experiment')
80
+
81
+ ## dataframe filter
82
+ # start date
83
+ dfSentiment = dfSentiment[dfSentiment['timestamp'] >= datetime(start_date_filter.year, start_date_filter.month, start_date_filter.day)]
84
+
85
+ # end date
86
+ dfSentiment = dfSentiment[dfSentiment['timestamp'] <= datetime(end_date_filter.year, end_date_filter.month, end_date_filter.day)] ### YOUR LINE OF CODE HERE
87
+ dfSentiment = dfSentiment.reset_index(drop=True)
88
+
89
+ # creating a single-element container
90
+ placeholder = st.empty() ### YOUR LINE OF CODE HERE
91
+
92
+ # near real-time / live feed simulation
93
+ for i in range(1, len(dfSentiment)-1):
94
+
95
+ # creating KPIs
96
+ last_close = dfSentiment['close'][i]
97
+ last_close_lag1 = dfSentiment['close'][i-1]
98
+ last_sentiment = dfSentiment[sentiment_select][i] ### YOUR LINE OF CODE HERE
99
+ last_sentiment_lag1 = dfSentiment[sentiment_select][i-1] ### YOUR LINE OF CODE HERE
100
+
101
+
102
+ with placeholder.container():
103
+
104
+ # create columns
105
+ kpi1, kpi2, kpi3 = st.columns(3)
106
+
107
+ # fill in those three columns with respective metrics or KPIs
108
+ kpi1.metric(
109
+ label='Sentiment Score',
110
+ value=round(last_sentiment, 3),
111
+ delta=round(last_sentiment_lag1, 3),
112
+ )
113
+
114
+ kpi2.metric(
115
+ label='Last Closing Price',
116
+ ### YOUR LINE 1 OF CODE HERE
117
+ value=round(last_close, 3),
118
+ ### YOUR LINE 2 OF CODE HERE
119
+ delta=round(last_close_lag1, 3),
120
+ )
121
+
122
+
123
+ # create two columns for charts
124
+ fig_col1, fig_col2 = st.columns(2)
125
+
126
+ with fig_col1:
127
+ # Add traces
128
+ fig=make_subplots(specs=[[{"secondary_y":True}]])
129
+
130
+
131
+ fig.add_trace(
132
+ go.Scatter(
133
+ x=dfSentiment['timestamp'][0:i],
134
+ y=dfSentiment[sentiment_select][0:i],
135
+ name=sentiment_select,
136
+ mode='lines',
137
+ hoverinfo='none',
138
+ )
139
+ )
140
+
141
+ if sentiment_select.startswith('perc') == True:
142
+ ### YOUR LINE OF CODE HERE
143
+ yaxis_label = '% Change Sentiment'
144
+
145
+ elif sentiment_select in sentiment_cols:
146
+ ### YOUR LINE OF CODE HERE
147
+ yaxis_label = 'Sentiment Score'
148
+
149
+ elif sentiment_select in post_list:
150
+ yaxis_label = 'Volume'
151
+
152
+ fig.layout.yaxis.title=yaxis_label
153
+
154
+ if stock_select.startswith('perc') == True:
155
+ fig.add_trace(
156
+ go.Scatter(
157
+ x=dfSentiment['timestamp'][0:i],
158
+ y=dfSentiment[stock_select][0:i],
159
+ name=stock_select,
160
+ mode='lines',
161
+ hoverinfo='none',
162
+ yaxis='y2',
163
+ )
164
+ )
165
+ fig.layout.yaxis2.title='% Change Stock Price ($US)'
166
+
167
+ elif stock_select == 'volume':
168
+ fig.add_trace(
169
+ go.Scatter(
170
+ x=dfSentiment['timestamp'][0:i],
171
+ y=dfSentiment[stock_select][0:i],
172
+ name=stock_select,
173
+ mode='lines',
174
+ hoverinfo='none',
175
+ yaxis='y2',
176
+ )
177
+ )
178
+
179
+ fig.layout.yaxis2.title="Shares Traded"
180
+
181
+
182
+ else:
183
+ fig.add_trace(
184
+ go.Scatter(
185
+ x=dfSentiment['timestamp'][0:i],
186
+ y=dfSentiment[stock_select][0:i],
187
+ name=stock_select,
188
+ mode='lines',
189
+ hoverinfo='none',
190
+ yaxis='y2',
191
+ )
192
+ )
193
+
194
+ fig.layout.yaxis2.title='Stock Price ($USD)'
195
+
196
+
197
+ fig.layout.xaxis.title='Timestamp'
198
+
199
+ # write the figure throught streamlit
200
+ ### YOUR LINE OF CODE HERE
201
+ st.plotly_chart(fig)
202
+
203
+
204
+ st.markdown('### Detailed Data View')
205
+ st.dataframe(dfSentiment.iloc[:, 1:][0:i])
206
+ time.sleep(1)
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-03,400,0.0,,0.9216578125953674,0.0,0.0,0.9935634190385992,-0.0723714310182734,0.0
3
+ 2,2022-07-04,1089,0.0,,0.9816401293783477,0.0,0.0,0.9216578125953674,0.06508089657925367,0.9656204536707714
4
+ 3,2022-07-05,16,699.20001,28193700,0.9899585247039795,0.0,inf,0.9816401293783477,0.008473976436659838,0.9644188222258981
5
+ 4,2022-07-06,36,695.20001,23951200,0.9599946339925131,699.20001,-0.0057208237168074405,0.9899585247039795,-0.030267824321656638,0.9771977626916134
6
+ 5,2022-07-07,144,733.63,27310200,0.9920109957456589,695.20001,0.05527904120714839,0.9599946339925131,0.033350563242206116,0.9806547181473838
7
+ 6,2022-07-08,4,752.28998,33343700,0.9752452969551086,733.63,0.025435137603424095,0.9920109957456589,-0.016900718704179342,0.9757503088977603
8
+ 7,2022-07-09,625,0.0,,0.9844982624053955,752.28998,-1.0,0.9752452969551086,0.009487833962569508,0.9839181850353876