Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- .streamlit/config.toml +6 -0
- app.py +270 -0
- requirements.txt +7 -0
.streamlit/config.toml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[theme]
|
2 |
+
primaryColor="#2980b9"
|
3 |
+
backgroundColor="#f4f4f9"
|
4 |
+
secondaryBackgroundColor="#ffffff"
|
5 |
+
textColor="#333333"
|
6 |
+
font = "sans serif"
|
app.py
ADDED
@@ -0,0 +1,270 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import yfinance as yf
|
3 |
+
import pandas as pd
|
4 |
+
from prophet import Prophet
|
5 |
+
import plotly.graph_objs as go
|
6 |
+
import google.generativeai as genai
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
# Streamlit app details
|
10 |
+
st.set_page_config(page_title="TechyTrade", layout="wide")
|
11 |
+
|
12 |
+
# Custom CSS
|
13 |
+
st.markdown("""
|
14 |
+
<style>
|
15 |
+
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;700&display=swap');
|
16 |
+
|
17 |
+
body {
|
18 |
+
background-color: #f4f4f9;
|
19 |
+
color: #333;
|
20 |
+
font-family: 'Montserrat', sans-serif;
|
21 |
+
}
|
22 |
+
.sidebar .sidebar-content {
|
23 |
+
background-color: #2c3e50;
|
24 |
+
color: white;
|
25 |
+
}
|
26 |
+
h1, h2, h3 {
|
27 |
+
color: #2980b9;
|
28 |
+
}
|
29 |
+
.css-1v3fvcr {
|
30 |
+
color: #2980b9 !important;
|
31 |
+
}
|
32 |
+
.css-17eq0hr {
|
33 |
+
font-family: 'Montserrat', sans-serif !important;
|
34 |
+
}
|
35 |
+
.css-2trqyj {
|
36 |
+
font-family: 'Montserrat', sans-serif !important;
|
37 |
+
}
|
38 |
+
</style>
|
39 |
+
""", unsafe_allow_html=True)
|
40 |
+
|
41 |
+
# Sidebar
|
42 |
+
with st.sidebar:
|
43 |
+
st.title("📊 TechyTrade")
|
44 |
+
ticker = st.text_input("Enter a stock ticker (e.g. AAPL) 🏷️", "AAPL")
|
45 |
+
period = st.selectbox("Enter a time frame ⏳", ("1D", "5D", "1M", "6M", "YTD", "1Y", "5Y"), index=2)
|
46 |
+
forecast_period = st.slider("Select forecast period (days) 🔮", min_value=1, max_value=365, value=30)
|
47 |
+
st.write("Select Technical Indicators:")
|
48 |
+
sma_checkbox = st.checkbox("Simple Moving Average (SMA)")
|
49 |
+
ema_checkbox = st.checkbox("Exponential Moving Average (EMA)")
|
50 |
+
rsi_checkbox = st.checkbox("Relative Strength Index (RSI)")
|
51 |
+
macd_checkbox = st.checkbox("Moving Average Convergence Divergence (MACD)")
|
52 |
+
bollinger_checkbox = st.checkbox("Bollinger Bands")
|
53 |
+
google_api_key = st.text_input("Enter your Google API Key 🔑", type="password")
|
54 |
+
button = st.button("Submit 🚀")
|
55 |
+
|
56 |
+
# Load generative model
|
57 |
+
@st.cache_resource
|
58 |
+
def load_model(api_key):
|
59 |
+
genai.configure(api_key=api_key)
|
60 |
+
return genai.GenerativeModel('gemini-1.5-flash')
|
61 |
+
|
62 |
+
# Function to generate reasons using the generative model
|
63 |
+
def generate_reasons(fig, stock_info, price_info, biz_metrics, api_key):
|
64 |
+
model = load_model(api_key)
|
65 |
+
prompt = f"Based on the following stock price graph description:\n\n{fig}\n\n and the tables:\n\n{stock_info}\n\n and\n\n{price_info}\n\n and\n\n{biz_metrics}\n\n and analyze the trends and give recommendations and insights."
|
66 |
+
response = model.generate_content(prompt)
|
67 |
+
return response.text
|
68 |
+
|
69 |
+
# Function to format large numbers
|
70 |
+
def format_value(value):
|
71 |
+
suffixes = ["", "K", "M", "B", "T"]
|
72 |
+
suffix_index = 0
|
73 |
+
while value >= 1000 and suffix_index < len(suffixes) - 1:
|
74 |
+
value /= 1000
|
75 |
+
suffix_index += 1
|
76 |
+
return f"${value:.1f}{suffixes[suffix_index]}"
|
77 |
+
|
78 |
+
# Technical Indicators Functions
|
79 |
+
def calculate_sma(data, window):
|
80 |
+
return data.rolling(window=window).mean()
|
81 |
+
|
82 |
+
def calculate_ema(data, window):
|
83 |
+
return data.ewm(span=window, adjust=False).mean()
|
84 |
+
|
85 |
+
def calculate_rsi(data, window):
|
86 |
+
delta = data.diff()
|
87 |
+
gain = (delta.where(delta > 0, 0)).rolling(window=window).mean()
|
88 |
+
loss = (-delta.where(delta < 0, 0)).rolling(window=window).mean()
|
89 |
+
rs = gain / loss
|
90 |
+
return 100 - (100 / (1 + rs))
|
91 |
+
|
92 |
+
def calculate_macd(data, short_window=12, long_window=26, signal_window=9):
|
93 |
+
short_ema = calculate_ema(data, short_window)
|
94 |
+
long_ema = calculate_ema(data, long_window)
|
95 |
+
macd = short_ema - long_ema
|
96 |
+
signal = calculate_ema(macd, signal_window)
|
97 |
+
return macd, signal
|
98 |
+
|
99 |
+
def calculate_bollinger_bands(data, window):
|
100 |
+
sma = calculate_sma(data, window)
|
101 |
+
std = data.rolling(window=window).std()
|
102 |
+
upper_band = sma + (std * 2)
|
103 |
+
lower_band = sma - (std * 2)
|
104 |
+
return upper_band, lower_band
|
105 |
+
|
106 |
+
# If Submit button is clicked
|
107 |
+
if button:
|
108 |
+
if not ticker.strip():
|
109 |
+
st.error("Please provide a valid stock ticker.")
|
110 |
+
elif not google_api_key.strip():
|
111 |
+
st.error("Please provide a valid Google API Key.")
|
112 |
+
else:
|
113 |
+
try:
|
114 |
+
with st.spinner('Please wait...'):
|
115 |
+
# Retrieve stock data
|
116 |
+
stock = yf.Ticker(ticker)
|
117 |
+
info = stock.info
|
118 |
+
|
119 |
+
st.subheader(f"{ticker} - {info.get('longName', 'N/A')}")
|
120 |
+
|
121 |
+
# Plot historical stock price data
|
122 |
+
if period == "1D":
|
123 |
+
history = stock.history(period="1d", interval="1h")
|
124 |
+
elif period == "5D":
|
125 |
+
history = stock.history(period="5d", interval="1d")
|
126 |
+
elif period == "1M":
|
127 |
+
history = stock.history(period="1mo", interval="1d")
|
128 |
+
elif period == "6M":
|
129 |
+
history = stock.history(period="6mo", interval="1wk")
|
130 |
+
elif period == "YTD":
|
131 |
+
history = stock.history(period="ytd", interval="1mo")
|
132 |
+
elif period == "1Y":
|
133 |
+
history = stock.history(period="1y", interval="1mo")
|
134 |
+
elif period == "5Y":
|
135 |
+
history = stock.history(period="5y", interval="3mo")
|
136 |
+
|
137 |
+
# Create a plotly figure
|
138 |
+
fig = go.Figure()
|
139 |
+
fig.add_trace(go.Scatter(x=history.index, y=history['Close'], mode='lines', name='Close Price'))
|
140 |
+
|
141 |
+
# Add Technical Indicators
|
142 |
+
if sma_checkbox:
|
143 |
+
sma = calculate_sma(history['Close'], window=20)
|
144 |
+
fig.add_trace(go.Scatter(x=history.index, y=sma, mode='lines', name='SMA (20)'))
|
145 |
+
|
146 |
+
if ema_checkbox:
|
147 |
+
ema = calculate_ema(history['Close'], window=20)
|
148 |
+
fig.add_trace(go.Scatter(x=history.index, y=ema, mode='lines', name='EMA (20)'))
|
149 |
+
|
150 |
+
if rsi_checkbox:
|
151 |
+
rsi = calculate_rsi(history['Close'], window=14)
|
152 |
+
fig.add_trace(go.Scatter(x=history.index, y=rsi, mode='lines', name='RSI (14)', yaxis='y2'))
|
153 |
+
fig.update_layout(yaxis2=dict(title='RSI', overlaying='y', side='right'))
|
154 |
+
|
155 |
+
if macd_checkbox:
|
156 |
+
macd, signal = calculate_macd(history['Close'])
|
157 |
+
fig.add_trace(go.Scatter(x=history.index, y=macd, mode='lines', name='MACD'))
|
158 |
+
fig.add_trace(go.Scatter(x=history.index, y=signal, mode='lines', name='Signal Line'))
|
159 |
+
|
160 |
+
if bollinger_checkbox:
|
161 |
+
upper_band, lower_band = calculate_bollinger_bands(history['Close'], window=20)
|
162 |
+
fig.add_trace(go.Scatter(x=history.index, y=upper_band, mode='lines', name='Upper Band'))
|
163 |
+
fig.add_trace(go.Scatter(x=history.index, y=lower_band, mode='lines', name='Lower Band'))
|
164 |
+
|
165 |
+
fig.update_layout(
|
166 |
+
title=f"Historical Stock Prices for {ticker}",
|
167 |
+
xaxis_title="Date",
|
168 |
+
yaxis_title="Close Price",
|
169 |
+
hovermode="x unified"
|
170 |
+
)
|
171 |
+
st.plotly_chart(fig, use_container_width=True)
|
172 |
+
|
173 |
+
col1, col2, col3 = st.columns(3)
|
174 |
+
|
175 |
+
# Display stock information as a dataframe
|
176 |
+
country = info.get('country', 'N/A')
|
177 |
+
sector = info.get('sector', 'N/A')
|
178 |
+
industry = info.get('industry', 'N/A')
|
179 |
+
market_cap = info.get('marketCap', 'N/A')
|
180 |
+
ent_value = info.get('enterpriseValue', 'N/A')
|
181 |
+
employees = info.get('fullTimeEmployees', 'N/A')
|
182 |
+
|
183 |
+
stock_info = [
|
184 |
+
("Stock Info", "Value"),
|
185 |
+
("Country ", country),
|
186 |
+
("Sector ", sector),
|
187 |
+
("Industry ", industry),
|
188 |
+
("Market Cap ", format_value(market_cap)),
|
189 |
+
("Enterprise Value ", format_value(ent_value)),
|
190 |
+
("Employees ", employees)
|
191 |
+
]
|
192 |
+
|
193 |
+
df = pd.DataFrame(stock_info[1:], columns=stock_info[0])
|
194 |
+
col1.dataframe(df, width=400, hide_index=True)
|
195 |
+
|
196 |
+
# Display price information as a dataframe
|
197 |
+
current_price = info.get('currentPrice', 'N/A')
|
198 |
+
prev_close = info.get('previousClose', 'N/A')
|
199 |
+
day_high = info.get('dayHigh', 'N/A')
|
200 |
+
day_low = info.get('dayLow', 'N/A')
|
201 |
+
ft_week_high = info.get('fiftyTwoWeekHigh', 'N/A')
|
202 |
+
ft_week_low = info.get('fiftyTwoWeekLow', 'N/A')
|
203 |
+
|
204 |
+
price_info = [
|
205 |
+
("Price Info", "Value"),
|
206 |
+
("Current Price ", f"${current_price:.2f}"),
|
207 |
+
("Previous Close ", f"${prev_close:.2f}"),
|
208 |
+
("Day High ", f"${day_high:.2f}"),
|
209 |
+
("Day Low ", f"${day_low:.2f}"),
|
210 |
+
("52 Week High ", f"${ft_week_high:.2f}"),
|
211 |
+
("52 Week Low ", f"${ft_week_low:.2f}")
|
212 |
+
]
|
213 |
+
|
214 |
+
df = pd.DataFrame(price_info[1:], columns=price_info[0])
|
215 |
+
col2.dataframe(df, width=400, hide_index=True)
|
216 |
+
|
217 |
+
# Display business metrics as a dataframe
|
218 |
+
forward_eps = info.get('forwardEps', 'N/A')
|
219 |
+
forward_pe = info.get('forwardPE', 'N/A')
|
220 |
+
peg_ratio = info.get('pegRatio', 'N/A')
|
221 |
+
dividend_rate = info.get('dividendRate', 'N/A')
|
222 |
+
dividend_yield = info.get('dividendYield', 'N/A')
|
223 |
+
recommendation = info.get('recommendationKey', 'N/A')
|
224 |
+
|
225 |
+
biz_metrics = [
|
226 |
+
("Business Metrics", "Value"),
|
227 |
+
("EPS (FWD) ", f"{forward_eps:.2f}"),
|
228 |
+
("P/E (FWD) ", f"{forward_pe:.2f}"),
|
229 |
+
("PEG Ratio ", f"{peg_ratio:.2f}"),
|
230 |
+
("Div Rate (FWD) ", f"${dividend_rate:.2f}"),
|
231 |
+
("Div Yield (FWD) ", f"{dividend_yield * 100:.2f}%"),
|
232 |
+
("Recommendation ", recommendation.capitalize())
|
233 |
+
]
|
234 |
+
|
235 |
+
df = pd.DataFrame(biz_metrics[1:], columns=biz_metrics[0])
|
236 |
+
col3.dataframe(df, width=400, hide_index=True)
|
237 |
+
|
238 |
+
# Forecasting
|
239 |
+
st.subheader("Stock Price Forecast 🔮")
|
240 |
+
df_forecast = history.reset_index()[['Date', 'Close']]
|
241 |
+
df_forecast['Date'] = pd.to_datetime(df_forecast['Date']).dt.tz_localize(None) # Remove timezone information
|
242 |
+
df_forecast.columns = ['ds', 'y']
|
243 |
+
|
244 |
+
m = Prophet(daily_seasonality=True)
|
245 |
+
m.fit(df_forecast)
|
246 |
+
|
247 |
+
future = m.make_future_dataframe(periods=forecast_period)
|
248 |
+
forecast = m.predict(future)
|
249 |
+
|
250 |
+
fig2 = go.Figure()
|
251 |
+
fig2.add_trace(go.Scatter(x=forecast['ds'], y=forecast['yhat'], mode='lines', name='Forecast'))
|
252 |
+
fig2.add_trace(go.Scatter(x=forecast['ds'], y=forecast['yhat_upper'], mode='lines', name='Upper Confidence Interval', line=dict(dash='dash')))
|
253 |
+
fig2.add_trace(go.Scatter(x=forecast['ds'], y=forecast['yhat_lower'], mode='lines', name='Lower Confidence Interval', line=dict(dash='dash')))
|
254 |
+
fig2.update_layout(
|
255 |
+
title=f"Stock Price Forecast for {ticker}",
|
256 |
+
xaxis_title="Date",
|
257 |
+
yaxis_title="Predicted Close Price",
|
258 |
+
hovermode="x unified"
|
259 |
+
)
|
260 |
+
st.plotly_chart(fig2, use_container_width=True)
|
261 |
+
|
262 |
+
# Generate reasons based on forecast
|
263 |
+
graph_description = f"The stock price forecast graph for {ticker} shows the predicted close prices along with the upper and lower confidence intervals for the next {forecast_period} days."
|
264 |
+
reasons = generate_reasons(fig, stock_info, price_info, biz_metrics, google_api_key)
|
265 |
+
|
266 |
+
st.subheader("Investment Analysis")
|
267 |
+
st.write(reasons)
|
268 |
+
|
269 |
+
except Exception as e:
|
270 |
+
st.exception(f"An error occurred: {e}")
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
yfinance
|
3 |
+
pandas
|
4 |
+
prophet
|
5 |
+
plotly
|
6 |
+
google-generativeai
|
7 |
+
numpy
|