bikrammaharjan commited on
Commit
0c142c0
1 Parent(s): 88ab604

TSLA stock sentiment

Browse files
TSLA_streamlit_app.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = ### YOUR LINE OF CODE HERE
17
+ dfSentiment = pd.read_csv('../Phase_I-Proof_of_concept/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.tolist()
22
+
23
+
24
+ r_sentiment = re.compile(".*sentiment")
25
+ sentiment_cols = list(filter(r_sentiment.match, col_list))
26
+
27
+ r_post = re.compile(".*post")
28
+ post_list = ist(filter(r_post.match, col_list))
29
+
30
+
31
+ r_perc= re.compile(".*perc")
32
+ perc_list = list(filter(r_perc.match, col_list))
33
+
34
+ r_close = re.compile(".*close")
35
+ close_list = list(filter(r_close.match, col_list))
36
+
37
+ r_volume = re.compile(".*volume")
38
+ volume_list = list(filter(r_volume.match, col_list))
39
+
40
+ sentiment_cols = sentiment_cols + post_list
41
+ stocks_cols = close_list + volume_list
42
+
43
+ # Config for page
44
+ st.set_page_config(
45
+ page_title= 'TSLA Bot',
46
+ page_icon='✅',
47
+ layout='wide',
48
+ )
49
+
50
+ with st.sidebar:
51
+ # FourthBrain logo to sidebar
52
+ fourthbrain_logo = Image.open('./images/fourthbrain_logo.png')
53
+ st.image([fourthbrain_logo], width=300)
54
+
55
+ # Date selection filters
56
+ start_date_filter = st.date_input(
57
+ 'Start Date',
58
+ min(dfSentiment['timestamp']),
59
+ min_value=min(dfSentiment['timestamp']),
60
+ max_value=max(dfSentiment['timestamp'])
61
+ )
62
+
63
+
64
+ end_date_filter = st.date_input(
65
+ 'End Date',
66
+ max(dfSentiment['timestamp']),
67
+ min_value=min(dfSentiment['timestamp']),
68
+ max_value=max(dfSentiment['timestamp'])
69
+ )
70
+
71
+ sentiment_select = st.selectbox('Select Sentiment/Reddit Data', sentiment_cols)
72
+ stock_select = st.selectbox('Select Stock Data', stocks_cols)
73
+
74
+ # Banner with TSLA and Reddit images
75
+ tsla_logo = Image.open('./images/tsla_logo.png')
76
+ reddit_logo = Image.open('./images/reddit_logo.png')
77
+ st.image([tsla_logo, reddit_logo], width=200)
78
+
79
+ # dashboard title
80
+ st.title('TSLA Subreddit and Stock Price')
81
+
82
+ ## dataframe filter
83
+ # start date
84
+ dfSentiment = dfSentiment[dfSentiment['timestamp'] >= datetime(start_date_filter.year, start_date_filter.month, start_date_filter.day)]
85
+
86
+ # end date
87
+ dfSentiment = dfSentiment[dfSentiment['timestamp'] <= datetime(end_date_filter.year, end_date_filter.month, end_date_filter.day)]
88
+ dfSentiment = dfSentiment.reset_index(drop=True)
89
+
90
+
91
+ # creating a single-element container
92
+ placeholder = st.empty()
93
+
94
+ # near real-time / live feed simulation
95
+ for i in range(1, len(dfSentiment)-1):
96
+
97
+ # creating KPIs
98
+ last_close = dfSentiment['close'][i]
99
+ last_close_lag1 = dfSentiment['close'][i-1]
100
+ last_sentiment = dfSentiment['sentiment_score'][i]
101
+ last_sentiment_lag1 = dfSentiment['sentiment_score'][i-1]
102
+
103
+
104
+ with placeholder.container():
105
+
106
+ # create columns
107
+ kpi1, kpi2 = st.columns(3)
108
+
109
+ # fill in those three columns with respective metrics or KPIs
110
+ kpi1.metric(
111
+ label='Sentiment Score',
112
+ value=round(last_sentiment, 3),
113
+ delta=round(last_sentiment_lag1, 3),
114
+ )
115
+
116
+ kpi2.metric(
117
+ label='Last Closing Price',
118
+ value=round(last_close),
119
+ delta=round(last_close - last_close_lag1)
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
+ yaxis_label = '% Change Sentiment'
143
+
144
+ elif sentiment_select in sentiment_cols:
145
+ yaxis_label = 'Sentiment Score'
146
+
147
+ elif sentiment_select in post_list:
148
+ yaxis_label = 'Volume'
149
+
150
+ fig.layout.yaxis.title=yaxis_label
151
+
152
+ if stock_select.startswith('perc') == True:
153
+ fig.add_trace(
154
+ go.Scatter(
155
+ x=dfSentiment['timestamp'][0:i],
156
+ y=dfSentiment[stock_select][0:i],
157
+ name=stock_select,
158
+ mode='lines',
159
+ hoverinfo='none',
160
+ yaxis='y2',
161
+ )
162
+ )
163
+ fig.layout.yaxis2.title='% Change Stock Price ($US)'
164
+
165
+ elif stock_select == 'volume':
166
+ fig.add_trace(
167
+ go.Scatter(
168
+ x=dfSentiment['timestamp'][0:i],
169
+ y=dfSentiment[stock_select][0:i],
170
+ name=stock_select,
171
+ mode='lines',
172
+ hoverinfo='none',
173
+ yaxis='y2',
174
+ )
175
+ )
176
+
177
+ fig.layout.yaxis2.title="Shares Traded"
178
+
179
+
180
+ else:
181
+ fig.add_trace(
182
+ go.Scatter(
183
+ x=dfSentiment['timestamp'][0:i],
184
+ y=dfSentiment[stock_select][0:i],
185
+ name=stock_select,
186
+ mode='lines',
187
+ hoverinfo='none',
188
+ yaxis='y2',
189
+ )
190
+ )
191
+
192
+ fig.layout.yaxis2.title='Stock Price ($USD)'
193
+
194
+
195
+ fig.layout.xaxis.title='Timestamp'
196
+
197
+ # write the figure throught streamlit
198
+ st.write(fig)
199
+
200
+
201
+ st.markdown('### Detailed Data View')
202
+ st.dataframe(dfSentiment.iloc[:, 1:][0:i])
203
+ time.sleep(1)
images/fourthbrain_logo.png ADDED
images/reddit_logo.png ADDED
images/tsla_logo.png ADDED