Spaces:
Runtime error
Runtime error
File size: 7,109 Bytes
0949c3d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
from datetime import date
from datetime import datetime
import re
import numpy as np
import pandas as pd
from PIL import Image
import plotly.express as px
import plotly.graph_objects as go
import streamlit as st
import time
from plotly.subplots import make_subplots
# Read CSV file into pandas and extract timestamp data
dfSentiment = pd.read_csv('sentiment_data.csv') ### YOUR LINE OF CODE HERE
dfSentiment['timestamp'] = [datetime.strptime(dt, '%Y-%m-%d') for dt in dfSentiment['timestamp'].tolist()]
# Multi-select columns to build chart
col_list = dfSentiment.columns.tolist() ### YOUR LINE OF CODE HERE #### Extract columns into a list
r_sentiment = re.compile(".*sentiment")
sentiment_cols = list(filter(r_sentiment.match, col_list)) ### YOUR LINE OF CODE HERE
r_post = re.compile(".*post")
post_list = list(filter(r_post.match, col_list)) ### YOUR LINE OF CODE HERE
r_perc= re.compile(".*perc")
perc_list = list(filter(r_perc.match, col_list))
r_close = re.compile(".*close")
close_list = list(filter(r_close.match, col_list))
r_volume = re.compile(".*volume")
volume_list = list(filter(r_volume.match, col_list))
sentiment_cols = sentiment_cols + post_list
stocks_cols = perc_list + close_list + volume_list ### YOUR LINE OF CODE HERE
# Config for page
st.set_page_config(
page_title= 'Sentiment Stock Experiment', ### YOUR LINE OF CODE HERE
page_icon='✅',
layout='wide',
)
with st.sidebar:
# FourthBrain logo to sidebar
fourthbrain_logo = Image.open('./images/fourthbrain_logo.png')
st.image([fourthbrain_logo], width=300)
# Date selection filters
start_date_filter = st.date_input(
### YOUR LINE OF CODE HERE
'Start Date',
min(dfSentiment['timestamp']),
min_value=min(dfSentiment['timestamp']),
max_value=max(dfSentiment['timestamp'])
)
end_date_filter = st.date_input(
'End Date',
max(dfSentiment['timestamp']),
min_value=min(dfSentiment['timestamp']),
max_value=max(dfSentiment['timestamp'])
)
sentiment_select = st.selectbox('Select Sentiment/Reddit Data', sentiment_cols) ### YOUR LINE OF CODE HERE
stock_select =st.selectbox('Select Stock Data', stocks_cols) ### YOUR LINE OF CODE HERE
# Banner with TSLA and Reddit images
tsla_logo = Image.open('./images/tsla_logo.png') ### YOUR LINE OF CODE HERE
reddit_logo = Image.open('./images/reddit_logo.png')
st.image([tsla_logo, reddit_logo], width=200)
# dashboard title
### YOUR LINE OF CODE HERE
st.title('Sentiment Stock Experiment')
## dataframe filter
# start date
dfSentiment = dfSentiment[dfSentiment['timestamp'] >= datetime(start_date_filter.year, start_date_filter.month, start_date_filter.day)]
# end date
dfSentiment = dfSentiment[dfSentiment['timestamp'] <= datetime(end_date_filter.year, end_date_filter.month, end_date_filter.day)] ### YOUR LINE OF CODE HERE
dfSentiment = dfSentiment.reset_index(drop=True)
# creating a single-element container
placeholder = st.empty() ### YOUR LINE OF CODE HERE
# near real-time / live feed simulation
for i in range(1, len(dfSentiment)-1):
# creating KPIs
last_close = dfSentiment['close'][i]
last_close_lag1 = dfSentiment['close'][i-1]
last_sentiment = dfSentiment[sentiment_select][i] ### YOUR LINE OF CODE HERE
last_sentiment_lag1 = dfSentiment[sentiment_select][i-1] ### YOUR LINE OF CODE HERE
with placeholder.container():
# create columns
kpi1, kpi2, kpi3 = st.columns(3)
# fill in those three columns with respective metrics or KPIs
kpi1.metric(
label='Sentiment Score',
value=round(last_sentiment, 3),
delta=round(last_sentiment_lag1, 3),
)
kpi2.metric(
label='Last Closing Price',
### YOUR LINE 1 OF CODE HERE
value=round(last_close, 3),
### YOUR LINE 2 OF CODE HERE
delta=round(last_close_lag1, 3),
)
# create two columns for charts
fig_col1, fig_col2 = st.columns(2)
with fig_col1:
# Add traces
fig=make_subplots(specs=[[{"secondary_y":True}]])
fig.add_trace(
go.Scatter(
x=dfSentiment['timestamp'][0:i],
y=dfSentiment[sentiment_select][0:i],
name=sentiment_select,
mode='lines',
hoverinfo='none',
)
)
if sentiment_select.startswith('perc') == True:
### YOUR LINE OF CODE HERE
yaxis_label = '% Change Sentiment'
elif sentiment_select in sentiment_cols:
### YOUR LINE OF CODE HERE
yaxis_label = 'Sentiment Score'
elif sentiment_select in post_list:
yaxis_label = 'Volume'
fig.layout.yaxis.title=yaxis_label
if stock_select.startswith('perc') == True:
fig.add_trace(
go.Scatter(
x=dfSentiment['timestamp'][0:i],
y=dfSentiment[stock_select][0:i],
name=stock_select,
mode='lines',
hoverinfo='none',
yaxis='y2',
)
)
fig.layout.yaxis2.title='% Change Stock Price ($US)'
elif stock_select == 'volume':
fig.add_trace(
go.Scatter(
x=dfSentiment['timestamp'][0:i],
y=dfSentiment[stock_select][0:i],
name=stock_select,
mode='lines',
hoverinfo='none',
yaxis='y2',
)
)
fig.layout.yaxis2.title="Shares Traded"
else:
fig.add_trace(
go.Scatter(
x=dfSentiment['timestamp'][0:i],
y=dfSentiment[stock_select][0:i],
name=stock_select,
mode='lines',
hoverinfo='none',
yaxis='y2',
)
)
fig.layout.yaxis2.title='Stock Price ($USD)'
fig.layout.xaxis.title='Timestamp'
# write the figure throught streamlit
### YOUR LINE OF CODE HERE
st.plotly_chart(fig)
st.markdown('### Detailed Data View')
st.dataframe(dfSentiment.iloc[:, 1:][0:i])
time.sleep(1)
|