ChrisFourthBrain commited on
Commit
61a3e5c
·
1 Parent(s): afe5463

Upload AAPL_streamlit_app.py

Browse files
Files changed (1) hide show
  1. AAPL_streamlit_app.py +196 -0
AAPL_streamlit_app.py ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
16
+ dfSentiment = pd.read_csv('./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()
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='AAPL 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/AAPL-logo.jpg')
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('AAPL Subreddit and Stock Price')
78
+
79
+ # dataframe filter
80
+ dfSentiment = dfSentiment[dfSentiment['timestamp'] >= datetime(start_date_filter.year, start_date_filter.month, start_date_filter.day)]
81
+ dfSentiment = dfSentiment[dfSentiment['timestamp'] <= datetime(end_date_filter.year, end_date_filter.month, end_date_filter.day)]
82
+ dfSentiment = dfSentiment.reset_index(drop=True)
83
+
84
+
85
+ # creating a single-element container
86
+ placeholder = st.empty()
87
+
88
+ # near real-time / live feed simulation
89
+ for i in range(1, len(dfSentiment)-1):
90
+
91
+ # creating KPIs
92
+ last_close = dfSentiment['close'][i]
93
+ last_close_lag1 = dfSentiment['close'][i-1]
94
+ last_sentiment = dfSentiment['sentiment_score'][i]
95
+ last_sentiment_lag1 = dfSentiment['sentiment_score'][i-1]
96
+
97
+
98
+ with placeholder.container():
99
+
100
+ # create columns
101
+ kpi1, kpi2, kpi3 = st.columns(3)
102
+
103
+ # fill in those three columns with respective metrics or KPIs
104
+ kpi1.metric(
105
+ label='Sentiment Score',
106
+ value=round(last_sentiment, 3),
107
+ delta=round(last_sentiment_lag1, 3),
108
+ )
109
+
110
+ kpi2.metric(
111
+ label='Last Closing Price',
112
+ value=round(last_close),
113
+ delta=round(last_close - last_close_lag1)
114
+ )
115
+
116
+
117
+ # create two columns for charts
118
+ fig_col1, fig_col2 = st.columns(2)
119
+
120
+ with fig_col1:
121
+ # Add traces
122
+ fig=make_subplots(specs=[[{"secondary_y":True}]])
123
+
124
+
125
+ fig.add_trace(
126
+ go.Scatter(
127
+ x=dfSentiment['timestamp'][0:i],
128
+ y=dfSentiment[sentiment_select][0:i],
129
+ name=sentiment_select,
130
+ mode='lines',
131
+ hoverinfo='none',
132
+ )
133
+ )
134
+
135
+ if sentiment_select.startswith('perc') == True:
136
+ yaxis_label = '% Change Sentiment'
137
+
138
+ elif sentiment_select in sentiment_cols:
139
+ yaxis_label = 'Sentiment Score'
140
+
141
+ elif sentiment_select in post_list:
142
+ yaxis_label = 'Volume'
143
+
144
+ fig.layout.yaxis.title=yaxis_label
145
+
146
+ if stock_select.startswith('perc') == True:
147
+ fig.add_trace(
148
+ go.Scatter(
149
+ x=dfSentiment['timestamp'][0:i],
150
+ y=dfSentiment[stock_select][0:i],
151
+ name=stock_select,
152
+ mode='lines',
153
+ hoverinfo='none',
154
+ yaxis='y2',
155
+ )
156
+ )
157
+ fig.layout.yaxis2.title='% Change Stock Price ($US)'
158
+
159
+ elif stock_select == 'volume':
160
+ fig.add_trace(
161
+ go.Scatter(
162
+ x=dfSentiment['timestamp'][0:i],
163
+ y=dfSentiment[stock_select][0:i],
164
+ name=stock_select,
165
+ mode='lines',
166
+ hoverinfo='none',
167
+ yaxis='y2',
168
+ )
169
+ )
170
+
171
+ fig.layout.yaxis2.title="Shares Traded"
172
+
173
+
174
+ else:
175
+ fig.add_trace(
176
+ go.Scatter(
177
+ x=dfSentiment['timestamp'][0:i],
178
+ y=dfSentiment[stock_select][0:i],
179
+ name=stock_select,
180
+ mode='lines',
181
+ hoverinfo='none',
182
+ yaxis='y2',
183
+ )
184
+ )
185
+
186
+ fig.layout.yaxis2.title='Stock Price ($USD)'
187
+
188
+
189
+ fig.layout.xaxis.title='Timestamp'
190
+
191
+ st.write(fig)
192
+
193
+
194
+ st.markdown('### Detailed Data View')
195
+ st.dataframe(dfSentiment.iloc[:, 1:][0:i])
196
+ time.sleep(1)