Update src/streamlit_app.py
Browse files- src/streamlit_app.py +116 -33
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,123 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
"""
|
| 7 |
-
# Welcome to Streamlit!
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
+
from stockbot import StockAnalyzer
|
| 5 |
+
import time
|
| 6 |
|
| 7 |
+
st.set_page_config(page_title="Stock Analysis Dashboard", layout="wide")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Initialize session state
|
| 10 |
+
if 'analyzer' not in st.session_state:
|
| 11 |
+
st.session_state.analyzer = StockAnalyzer()
|
| 12 |
+
st.session_state.last_update = None
|
| 13 |
|
| 14 |
+
def create_candlestick_chart(symbol, price_data):
|
| 15 |
+
"""Create a candlestick chart using plotly"""
|
| 16 |
+
fig = go.Figure(data=[go.Candlestick(x=price_data.index,
|
| 17 |
+
open=price_data['Open'],
|
| 18 |
+
high=price_data['High'],
|
| 19 |
+
low=price_data['Low'],
|
| 20 |
+
close=price_data['Close'])])
|
| 21 |
+
fig.update_layout(title=f'{symbol} Price History',
|
| 22 |
+
xaxis_title='Date',
|
| 23 |
+
yaxis_title='Price ($)',
|
| 24 |
+
template='plotly_dark')
|
| 25 |
+
return fig
|
| 26 |
|
| 27 |
+
def main():
|
| 28 |
+
st.title("Stock Analysis Dashboard")
|
| 29 |
+
|
| 30 |
+
# Add refresh button
|
| 31 |
+
col1, col2 = st.columns([1, 5])
|
| 32 |
+
with col1:
|
| 33 |
+
if st.button("🔄 Refresh Data"):
|
| 34 |
+
st.session_state.analyzer.update_data()
|
| 35 |
+
st.session_state.analyzer.analyze_stocks()
|
| 36 |
+
st.session_state.last_update = time.strftime("%Y-%m-%d %H:%M:%S")
|
| 37 |
+
st.success("Data refreshed!")
|
| 38 |
+
|
| 39 |
+
with col2:
|
| 40 |
+
if st.session_state.last_update:
|
| 41 |
+
st.text(f"Last Update: {st.session_state.last_update}")
|
| 42 |
|
| 43 |
+
# Display stock analysis results
|
| 44 |
+
results = st.session_state.analyzer.analysis_results
|
| 45 |
+
if results:
|
| 46 |
+
# Convert results to DataFrame for easier display
|
| 47 |
+
data = []
|
| 48 |
+
for symbol, info in results.items():
|
| 49 |
+
data.append({
|
| 50 |
+
'Symbol': symbol,
|
| 51 |
+
'Price': f"${info['current_price']}",
|
| 52 |
+
'7d Change': f"{info['change_7d']}%",
|
| 53 |
+
'30d Change': f"{info['change_30d']}%",
|
| 54 |
+
'90d Change': f"{info['change_90d']}%",
|
| 55 |
+
'YTD Change': f"{info['change_365d']}%",
|
| 56 |
+
'RSI': round(info['rsi'], 2),
|
| 57 |
+
'Trend': info['trend'],
|
| 58 |
+
'Support': f"${info['support']}",
|
| 59 |
+
'Resistance': f"${info['resistance']}",
|
| 60 |
+
'Recommendation': info['recommendation'],
|
| 61 |
+
'LLM Sentiment': info['llm_analysis']['sentiment'],
|
| 62 |
+
'LLM Confidence': f"{info['llm_analysis']['confidence']:.1%}",
|
| 63 |
+
'7d Prediction': f"{info['predictions']['prediction_7d']}%",
|
| 64 |
+
'21d Prediction': f"{info['predictions']['prediction_21d']}%"
|
| 65 |
+
})
|
| 66 |
+
|
| 67 |
+
df = pd.DataFrame(data)
|
| 68 |
+
|
| 69 |
+
# Style the dataframe
|
| 70 |
+
def color_negative_red(val):
|
| 71 |
+
try:
|
| 72 |
+
value = float(val.replace('%', '').replace('$', ''))
|
| 73 |
+
color = 'red' if value < 0 else 'green' if value > 0 else 'white'
|
| 74 |
+
return f'color: {color}'
|
| 75 |
+
except:
|
| 76 |
+
return ''
|
| 77 |
+
|
| 78 |
+
styled_df = df.style.applymap(color_negative_red, subset=['7d Change', '30d Change', '90d Change', 'YTD Change'])
|
| 79 |
+
|
| 80 |
+
# Display the main table
|
| 81 |
+
st.dataframe(styled_df, height=400)
|
| 82 |
+
|
| 83 |
+
# Add detailed view for selected stock
|
| 84 |
+
st.subheader("Detailed Stock Analysis")
|
| 85 |
+
selected_symbol = st.selectbox("Select a stock for detailed analysis", list(results.keys()))
|
| 86 |
+
|
| 87 |
+
if selected_symbol:
|
| 88 |
+
col1, col2 = st.columns(2)
|
| 89 |
+
|
| 90 |
+
with col1:
|
| 91 |
+
# Display candlestick chart
|
| 92 |
+
if selected_symbol in st.session_state.analyzer.price_data:
|
| 93 |
+
price_data = st.session_state.analyzer.price_data[selected_symbol]
|
| 94 |
+
fig = create_candlestick_chart(selected_symbol, price_data)
|
| 95 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 96 |
+
|
| 97 |
+
with col2:
|
| 98 |
+
# Display detailed analysis
|
| 99 |
+
stock_info = results[selected_symbol]
|
| 100 |
+
st.write("### Technical Analysis")
|
| 101 |
+
st.write(f"**Current Price:** ${stock_info['current_price']}")
|
| 102 |
+
st.write(f"**RSI:** {stock_info['rsi']:.2f}")
|
| 103 |
+
st.write(f"**Trend:** {stock_info['trend']}")
|
| 104 |
+
st.write(f"**Support Levels:** ${stock_info['support']}")
|
| 105 |
+
st.write(f"**Resistance Levels:** ${stock_info['resistance']}")
|
| 106 |
+
|
| 107 |
+
st.write("### LLM Analysis")
|
| 108 |
+
st.write(f"**Sentiment:** {stock_info['llm_analysis']['sentiment']}")
|
| 109 |
+
st.write(f"**Confidence:** {stock_info['llm_analysis']['confidence']:.1%}")
|
| 110 |
+
|
| 111 |
+
st.write("### Predictions")
|
| 112 |
+
st.write(f"**7-Day Forecast:** {stock_info['predictions']['prediction_7d']}%")
|
| 113 |
+
st.write(f"**21-Day Forecast:** {stock_info['predictions']['prediction_21d']}%")
|
| 114 |
+
st.write(f"**Confidence:** {stock_info['predictions']['confidence']}%")
|
| 115 |
+
|
| 116 |
+
# Display support/resistance analysis from LLM
|
| 117 |
+
if 'support_resistance' in stock_info['llm_analysis']:
|
| 118 |
+
st.write("### Support & Resistance Analysis")
|
| 119 |
+
sr_analysis = stock_info['llm_analysis']['support_resistance']
|
| 120 |
+
st.write(sr_analysis['analysis'])
|
| 121 |
|
| 122 |
+
if __name__ == "__main__":
|
| 123 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|