JUpton commited on
Commit
cbab386
1 Parent(s): 8b4ec6d

First Commit

Browse files
.DS_Store ADDED
Binary file (6.15 kB). View file
 
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 = dfSentiment.columns.tolist() ### Extract columns into a list
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='TSLA Bot',
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('Select Sentiment/Reddit Data', sentiment_cols)
69
+ stock_select = st.selectbox('Select Stock Data', stocks_cols)
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('TSLA Subreddit 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]
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),
116
+ delta=round(last_close - last_close_lag1)
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 = '% Change Sentiment'
140
+
141
+ elif sentiment_select in sentiment_cols:
142
+ yaxis_label = 'Sentiment Score'
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)
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.1
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,7 @@
 
 
 
 
 
 
 
 
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,304175,1099.56995,40225400,0.8751397946606512,932.0,0.17979608369098718,0.875910526762406,-0.0008799210401131382,0.0
3
+ 2,2022-04-01,172980,1145.44995,45377900,0.8389155454533075,1099.56995,0.04172540364530686,0.8751397946606512,-0.041392528860363605,0.8633219556254549
4
+ 3,2022-05-01,382725,952.62,48324400,0.8546658017017223,1145.44995,-0.1683442825240858,0.8389155454533075,0.018774543318188508,0.856240380605227
5
+ 4,2022-06-01,33600,775.0,40931000,0.8597877576947213,952.62,-0.18645419999580107,0.8546658017017223,0.005992934294083847,0.851123034949917
6
+ 5,2022-07-01,1156,681.78998,24781500,0.8699657443691703,775.0,-0.12027099354838708,0.8597877576947213,0.011837789714217877,0.8614731012552047
7
+ 6,2022-08-01,123904,0.0,,0.8506387988613411,681.78998,-1.0,0.8699657443691703,-0.022215754623584133,0.8601307669750776