sfmajors commited on
Commit
9bbed42
1 Parent(s): 34ae809

First Push

Browse files
Files changed (2) hide show
  1. app.py +200 -0
  2. 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("../TSLASentimentAnalyzer/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 = list(dfSentiment.columns)
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 = perc_list + close_list + volume_list
39
+
40
+ # Config for page
41
+ st.set_page_config(
42
+ page_title='Stocks v Sentiments',
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 = "sentiment_score"
69
+ stock_select = "close"
70
+
71
+ # Banner with TSLA and Reddit images
72
+ tsla_logo = Image.open('./images/tsla_logo.png')
73
+ reddit_logo = Image.open('./images/reddit_logo.png')
74
+ st.image([tsla_logo, reddit_logo], width=200)
75
+
76
+ # dashboard title
77
+ st.title("Stocks vs Sentiment")
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]
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
+ yaxis_label = "Perc"
140
+
141
+ elif sentiment_select in sentiment_cols:
142
+ yaxis_label = "Sentiment"
143
+
144
+ elif sentiment_select in post_list:
145
+ yaxis_label = 'Volume'
146
+
147
+ fig.layout.yaxis.title=yaxis_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])
200
+ time.sleep(1)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ numpy==1.23.0
2
+ pandas==1.4.3
3
+ Pillow==9.2.0
4
+ plotly==5.9.0
5
+ streamlit==1.10.0