Update app.py
Browse files
app.py
CHANGED
@@ -1,156 +1,156 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import yfinance as yf
|
3 |
-
import plotly.graph_objs as go
|
4 |
-
from plotly.subplots import make_subplots
|
5 |
-
from crew import crew_creator
|
6 |
-
from dotenv import load_dotenv
|
7 |
-
load_dotenv()
|
8 |
-
|
9 |
-
st.set_page_config(layout="wide", page_title="Finance Agent", initial_sidebar_state="expanded")
|
10 |
-
st.sidebar.markdown('<p class="medium-font">Configuration</p>', unsafe_allow_html=True)
|
11 |
-
|
12 |
-
st.markdown("""
|
13 |
-
<div class="analysis-card">
|
14 |
-
<h2 class="analysis-title">AI-Agents Finance Analyst Platform</h2>
|
15 |
-
<p class="analysis-content">
|
16 |
-
Welcome to my cutting-edge stock analysis platform, leveraging Artificial Intelligence and Large Language Models (LLMs) to deliver professional-grade investment insights. Our system offers:
|
17 |
-
</p>
|
18 |
-
<ul class="analysis-list">
|
19 |
-
<li class="analysis-list-item">Comprehensive Data Analysis on stocks, and investing.</li>
|
20 |
-
<li class="analysis-list-item">In-depth fundamental and technical analyses</li>
|
21 |
-
<li class="analysis-list-item">Extensive web and news research integration</li>
|
22 |
-
<li class="analysis-list-item">Customizable analysis parameters including time frames and specific indicators</li>
|
23 |
-
</ul>
|
24 |
-
<p class="analysis-content">
|
25 |
-
Users can obtain a detailed, AI-generated analysis report by simply selecting a stock symbol, specifying a time period, and choosing desired analysis indicators. This platform aims to empower investors with data-driven, AI-enhanced decision-making tools for the complex world of stock market investments.
|
26 |
-
</p>
|
27 |
-
<p class="analysis-content">
|
28 |
-
Please note, this analysis is for informational purposes only and should not be construed as financial or investment advice.
|
29 |
-
</div>
|
30 |
-
""", unsafe_allow_html=True)
|
31 |
-
|
32 |
-
stock_symbol = st.sidebar.text_input("Enter Stock Symbol", value="
|
33 |
-
time_period = st.sidebar.selectbox("Select Time Period", ['1mo', '3mo', '6mo', '1y', '2y', '5y', 'max'])
|
34 |
-
indicators = st.sidebar.multiselect("Select Indicators", ['Moving Averages', 'Volume', 'RSI', 'MACD'])
|
35 |
-
analyze_button = st.sidebar.button("π Analyze Stock", help="Click to start the stock analysis")
|
36 |
-
|
37 |
-
# Initialize session state
|
38 |
-
if 'analyzed' not in st.session_state:
|
39 |
-
st.session_state.analyzed = False
|
40 |
-
st.session_state.stock_info = None
|
41 |
-
st.session_state.stock_data = None
|
42 |
-
st.session_state.result_file_path = None
|
43 |
-
|
44 |
-
def get_stock_data(stock_symbol, period='1y'):
|
45 |
-
return yf.download(stock_symbol, period=period)
|
46 |
-
|
47 |
-
def plot_stock_chart(stock_data, indicators):
|
48 |
-
fig = make_subplots(rows=3, cols=1, shared_xaxes=True, vertical_spacing=0.05, row_heights=[0.6, 0.2, 0.2])
|
49 |
-
|
50 |
-
# Main price chart
|
51 |
-
fig.add_trace(go.Candlestick(x=stock_data.index,
|
52 |
-
open=stock_data['Open'],
|
53 |
-
high=stock_data['High'],
|
54 |
-
low=stock_data['Low'],
|
55 |
-
close=stock_data['Close'],
|
56 |
-
name='Price'),
|
57 |
-
row=1, col=1)
|
58 |
-
|
59 |
-
# Add selected indicators
|
60 |
-
if 'Moving Averages' in indicators:
|
61 |
-
fig.add_trace(go.Scatter(x=stock_data.index, y=stock_data['Close'].rolling(window=50).mean(), name='50 MA', line=dict(color='orange')), row=1, col=1)
|
62 |
-
fig.add_trace(go.Scatter(x=stock_data.index, y=stock_data['Close'].rolling(window=200).mean(), name='200 MA', line=dict(color='red')), row=1, col=1)
|
63 |
-
|
64 |
-
if 'Volume' in indicators:
|
65 |
-
fig.add_trace(go.Bar(x=stock_data.index, y=stock_data['Volume'], name='Volume'), row=2, col=1)
|
66 |
-
|
67 |
-
if 'RSI' in indicators:
|
68 |
-
delta = stock_data['Close'].diff()
|
69 |
-
gain = (delta.where(delta > 0, 0)).rolling(window=14).mean()
|
70 |
-
loss = (-delta.where(delta < 0, 0)).rolling(window=14).mean()
|
71 |
-
rs = gain / loss
|
72 |
-
rsi = 100 - (100 / (1 + rs))
|
73 |
-
fig.add_trace(go.Scatter(x=stock_data.index, y=rsi, name='RSI'), row=3, col=1)
|
74 |
-
|
75 |
-
if 'MACD' in indicators:
|
76 |
-
ema12 = stock_data['Close'].ewm(span=12, adjust=False).mean()
|
77 |
-
ema26 = stock_data['Close'].ewm(span=26, adjust=False).mean()
|
78 |
-
macd = ema12 - ema26
|
79 |
-
signal = macd.ewm(span=9, adjust=False).mean()
|
80 |
-
fig.add_trace(go.Scatter(x=stock_data.index, y=macd, name='MACD'), row=3, col=1)
|
81 |
-
fig.add_trace(go.Scatter(x=stock_data.index, y=signal, name='Signal'), row=3, col=1)
|
82 |
-
|
83 |
-
fig.update_layout(
|
84 |
-
title='Stock Analysis',
|
85 |
-
yaxis_title='Price',
|
86 |
-
xaxis_rangeslider_visible=False,
|
87 |
-
height=800,
|
88 |
-
showlegend=True
|
89 |
-
)
|
90 |
-
|
91 |
-
fig.update_xaxes(
|
92 |
-
rangeselector=dict(
|
93 |
-
buttons=list([
|
94 |
-
dict(count=1, label="1m", step="month", stepmode="backward"),
|
95 |
-
dict(count=6, label="6m", step="month", stepmode="backward"),
|
96 |
-
dict(count=1, label="YTD", step="year", stepmode="todate"),
|
97 |
-
dict(count=1, label="1y", step="year", stepmode="backward"),
|
98 |
-
dict(step="all")
|
99 |
-
])
|
100 |
-
),
|
101 |
-
rangeslider=dict(visible=False),
|
102 |
-
type="date"
|
103 |
-
)
|
104 |
-
|
105 |
-
return fig
|
106 |
-
|
107 |
-
if analyze_button:
|
108 |
-
st.session_state.analyzed = False # Reset analyzed state
|
109 |
-
st.snow()
|
110 |
-
|
111 |
-
# Fetch stock info and data
|
112 |
-
with st.spinner(f"Fetching data for {stock_symbol}..."):
|
113 |
-
stock = yf.Ticker(stock_symbol)
|
114 |
-
st.session_state.stock_info = stock.info
|
115 |
-
st.session_state.stock_data = get_stock_data(stock_symbol, period=time_period)
|
116 |
-
|
117 |
-
# Create and run the crew
|
118 |
-
with st.spinner("Running analysis, please wait..."):
|
119 |
-
|
120 |
-
st.session_state.result_file_path = crew_creator(stock_symbol)
|
121 |
-
|
122 |
-
st.session_state.analyzed = True
|
123 |
-
|
124 |
-
# Display stock info if available
|
125 |
-
if st.session_state.stock_info:
|
126 |
-
st.markdown('<p class="medium-font">Stock Information</p>', unsafe_allow_html=True)
|
127 |
-
info = st.session_state.stock_info
|
128 |
-
col1, col2, col3 = st.columns(3)
|
129 |
-
with col1:
|
130 |
-
st.markdown(f"**Company Name:** {info.get('longName', 'N/A')}")
|
131 |
-
st.markdown(f"**Sector:** {info.get('sector', 'N/A')}")
|
132 |
-
with col2:
|
133 |
-
st.markdown(f"**Industry:** {info.get('industry', 'N/A')}")
|
134 |
-
st.markdown(f"**Country:** {info.get('country', 'N/A')}")
|
135 |
-
with col3:
|
136 |
-
st.markdown(f"**Current Price:** ${info.get('currentPrice', 'N/A')}")
|
137 |
-
st.markdown(f"**Market Cap:** ${info.get('marketCap', 'N/A')}")
|
138 |
-
|
139 |
-
# Display CrewAI result if available
|
140 |
-
if st.session_state.result_file_path:
|
141 |
-
st.markdown('<p class="medium-font">Analysis Result</p>', unsafe_allow_html=True)
|
142 |
-
|
143 |
-
# with open(st.session_state.result_file_path, 'r') as file:
|
144 |
-
# result = file.read()
|
145 |
-
st.markdown("---")
|
146 |
-
|
147 |
-
st.markdown(st.session_state.result_file_path)
|
148 |
-
|
149 |
-
# Display chart
|
150 |
-
if st.session_state.analyzed and st.session_state.stock_data is not None:
|
151 |
-
st.markdown('<p class="medium-font">Interactive Stock Chart</p>', unsafe_allow_html=True)
|
152 |
-
st.plotly_chart(plot_stock_chart(st.session_state.stock_data, indicators), use_container_width=True)
|
153 |
-
|
154 |
-
|
155 |
-
st.markdown("---")
|
156 |
st.markdown('<p class="small-font">Crafted by base234 </p>', unsafe_allow_html=True)
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import yfinance as yf
|
3 |
+
import plotly.graph_objs as go
|
4 |
+
from plotly.subplots import make_subplots
|
5 |
+
from crew import crew_creator
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
st.set_page_config(layout="wide", page_title="Finance Agent", initial_sidebar_state="expanded")
|
10 |
+
st.sidebar.markdown('<p class="medium-font">Configuration</p>', unsafe_allow_html=True)
|
11 |
+
|
12 |
+
st.markdown("""
|
13 |
+
<div class="analysis-card">
|
14 |
+
<h2 class="analysis-title">AI-Agents Finance Analyst Platform</h2>
|
15 |
+
<p class="analysis-content">
|
16 |
+
Welcome to my cutting-edge stock analysis platform, leveraging Artificial Intelligence and Large Language Models (LLMs) to deliver professional-grade investment insights. Our system offers:
|
17 |
+
</p>
|
18 |
+
<ul class="analysis-list">
|
19 |
+
<li class="analysis-list-item">Comprehensive Data Analysis on stocks, and investing.</li>
|
20 |
+
<li class="analysis-list-item">In-depth fundamental and technical analyses</li>
|
21 |
+
<li class="analysis-list-item">Extensive web and news research integration</li>
|
22 |
+
<li class="analysis-list-item">Customizable analysis parameters including time frames and specific indicators</li>
|
23 |
+
</ul>
|
24 |
+
<p class="analysis-content">
|
25 |
+
Users can obtain a detailed, AI-generated analysis report by simply selecting a stock symbol, specifying a time period, and choosing desired analysis indicators. This platform aims to empower investors with data-driven, AI-enhanced decision-making tools for the complex world of stock market investments.
|
26 |
+
</p>
|
27 |
+
<p class="analysis-content">
|
28 |
+
Please note, this analysis is for informational purposes only and should not be construed as financial or investment advice.
|
29 |
+
</div>
|
30 |
+
""", unsafe_allow_html=True)
|
31 |
+
|
32 |
+
stock_symbol = st.sidebar.text_input("Enter Stock Symbol", value="META", placeholder="META, AAPL, NVDA")
|
33 |
+
time_period = st.sidebar.selectbox("Select Time Period", ['1mo', '3mo', '6mo', '1y', '2y', '5y', 'max'])
|
34 |
+
indicators = st.sidebar.multiselect("Select Indicators", ['Moving Averages', 'Volume', 'RSI', 'MACD'])
|
35 |
+
analyze_button = st.sidebar.button("π Analyze Stock", help="Click to start the stock analysis")
|
36 |
+
|
37 |
+
# Initialize session state
|
38 |
+
if 'analyzed' not in st.session_state:
|
39 |
+
st.session_state.analyzed = False
|
40 |
+
st.session_state.stock_info = None
|
41 |
+
st.session_state.stock_data = None
|
42 |
+
st.session_state.result_file_path = None
|
43 |
+
|
44 |
+
def get_stock_data(stock_symbol, period='1y'):
|
45 |
+
return yf.download(stock_symbol, period=period)
|
46 |
+
|
47 |
+
def plot_stock_chart(stock_data, indicators):
|
48 |
+
fig = make_subplots(rows=3, cols=1, shared_xaxes=True, vertical_spacing=0.05, row_heights=[0.6, 0.2, 0.2])
|
49 |
+
|
50 |
+
# Main price chart
|
51 |
+
fig.add_trace(go.Candlestick(x=stock_data.index,
|
52 |
+
open=stock_data['Open'],
|
53 |
+
high=stock_data['High'],
|
54 |
+
low=stock_data['Low'],
|
55 |
+
close=stock_data['Close'],
|
56 |
+
name='Price'),
|
57 |
+
row=1, col=1)
|
58 |
+
|
59 |
+
# Add selected indicators
|
60 |
+
if 'Moving Averages' in indicators:
|
61 |
+
fig.add_trace(go.Scatter(x=stock_data.index, y=stock_data['Close'].rolling(window=50).mean(), name='50 MA', line=dict(color='orange')), row=1, col=1)
|
62 |
+
fig.add_trace(go.Scatter(x=stock_data.index, y=stock_data['Close'].rolling(window=200).mean(), name='200 MA', line=dict(color='red')), row=1, col=1)
|
63 |
+
|
64 |
+
if 'Volume' in indicators:
|
65 |
+
fig.add_trace(go.Bar(x=stock_data.index, y=stock_data['Volume'], name='Volume'), row=2, col=1)
|
66 |
+
|
67 |
+
if 'RSI' in indicators:
|
68 |
+
delta = stock_data['Close'].diff()
|
69 |
+
gain = (delta.where(delta > 0, 0)).rolling(window=14).mean()
|
70 |
+
loss = (-delta.where(delta < 0, 0)).rolling(window=14).mean()
|
71 |
+
rs = gain / loss
|
72 |
+
rsi = 100 - (100 / (1 + rs))
|
73 |
+
fig.add_trace(go.Scatter(x=stock_data.index, y=rsi, name='RSI'), row=3, col=1)
|
74 |
+
|
75 |
+
if 'MACD' in indicators:
|
76 |
+
ema12 = stock_data['Close'].ewm(span=12, adjust=False).mean()
|
77 |
+
ema26 = stock_data['Close'].ewm(span=26, adjust=False).mean()
|
78 |
+
macd = ema12 - ema26
|
79 |
+
signal = macd.ewm(span=9, adjust=False).mean()
|
80 |
+
fig.add_trace(go.Scatter(x=stock_data.index, y=macd, name='MACD'), row=3, col=1)
|
81 |
+
fig.add_trace(go.Scatter(x=stock_data.index, y=signal, name='Signal'), row=3, col=1)
|
82 |
+
|
83 |
+
fig.update_layout(
|
84 |
+
title='Stock Analysis',
|
85 |
+
yaxis_title='Price',
|
86 |
+
xaxis_rangeslider_visible=False,
|
87 |
+
height=800,
|
88 |
+
showlegend=True
|
89 |
+
)
|
90 |
+
|
91 |
+
fig.update_xaxes(
|
92 |
+
rangeselector=dict(
|
93 |
+
buttons=list([
|
94 |
+
dict(count=1, label="1m", step="month", stepmode="backward"),
|
95 |
+
dict(count=6, label="6m", step="month", stepmode="backward"),
|
96 |
+
dict(count=1, label="YTD", step="year", stepmode="todate"),
|
97 |
+
dict(count=1, label="1y", step="year", stepmode="backward"),
|
98 |
+
dict(step="all")
|
99 |
+
])
|
100 |
+
),
|
101 |
+
rangeslider=dict(visible=False),
|
102 |
+
type="date"
|
103 |
+
)
|
104 |
+
|
105 |
+
return fig
|
106 |
+
|
107 |
+
if analyze_button:
|
108 |
+
st.session_state.analyzed = False # Reset analyzed state
|
109 |
+
st.snow()
|
110 |
+
|
111 |
+
# Fetch stock info and data
|
112 |
+
with st.spinner(f"Fetching data for {stock_symbol}..."):
|
113 |
+
stock = yf.Ticker(stock_symbol)
|
114 |
+
st.session_state.stock_info = stock.info
|
115 |
+
st.session_state.stock_data = get_stock_data(stock_symbol, period=time_period)
|
116 |
+
|
117 |
+
# Create and run the crew
|
118 |
+
with st.spinner("Running analysis, please wait..."):
|
119 |
+
|
120 |
+
st.session_state.result_file_path = crew_creator(stock_symbol)
|
121 |
+
|
122 |
+
st.session_state.analyzed = True
|
123 |
+
|
124 |
+
# Display stock info if available
|
125 |
+
if st.session_state.stock_info:
|
126 |
+
st.markdown('<p class="medium-font">Stock Information</p>', unsafe_allow_html=True)
|
127 |
+
info = st.session_state.stock_info
|
128 |
+
col1, col2, col3 = st.columns(3)
|
129 |
+
with col1:
|
130 |
+
st.markdown(f"**Company Name:** {info.get('longName', 'N/A')}")
|
131 |
+
st.markdown(f"**Sector:** {info.get('sector', 'N/A')}")
|
132 |
+
with col2:
|
133 |
+
st.markdown(f"**Industry:** {info.get('industry', 'N/A')}")
|
134 |
+
st.markdown(f"**Country:** {info.get('country', 'N/A')}")
|
135 |
+
with col3:
|
136 |
+
st.markdown(f"**Current Price:** ${info.get('currentPrice', 'N/A')}")
|
137 |
+
st.markdown(f"**Market Cap:** ${info.get('marketCap', 'N/A')}")
|
138 |
+
|
139 |
+
# Display CrewAI result if available
|
140 |
+
if st.session_state.result_file_path:
|
141 |
+
st.markdown('<p class="medium-font">Analysis Result</p>', unsafe_allow_html=True)
|
142 |
+
|
143 |
+
# with open(st.session_state.result_file_path, 'r') as file:
|
144 |
+
# result = file.read()
|
145 |
+
st.markdown("---")
|
146 |
+
|
147 |
+
st.markdown(st.session_state.result_file_path)
|
148 |
+
|
149 |
+
# Display chart
|
150 |
+
if st.session_state.analyzed and st.session_state.stock_data is not None:
|
151 |
+
st.markdown('<p class="medium-font">Interactive Stock Chart</p>', unsafe_allow_html=True)
|
152 |
+
st.plotly_chart(plot_stock_chart(st.session_state.stock_data, indicators), use_container_width=True)
|
153 |
+
|
154 |
+
|
155 |
+
st.markdown("---")
|
156 |
st.markdown('<p class="small-font">Crafted by base234 </p>', unsafe_allow_html=True)
|