mnarahari commited on
Commit
17a28af
1 Parent(s): e842b9e

branch updated

Browse files
app.py ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ ### YOUR LINE OF CODE HERE
17
+ dfSentiment = pd.read_csv("../TSLASentimentAnalyzer/sentiment_data.csv")
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.values.tolist() ### YOUR LINE OF CODE HERE #### Extract columns into a list
22
+
23
+ r_sentiment = re.compile(".*sentiment")
24
+ sentiment_cols = list(filter(r_sentiment.match, col_list)) ### YOUR LINE OF CODE HERE
25
+
26
+ r_post = re.compile(".*post")
27
+ post_list = list(filter(r_post.match, col_list)) ### YOUR LINE OF CODE HERE
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 = perc_list + close_list + volume_list ### YOUR LINE OF CODE HERE
40
+
41
+ # Config for page
42
+ st.set_page_config(
43
+ page_title= 'TSLA Subreddit and Stock Price', ### YOUR LINE OF CODE HERE
44
+ page_icon='✅',
45
+ layout='wide',
46
+ )
47
+
48
+ with st.sidebar:
49
+ # FourthBrain logo to sidebar
50
+ fourthbrain_logo = Image.open('./images/fourthbrain_logo.png')
51
+ st.image([fourthbrain_logo], width=300)
52
+
53
+ # Date selection filters
54
+ start_date_filter = st.date_input(
55
+ ### YOUR LINE OF CODE HERE
56
+ 'Start Date',
57
+ min(dfSentiment['timestamp']),
58
+ min_value=min(dfSentiment['timestamp']),
59
+ max_value=max(dfSentiment['timestamp'])
60
+ )
61
+
62
+
63
+ end_date_filter = st.date_input(
64
+ 'End Date',
65
+ max(dfSentiment['timestamp']),
66
+ min_value=min(dfSentiment['timestamp']),
67
+ max_value=max(dfSentiment['timestamp'])
68
+ )
69
+
70
+ sentiment_select = st.selectbox('Select Sentiment/Reddit Data', ('sentiment_score', 'sentiment_score_lag1',
71
+ 'perc_change_sentiment', 'sentiment_SMA3mo')) ### YOUR LINE OF CODE HERE
72
+ stock_select = st.selectbox('Select Stock Data', ('close', 'close_lag1', 'perc_change_close',
73
+ 'volume')) ### YOUR LINE OF CODE HERE
74
+
75
+ # Banner with TSLA and Reddit images
76
+ tsla_logo = Image.open('./images/tsla_logo.png') ### YOUR LINE OF CODE HERE
77
+ reddit_logo = Image.open('./images/reddit_logo.png')
78
+ st.image([tsla_logo, reddit_logo], width=200)
79
+
80
+ # dashboard title
81
+ ### YOUR LINE OF CODE HERE
82
+ st.title('TSLA Subreddit and Stock Price')
83
+
84
+ ## dataframe filter
85
+ # start date
86
+ dfSentiment = dfSentiment[dfSentiment['timestamp'] >= datetime(start_date_filter.year, start_date_filter.month, start_date_filter.day)]
87
+
88
+ # end date
89
+ dfSentiment = dfSentiment[dfSentiment['timestamp'] <= datetime(end_date_filter.year, end_date_filter.month, end_date_filter.day)] ### YOUR LINE OF CODE HERE
90
+ dfSentiment = dfSentiment.reset_index(drop=True)
91
+
92
+
93
+ # creating a single-element container
94
+ placeholder = st.empty() ### YOUR LINE OF CODE HERE
95
+
96
+ # near real-time / live feed simulation
97
+ for i in range(1, len(dfSentiment)-1):
98
+
99
+ # creating KPIs
100
+ last_close = dfSentiment['close'][i]
101
+ last_close_lag1 = dfSentiment['close'][i-1]
102
+ last_sentiment = dfSentiment['sentiment_score'][i] ### YOUR LINE OF CODE HERE
103
+ last_sentiment_lag1 = dfSentiment['sentiment_score'][i-1] ### YOUR LINE OF CODE HERE
104
+
105
+
106
+ with placeholder.container():
107
+
108
+ # create columns
109
+ kpi1, kpi2, kip3 = st.columns(3)
110
+
111
+ # fill in those three columns with respective metrics or KPIs
112
+ kpi1.metric(
113
+ label='Sentiment Score',
114
+ value=round(last_sentiment, 3),
115
+ delta=round(last_sentiment_lag1, 3),
116
+ )
117
+
118
+ kpi2.metric(
119
+ label='Last Closing Price',
120
+ ### YOUR LINE 1 OF CODE HERE
121
+ value=round(last_close, 3),
122
+ ### YOUR LINE 2 OF CODE HERE
123
+ delta=round(last_close_lag1, 3)
124
+ )
125
+
126
+
127
+ # create two columns for charts
128
+ fig_col1, fig_col2 = st.columns(2)
129
+
130
+ with fig_col1:
131
+ # Add traces
132
+ fig=make_subplots(specs=[[{"secondary_y":True}]])
133
+
134
+
135
+ fig.add_trace(
136
+ go.Scatter(
137
+ x=dfSentiment['timestamp'][0:i],
138
+ y=dfSentiment[sentiment_select][0:i],
139
+ name=sentiment_select,
140
+ mode='lines',
141
+ hoverinfo='none',
142
+ )
143
+ )
144
+
145
+ if sentiment_select.startswith('perc') == True:
146
+ ### YOUR LINE OF CODE HERE
147
+ yaxis_label = 'Percentage'
148
+
149
+ elif sentiment_select in sentiment_cols:
150
+ ### YOUR LINE OF CODE HERE
151
+ yaxis_label = 'Sentiment'
152
+
153
+ elif sentiment_select in post_list:
154
+ yaxis_label = 'Volume'
155
+
156
+ fig.layout.yaxis.title=yaxis_label
157
+
158
+ if stock_select.startswith('perc') == True:
159
+ fig.add_trace(
160
+ go.Scatter(
161
+ x=dfSentiment['timestamp'][0:i],
162
+ y=dfSentiment[stock_select][0:i],
163
+ name=stock_select,
164
+ mode='lines',
165
+ hoverinfo='none',
166
+ yaxis='y2',
167
+ )
168
+ )
169
+ fig.layout.yaxis2.title='% Change Stock Price ($US)'
170
+
171
+ elif stock_select == 'volume':
172
+ fig.add_trace(
173
+ go.Scatter(
174
+ x=dfSentiment['timestamp'][0:i],
175
+ y=dfSentiment[stock_select][0:i],
176
+ name=stock_select,
177
+ mode='lines',
178
+ hoverinfo='none',
179
+ yaxis='y2',
180
+ )
181
+ )
182
+
183
+ fig.layout.yaxis2.title="Shares Traded"
184
+
185
+
186
+ else:
187
+ fig.add_trace(
188
+ go.Scatter(
189
+ x=dfSentiment['timestamp'][0:i],
190
+ y=dfSentiment[stock_select][0:i],
191
+ name=stock_select,
192
+ mode='lines',
193
+ hoverinfo='none',
194
+ yaxis='y2',
195
+ )
196
+ )
197
+
198
+ fig.layout.yaxis2.title='Stock Price ($USD)'
199
+
200
+
201
+ fig.layout.xaxis.title='Timestamp'
202
+
203
+ # write the figure throught streamlit
204
+ ### YOUR LINE OF CODE HERE
205
+ st.plotly_chart(fig)
206
+
207
+ st.markdown('### Detailed Data View')
208
+ st.dataframe(dfSentiment.iloc[:, 1:][0:i])
209
+ 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.22.3
2
+ pandas==1.4.3
3
+ Pillow==9.2.0
4
+ plotly==5.6.0
5
+ streamlit==1.10.0
sentiment_data.csv ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ ,timestamp,counter,close,volume,sentiment_score,close_lag1,perc_change_close,sentiment_score_lag1,perc_change_sentiment,sentiment_SMA3mo
2
+ 1,2022-03-01,298908,1099.56995,40225400,0.8713025534362123,932.0,0.17979608369098718,0.8656592077138473,0.006519130937529968,0.0
3
+ 2,2022-04-01,204020,1145.44995,45377900,0.8560624771779126,1099.56995,0.04172540364530686,0.8713025534362123,-0.01749114150784527,0.8643414127759907
4
+ 3,2022-05-01,394149,952.62,48324400,0.8597976830753967,1145.44995,-0.1683442825240858,0.8560624771779126,0.004363239830108604,0.8623875712298404
5
+ 4,2022-06-01,78141,775.0,40931000,0.8685562434743662,952.62,-0.18645419999580107,0.8597976830753967,0.010186769017149638,0.8614721345758918
6
+ 5,2022-07-01,190969,681.78998,24781500,0.8626362161723645,775.0,-0.12027099354838708,0.8685562434743662,-0.006815940068913239,0.8636633809073758