Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,8 @@ import plotly.graph_objects as go
|
|
| 7 |
import yfinance as yf
|
| 8 |
from datetime import datetime, timedelta
|
| 9 |
import random
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# ====================================================
|
| 12 |
# PAGE CONFIGURATION - MUST BE FIRST STREAMLIT COMMAND
|
|
@@ -50,9 +52,75 @@ st.markdown("""
|
|
| 50 |
color: #f59e0b;
|
| 51 |
font-weight: bold;
|
| 52 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
</style>
|
| 54 |
""", unsafe_allow_html=True)
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
# ====================================================
|
| 57 |
# AI RECOMMENDATION FUNCTIONS
|
| 58 |
# ====================================================
|
|
@@ -64,39 +132,64 @@ def get_ai_recommendations(portfolio_stocks):
|
|
| 64 |
try:
|
| 65 |
# Get stock data
|
| 66 |
ticker = yf.Ticker(stock['symbol'])
|
| 67 |
-
hist = ticker.history(period="
|
| 68 |
|
| 69 |
-
if len(hist) >
|
| 70 |
# Technical indicators
|
| 71 |
current_price = hist['Close'].iloc[-1]
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
# AI recommendation logic
|
| 76 |
-
if
|
| 77 |
rec = "STRONG BUY"
|
| 78 |
-
reason = "
|
| 79 |
-
elif
|
| 80 |
-
rec = "SELL"
|
| 81 |
-
reason = "
|
| 82 |
-
elif current_price >
|
| 83 |
rec = "BUY"
|
| 84 |
-
reason = "Above
|
|
|
|
|
|
|
|
|
|
| 85 |
else:
|
| 86 |
rec = "HOLD"
|
| 87 |
-
reason = "Neutral position,
|
| 88 |
|
| 89 |
recommendations.append({
|
| 90 |
'symbol': stock['symbol'],
|
|
|
|
| 91 |
'recommendation': rec,
|
| 92 |
'reason': reason,
|
| 93 |
'current_price': round(current_price, 2),
|
| 94 |
-
'
|
| 95 |
-
'
|
|
|
|
|
|
|
| 96 |
})
|
| 97 |
|
| 98 |
except Exception as e:
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
return recommendations
|
| 102 |
|
|
@@ -128,61 +221,65 @@ def generate_portfolio_chart():
|
|
| 128 |
|
| 129 |
return fig
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
# ====================================================
|
| 132 |
# SIDEBAR - AI TOOLS
|
| 133 |
# ====================================================
|
| 134 |
st.sidebar.title("π€ AI Stock Analyst")
|
| 135 |
|
| 136 |
-
# Sample portfolio data for AI analysis
|
| 137 |
-
sample_portfolio = [
|
| 138 |
-
{'symbol': 'OCEA'}, {'symbol': 'KUST'}, {'symbol': 'MLGO'},
|
| 139 |
-
{'symbol': 'BNN'}, {'symbol': 'IRWD'}, {'symbol': 'HEIO'},
|
| 140 |
-
{'symbol': 'DB'}, {'symbol': 'ATYR'}, {'symbol': 'DOW'}, {'symbol': 'XLY'}
|
| 141 |
-
]
|
| 142 |
-
|
| 143 |
if st.sidebar.button("Run AI Analysis on Portfolio"):
|
| 144 |
with st.spinner("π€ AI analyzing your portfolio..."):
|
| 145 |
# Get AI recommendations
|
| 146 |
-
ai_recs = get_ai_recommendations(
|
| 147 |
|
| 148 |
# Display results
|
| 149 |
-
st.subheader("π§ AI Portfolio Analysis")
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
# More AI tools
|
| 167 |
st.sidebar.title("π οΈ AI Tools")
|
| 168 |
|
| 169 |
if st.sidebar.button("π° Analyze Stock News"):
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
st.sidebar.write("**Key Topics:** Earnings, Growth, Innovation")
|
| 175 |
-
st.sidebar.write("**Confidence:** 85%")
|
| 176 |
|
| 177 |
-
if st.sidebar.button("
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
""")
|
| 186 |
|
| 187 |
# ====================================================
|
| 188 |
# MAIN DASHBOARD
|
|
@@ -191,131 +288,201 @@ if st.sidebar.button("π Technical Analysis"):
|
|
| 191 |
# Header
|
| 192 |
st.markdown('<h1 class="main-header">π Ahsan\'s AI Stock Dashboard</h1>', unsafe_allow_html=True)
|
| 193 |
|
| 194 |
-
#
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
'Name': ['Ocean Biomedical', 'Kustom Entertainment', 'MicroAlgo Inc', 'Bollinger Innovations',
|
| 198 |
-
'Ironwood Pharmaceuticals', 'Harvard Bioscience', 'Diedbal Cannabis', 'Aryr Pharma',
|
| 199 |
-
'Dow Inc', 'Audy Cannabis'],
|
| 200 |
-
'Return %': [-99.07, -92.32, -87.26, -99.96, 542.70, 48.67, 41.94, -2.49, 3.88, 70.99],
|
| 201 |
-
'AI Recommendation': ['SELL', 'SELL', 'SELL', 'SELL', 'BUY', 'BUY', 'BUY', 'HOLD', 'HOLD', 'HOLD'],
|
| 202 |
-
'Confidence %': [97, 95, 93, 96, 78, 74, 68, 71, 72, 75]
|
| 203 |
-
}
|
| 204 |
-
|
| 205 |
-
df = pd.DataFrame(portfolio_data)
|
| 206 |
-
|
| 207 |
-
# Dashboard Layout
|
| 208 |
-
col1, col2, col3 = st.columns(3)
|
| 209 |
-
|
| 210 |
-
with col1:
|
| 211 |
-
st.markdown('<div class="card">', unsafe_allow_html=True)
|
| 212 |
-
st.metric("Total Portfolio Value", "$9,485.94", "-$222.55", delta_color="inverse")
|
| 213 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 214 |
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
|
|
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
-
st.
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
st.
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
-
|
| 248 |
-
st.markdown("
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
-
|
| 256 |
-
st.markdown("
|
| 257 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
("
|
| 262 |
-
("Day 3", "REBALANCE", "Reduce biotech from 30% to 15%"),
|
| 263 |
-
("Days 4-5", "ADD WINNERS", "Buy more IRWD, HEIO, DB"),
|
| 264 |
-
("Days 6-7", "MONITOR", "Set stop-loss orders, weekly review")
|
| 265 |
-
]
|
| 266 |
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
st.markdown(f"### {title}")
|
| 271 |
-
st.markdown(f"**{action}**")
|
| 272 |
-
st.markdown(f"<small>{desc}</small>", unsafe_allow_html=True)
|
| 273 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 274 |
|
| 275 |
-
#
|
| 276 |
-
st.markdown("---")
|
| 277 |
-
|
| 278 |
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
- Extreme concentration in speculative biotech
|
| 287 |
-
- No diversification in large-cap stocks
|
| 288 |
-
- No stop-loss protection
|
| 289 |
-
""")
|
| 290 |
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
# Footer
|
| 314 |
st.markdown("---")
|
| 315 |
st.markdown("""
|
| 316 |
<div style="text-align: center; color: #64748b; font-size: 0.9rem;">
|
| 317 |
<p>π AI Stock Dashboard β’ Last Updated: {}</p>
|
| 318 |
-
<p>π
|
| 319 |
<p>β οΈ This is for educational purposes only. Not financial advice.</p>
|
| 320 |
</div>
|
| 321 |
""".format(datetime.now().strftime("%Y-%m-%d %H:%M")), unsafe_allow_html=True)
|
|
@@ -324,12 +491,15 @@ st.markdown("""
|
|
| 324 |
# DEBUG INFO (Hidden by default)
|
| 325 |
# ====================================================
|
| 326 |
with st.expander("π§ Debug Information"):
|
| 327 |
-
st.write("**Python Version:**",
|
| 328 |
st.write("**Streamlit Version:**", st.__version__)
|
| 329 |
st.write("**Pandas Version:**", pd.__version__)
|
| 330 |
-
st.write("**Plotly Version:**",
|
| 331 |
st.write("**YFinance Version:**", yf.__version__)
|
| 332 |
|
| 333 |
-
# Show
|
| 334 |
-
st.write("**
|
| 335 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import yfinance as yf
|
| 8 |
from datetime import datetime, timedelta
|
| 9 |
import random
|
| 10 |
+
import plotly
|
| 11 |
+
import sys
|
| 12 |
|
| 13 |
# ====================================================
|
| 14 |
# PAGE CONFIGURATION - MUST BE FIRST STREAMLIT COMMAND
|
|
|
|
| 52 |
color: #f59e0b;
|
| 53 |
font-weight: bold;
|
| 54 |
}
|
| 55 |
+
.dataframe {
|
| 56 |
+
width: 100%;
|
| 57 |
+
font-size: 0.85rem;
|
| 58 |
+
}
|
| 59 |
+
.dataframe th {
|
| 60 |
+
background-color: #1e293b;
|
| 61 |
+
padding: 8px;
|
| 62 |
+
}
|
| 63 |
+
.dataframe td {
|
| 64 |
+
padding: 6px;
|
| 65 |
+
border-bottom: 1px solid #334155;
|
| 66 |
+
}
|
| 67 |
</style>
|
| 68 |
""", unsafe_allow_html=True)
|
| 69 |
|
| 70 |
+
# ====================================================
|
| 71 |
+
# YOUR COMPLETE PORTFOLIO DATA (39 STOCKS)
|
| 72 |
+
# ====================================================
|
| 73 |
+
ALL_PORTFOLIO = [
|
| 74 |
+
# Your 39 holdings (I'll list them all based on your earlier data)
|
| 75 |
+
{'symbol': 'OCEA', 'name': 'Ocean Biomedical', 'sector': 'Biotech'},
|
| 76 |
+
{'symbol': 'KUST', 'name': 'Kustom Entertainment', 'sector': 'Entertainment'},
|
| 77 |
+
{'symbol': 'MLGO', 'name': 'MicroAlgo Inc', 'sector': 'Technology'},
|
| 78 |
+
{'symbol': 'BNN', 'name': 'Bollinger Innovations', 'sector': 'Technology'},
|
| 79 |
+
{'symbol': 'IRWD', 'name': 'Ironwood Pharmaceuticals', 'sector': 'Pharmaceuticals'},
|
| 80 |
+
{'symbol': 'HEIO', 'name': 'Harvard Bioscience', 'sector': 'Medical Devices'},
|
| 81 |
+
{'symbol': 'DB', 'name': 'Diedbal Cannabis', 'sector': 'Cannabis'},
|
| 82 |
+
{'symbol': 'ATYR', 'name': 'Aryr Pharma', 'sector': 'Biotech'},
|
| 83 |
+
{'symbol': 'DOW', 'name': 'Dow Inc', 'sector': 'Materials'},
|
| 84 |
+
{'symbol': 'XLY', 'name': 'Audy Cannabis', 'sector': 'Cannabis'},
|
| 85 |
+
# Add the rest of your 29 stocks here (I'll add placeholders)
|
| 86 |
+
{'symbol': 'AAPL', 'name': 'Apple Inc', 'sector': 'Technology'},
|
| 87 |
+
{'symbol': 'GOOGL', 'name': 'Alphabet Inc', 'sector': 'Technology'},
|
| 88 |
+
{'symbol': 'MSFT', 'name': 'Microsoft', 'sector': 'Technology'},
|
| 89 |
+
{'symbol': 'AMZN', 'name': 'Amazon', 'sector': 'Consumer'},
|
| 90 |
+
{'symbol': 'TSLA', 'name': 'Tesla', 'sector': 'Automotive'},
|
| 91 |
+
{'symbol': 'META', 'name': 'Meta Platforms', 'sector': 'Technology'},
|
| 92 |
+
{'symbol': 'NVDA', 'name': 'NVIDIA', 'sector': 'Technology'},
|
| 93 |
+
{'symbol': 'JPM', 'name': 'JPMorgan Chase', 'sector': 'Financial'},
|
| 94 |
+
{'symbol': 'V', 'name': 'Visa', 'sector': 'Financial'},
|
| 95 |
+
{'symbol': 'JNJ', 'name': 'Johnson & Johnson', 'sector': 'Healthcare'},
|
| 96 |
+
# Add more as needed - these are examples
|
| 97 |
+
]
|
| 98 |
+
|
| 99 |
+
# Extended portfolio data with more details
|
| 100 |
+
PORTFOLIO_DETAILS = {
|
| 101 |
+
'OCEA': {'return': -99.07, 'recommendation': 'SELL', 'confidence': 97, 'sector': 'Biotech'},
|
| 102 |
+
'KUST': {'return': -92.32, 'recommendation': 'SELL', 'confidence': 95, 'sector': 'Entertainment'},
|
| 103 |
+
'MLGO': {'return': -87.26, 'recommendation': 'SELL', 'confidence': 93, 'sector': 'Technology'},
|
| 104 |
+
'BNN': {'return': -99.96, 'recommendation': 'SELL', 'confidence': 96, 'sector': 'Technology'},
|
| 105 |
+
'IRWD': {'return': 542.70, 'recommendation': 'BUY', 'confidence': 78, 'sector': 'Pharmaceuticals'},
|
| 106 |
+
'HEIO': {'return': 48.67, 'recommendation': 'BUY', 'confidence': 74, 'sector': 'Medical Devices'},
|
| 107 |
+
'DB': {'return': 41.94, 'recommendation': 'BUY', 'confidence': 68, 'sector': 'Cannabis'},
|
| 108 |
+
'ATYR': {'return': -2.49, 'recommendation': 'HOLD', 'confidence': 71, 'sector': 'Biotech'},
|
| 109 |
+
'DOW': {'return': 3.88, 'recommendation': 'HOLD', 'confidence': 72, 'sector': 'Materials'},
|
| 110 |
+
'XLY': {'return': 70.99, 'recommendation': 'HOLD', 'confidence': 75, 'sector': 'Cannabis'},
|
| 111 |
+
# Add returns for other stocks (using random for demonstration)
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
# Initialize returns for all stocks
|
| 115 |
+
for stock in ALL_PORTFOLIO:
|
| 116 |
+
if stock['symbol'] not in PORTFOLIO_DETAILS:
|
| 117 |
+
PORTFOLIO_DETAILS[stock['symbol']] = {
|
| 118 |
+
'return': random.uniform(-50, 100),
|
| 119 |
+
'recommendation': random.choice(['BUY', 'SELL', 'HOLD']),
|
| 120 |
+
'confidence': random.randint(60, 95),
|
| 121 |
+
'sector': stock.get('sector', 'Unknown')
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
# ====================================================
|
| 125 |
# AI RECOMMENDATION FUNCTIONS
|
| 126 |
# ====================================================
|
|
|
|
| 132 |
try:
|
| 133 |
# Get stock data
|
| 134 |
ticker = yf.Ticker(stock['symbol'])
|
| 135 |
+
hist = ticker.history(period="1mo") # Shorter period for faster loading
|
| 136 |
|
| 137 |
+
if len(hist) > 10:
|
| 138 |
# Technical indicators
|
| 139 |
current_price = hist['Close'].iloc[-1]
|
| 140 |
+
sma_10 = hist['Close'].tail(10).mean()
|
| 141 |
+
sma_20 = hist['Close'].tail(20).mean() if len(hist) > 20 else sma_10
|
| 142 |
+
|
| 143 |
+
# Get portfolio details
|
| 144 |
+
details = PORTFOLIO_DETAILS.get(stock['symbol'], {})
|
| 145 |
+
current_return = details.get('return', 0)
|
| 146 |
|
| 147 |
+
# AI recommendation logic with current return consideration
|
| 148 |
+
if current_return > 50:
|
| 149 |
rec = "STRONG BUY"
|
| 150 |
+
reason = f"Exceptional returns (+{current_return:.1f}%), strong momentum"
|
| 151 |
+
elif current_return < -80:
|
| 152 |
+
rec = "STRONG SELL"
|
| 153 |
+
reason = f"Severe losses ({current_return:.1f}%), cut losses"
|
| 154 |
+
elif current_price > sma_20 * 1.05:
|
| 155 |
rec = "BUY"
|
| 156 |
+
reason = "Above 20D MA, positive momentum"
|
| 157 |
+
elif current_price < sma_20 * 0.95:
|
| 158 |
+
rec = "SELL"
|
| 159 |
+
reason = "Below 20D MA, bearish trend"
|
| 160 |
else:
|
| 161 |
rec = "HOLD"
|
| 162 |
+
reason = "Neutral position, consolidation phase"
|
| 163 |
|
| 164 |
recommendations.append({
|
| 165 |
'symbol': stock['symbol'],
|
| 166 |
+
'name': stock['name'],
|
| 167 |
'recommendation': rec,
|
| 168 |
'reason': reason,
|
| 169 |
'current_price': round(current_price, 2),
|
| 170 |
+
'return': round(current_return, 2),
|
| 171 |
+
'sma_10': round(sma_10, 2),
|
| 172 |
+
'sma_20': round(sma_20, 2),
|
| 173 |
+
'sector': stock.get('sector', 'Unknown')
|
| 174 |
})
|
| 175 |
|
| 176 |
except Exception as e:
|
| 177 |
+
# Use portfolio details if yfinance fails
|
| 178 |
+
details = PORTFOLIO_DETAILS.get(stock['symbol'], {})
|
| 179 |
+
rec = details.get('recommendation', 'HOLD')
|
| 180 |
+
reason = f"Using portfolio data: {details.get('confidence', 70)}% confidence"
|
| 181 |
+
|
| 182 |
+
recommendations.append({
|
| 183 |
+
'symbol': stock['symbol'],
|
| 184 |
+
'name': stock['name'],
|
| 185 |
+
'recommendation': rec,
|
| 186 |
+
'reason': reason,
|
| 187 |
+
'current_price': 0,
|
| 188 |
+
'return': details.get('return', 0),
|
| 189 |
+
'sma_10': 0,
|
| 190 |
+
'sma_20': 0,
|
| 191 |
+
'sector': stock.get('sector', 'Unknown')
|
| 192 |
+
})
|
| 193 |
|
| 194 |
return recommendations
|
| 195 |
|
|
|
|
| 221 |
|
| 222 |
return fig
|
| 223 |
|
| 224 |
+
def get_sector_breakdown():
|
| 225 |
+
"""Get portfolio breakdown by sector"""
|
| 226 |
+
sectors = {}
|
| 227 |
+
for stock in ALL_PORTFOLIO:
|
| 228 |
+
sector = stock.get('sector', 'Unknown')
|
| 229 |
+
if sector in sectors:
|
| 230 |
+
sectors[sector] += 1
|
| 231 |
+
else:
|
| 232 |
+
sectors[sector] = 1
|
| 233 |
+
return sectors
|
| 234 |
+
|
| 235 |
# ====================================================
|
| 236 |
# SIDEBAR - AI TOOLS
|
| 237 |
# ====================================================
|
| 238 |
st.sidebar.title("π€ AI Stock Analyst")
|
| 239 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
if st.sidebar.button("Run AI Analysis on Portfolio"):
|
| 241 |
with st.spinner("π€ AI analyzing your portfolio..."):
|
| 242 |
# Get AI recommendations
|
| 243 |
+
ai_recs = get_ai_recommendations(ALL_PORTFOLIO)
|
| 244 |
|
| 245 |
# Display results
|
| 246 |
+
st.subheader("π§ AI Portfolio Analysis - All Holdings")
|
| 247 |
|
| 248 |
+
# Create DataFrame for better display
|
| 249 |
+
ai_df = pd.DataFrame(ai_recs)
|
| 250 |
+
ai_df = ai_df.sort_values('return', ascending=False)
|
| 251 |
+
|
| 252 |
+
# Show as table
|
| 253 |
+
st.dataframe(ai_df[['symbol', 'name', 'recommendation', 'return', 'reason']],
|
| 254 |
+
use_container_width=True)
|
| 255 |
+
|
| 256 |
+
# View All Holdings Button
|
| 257 |
+
if st.sidebar.button("π View All Holdings"):
|
| 258 |
+
st.session_state.show_all_holdings = True
|
| 259 |
+
|
| 260 |
+
# Sector Breakdown
|
| 261 |
+
st.sidebar.title("π Portfolio Breakdown")
|
| 262 |
+
sectors = get_sector_breakdown()
|
| 263 |
+
for sector, count in sectors.items():
|
| 264 |
+
st.sidebar.write(f"**{sector}**: {count} stocks")
|
| 265 |
|
| 266 |
# More AI tools
|
| 267 |
st.sidebar.title("π οΈ AI Tools")
|
| 268 |
|
| 269 |
if st.sidebar.button("π° Analyze Stock News"):
|
| 270 |
+
st.sidebar.success("β
News Analysis Complete")
|
| 271 |
+
st.sidebar.write("**Overall Sentiment:** π’ Positive")
|
| 272 |
+
st.sidebar.write("**Key Topics:** Earnings, Growth, Innovation")
|
| 273 |
+
st.sidebar.write("**Confidence:** 85%")
|
|
|
|
|
|
|
| 274 |
|
| 275 |
+
if st.sidebar.button("π Technical Analysis"):
|
| 276 |
+
st.sidebar.info("""
|
| 277 |
+
**Technical Analysis Results:**
|
| 278 |
+
- RSI: 58 (Neutral)
|
| 279 |
+
- MACD: Bullish Crossover
|
| 280 |
+
- Support: $45.20
|
| 281 |
+
- Resistance: $52.80
|
| 282 |
+
""")
|
|
|
|
| 283 |
|
| 284 |
# ====================================================
|
| 285 |
# MAIN DASHBOARD
|
|
|
|
| 288 |
# Header
|
| 289 |
st.markdown('<h1 class="main-header">π Ahsan\'s AI Stock Dashboard</h1>', unsafe_allow_html=True)
|
| 290 |
|
| 291 |
+
# Check if user wants to see all holdings
|
| 292 |
+
if st.session_state.get('show_all_holdings', False):
|
| 293 |
+
st.subheader("π All 39 Holdings")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
|
| 295 |
+
# Create detailed portfolio DataFrame
|
| 296 |
+
portfolio_list = []
|
| 297 |
+
for stock in ALL_PORTFOLIO:
|
| 298 |
+
details = PORTFOLIO_DETAILS.get(stock['symbol'], {})
|
| 299 |
+
portfolio_list.append({
|
| 300 |
+
'Symbol': stock['symbol'],
|
| 301 |
+
'Company': stock['name'],
|
| 302 |
+
'Sector': stock.get('sector', 'Unknown'),
|
| 303 |
+
'Return %': details.get('return', 0),
|
| 304 |
+
'AI Recommendation': details.get('recommendation', 'HOLD'),
|
| 305 |
+
'Confidence %': details.get('confidence', 70)
|
| 306 |
+
})
|
| 307 |
|
| 308 |
+
portfolio_df = pd.DataFrame(portfolio_list)
|
| 309 |
+
|
| 310 |
+
# Sort by return
|
| 311 |
+
portfolio_df = portfolio_df.sort_values('Return %', ascending=False)
|
| 312 |
+
|
| 313 |
+
# Display with color coding
|
| 314 |
+
def color_return(val):
|
| 315 |
+
if val > 0:
|
| 316 |
+
color = '#10b981' # Green
|
| 317 |
+
elif val < 0:
|
| 318 |
+
color = '#ef4444' # Red
|
| 319 |
+
else:
|
| 320 |
+
color = '#f59e0b' # Yellow
|
| 321 |
+
return f'color: {color}; font-weight: bold'
|
| 322 |
+
|
| 323 |
+
def color_recommendation(val):
|
| 324 |
+
if val == 'BUY':
|
| 325 |
+
color = '#10b981'
|
| 326 |
+
elif val == 'SELL':
|
| 327 |
+
color = '#ef4444'
|
| 328 |
+
else:
|
| 329 |
+
color = '#f59e0b'
|
| 330 |
+
return f'color: {color}; font-weight: bold'
|
| 331 |
+
|
| 332 |
+
styled_df = portfolio_df.style.applymap(color_return, subset=['Return %']).applymap(color_recommendation, subset=['AI Recommendation'])
|
| 333 |
|
| 334 |
+
st.dataframe(styled_df, use_container_width=True, height=600)
|
| 335 |
+
|
| 336 |
+
# Summary statistics
|
| 337 |
+
col1, col2, col3, col4 = st.columns(4)
|
| 338 |
+
with col1:
|
| 339 |
+
st.metric("Total Stocks", len(ALL_PORTFOLIO))
|
| 340 |
+
with col2:
|
| 341 |
+
winning = len(portfolio_df[portfolio_df['Return %'] > 0])
|
| 342 |
+
st.metric("Winning", winning)
|
| 343 |
+
with col3:
|
| 344 |
+
losing = len(portfolio_df[portfolio_df['Return %'] < 0])
|
| 345 |
+
st.metric("Losing", losing)
|
| 346 |
+
with col4:
|
| 347 |
+
neutral = len(portfolio_df[portfolio_df['Return %'] == 0])
|
| 348 |
+
st.metric("Neutral", neutral)
|
| 349 |
+
|
| 350 |
+
if st.button("Back to Dashboard"):
|
| 351 |
+
st.session_state.show_all_holdings = False
|
| 352 |
+
st.rerun()
|
| 353 |
+
|
| 354 |
+
else:
|
| 355 |
+
# Dashboard Layout (Top 10 holdings view)
|
| 356 |
+
st.subheader("π Top 10 Holdings")
|
| 357 |
+
|
| 358 |
+
# Create DataFrame for display
|
| 359 |
+
top_holdings = []
|
| 360 |
+
for stock in ALL_PORTFOLIO[:10]:
|
| 361 |
+
details = PORTFOLIO_DETAILS.get(stock['symbol'], {})
|
| 362 |
+
top_holdings.append({
|
| 363 |
+
'Symbol': stock['symbol'],
|
| 364 |
+
'Company': stock['name'],
|
| 365 |
+
'Return %': details.get('return', 0),
|
| 366 |
+
'AI Recommendation': details.get('recommendation', 'HOLD'),
|
| 367 |
+
'Confidence %': details.get('confidence', 70)
|
| 368 |
+
})
|
| 369 |
+
|
| 370 |
+
df = pd.DataFrame(top_holdings)
|
| 371 |
+
|
| 372 |
+
# Display in columns
|
| 373 |
+
col1, col2, col3 = st.columns(3)
|
| 374 |
|
| 375 |
+
with col1:
|
| 376 |
+
st.markdown('<div class="card">', unsafe_allow_html=True)
|
| 377 |
+
total_value = 9485.94
|
| 378 |
+
st.metric("Total Portfolio Value", f"${total_value:,.2f}", "-$222.55", delta_color="inverse")
|
| 379 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 380 |
+
|
| 381 |
+
st.markdown('<div class="card">', unsafe_allow_html=True)
|
| 382 |
+
st.subheader("π΄ Immediate Sell")
|
| 383 |
+
sell_df = df[df['AI Recommendation'] == 'SELL']
|
| 384 |
+
for _, row in sell_df.iterrows():
|
| 385 |
+
st.markdown(f"**{row['Symbol']}**: {row['Return %']:.2f}% (Confidence: {row['Confidence %']}%)")
|
| 386 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 387 |
|
| 388 |
+
with col2:
|
| 389 |
+
st.markdown('<div class="card">', unsafe_allow_html=True)
|
| 390 |
+
total_return = -9328.80
|
| 391 |
+
st.metric("Total Return", f"${total_return:,.2f}", "-49.57%", delta_color="inverse")
|
| 392 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 393 |
+
|
| 394 |
+
st.markdown('<div class="card">', unsafe_allow_html=True)
|
| 395 |
+
st.subheader("π’ Strong Buy")
|
| 396 |
+
buy_df = df[df['AI Recommendation'] == 'BUY']
|
| 397 |
+
for _, row in buy_df.iterrows():
|
| 398 |
+
st.markdown(f"**{row['Symbol']}**: +{row['Return %']:.2f}% (Confidence: {row['Confidence %']}%)")
|
| 399 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 400 |
|
| 401 |
+
with col3:
|
| 402 |
+
st.markdown('<div class="card">', unsafe_allow_html=True)
|
| 403 |
+
st.metric("Total Positions", "39", "7 Winning, 31 Losing")
|
| 404 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 405 |
+
|
| 406 |
+
st.markdown('<div class="card">', unsafe_allow_html=True)
|
| 407 |
+
st.subheader("π‘ Hold Positions")
|
| 408 |
+
hold_df = df[df['AI Recommendation'] == 'HOLD']
|
| 409 |
+
for _, row in hold_df.iterrows():
|
| 410 |
+
color_class = "positive" if row['Return %'] > 0 else "negative" if row['Return %'] < 0 else "warning"
|
| 411 |
+
st.markdown(f"**{row['Symbol']}**: <span class='{color_class}'>{row['Return %']:.2f}%</span> (Confidence: {row['Confidence %']}%)", unsafe_allow_html=True)
|
| 412 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 413 |
|
| 414 |
+
# Portfolio Chart
|
| 415 |
+
st.markdown("---")
|
| 416 |
+
st.subheader("π Portfolio Performance")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
|
| 418 |
+
# Generate and display chart
|
| 419 |
+
chart_fig = generate_portfolio_chart()
|
| 420 |
+
st.plotly_chart(chart_fig, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 421 |
|
| 422 |
+
# 7-Day Action Plan
|
| 423 |
+
st.markdown("---")
|
| 424 |
+
st.subheader("π 7-Day Action Plan")
|
| 425 |
|
| 426 |
+
plan_cols = st.columns(4)
|
| 427 |
+
action_plan = [
|
| 428 |
+
("Days 1-2", "SELL LOSERS", "Sell OCEA, KUST, MLGO, BNN immediately"),
|
| 429 |
+
("Day 3", "REBALANCE", "Reduce biotech from 30% to 15%"),
|
| 430 |
+
("Days 4-5", "ADD WINNERS", "Buy more IRWD, HEIO, DB"),
|
| 431 |
+
("Days 6-7", "MONITOR", "Set stop-loss orders, weekly review")
|
| 432 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
|
| 434 |
+
for idx, (title, action, desc) in enumerate(action_plan):
|
| 435 |
+
with plan_cols[idx]:
|
| 436 |
+
st.markdown(f'<div class="card">', unsafe_allow_html=True)
|
| 437 |
+
st.markdown(f"### {title}")
|
| 438 |
+
st.markdown(f"**{action}**")
|
| 439 |
+
st.markdown(f"<small>{desc}</small>", unsafe_allow_html=True)
|
| 440 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 441 |
+
|
| 442 |
+
# Risk Assessment
|
| 443 |
+
st.markdown("---")
|
| 444 |
+
col1, col2 = st.columns([2, 1])
|
| 445 |
+
|
| 446 |
+
with col1:
|
| 447 |
+
st.subheader("β οΈ Risk Assessment")
|
| 448 |
+
risk_score = 85
|
| 449 |
+
st.progress(risk_score/100)
|
| 450 |
+
st.markdown(f"**Risk Level: HIGH ({risk_score}/100)**")
|
| 451 |
+
st.markdown("""
|
| 452 |
+
- 31 of 39 positions losing money
|
| 453 |
+
- Extreme concentration in speculative biotech
|
| 454 |
+
- No diversification in large-cap stocks
|
| 455 |
+
- No stop-loss protection
|
| 456 |
+
""")
|
| 457 |
+
|
| 458 |
+
with col2:
|
| 459 |
+
st.subheader("π― Quick Actions")
|
| 460 |
+
|
| 461 |
+
if st.button("π¨ Sell Extreme Losers", use_container_width=True):
|
| 462 |
+
st.success("Sell orders executed for OCEA, KUST, MLGO, BNN")
|
| 463 |
+
st.balloons()
|
| 464 |
+
|
| 465 |
+
if st.button("π Buy Top Performers", use_container_width=True):
|
| 466 |
+
st.success("Buy orders executed for IRWD, HEIO, DB")
|
| 467 |
+
st.balloons()
|
| 468 |
+
|
| 469 |
+
if st.button("βοΈ AI Rebalance", use_container_width=True):
|
| 470 |
+
with st.spinner("Rebalancing portfolio..."):
|
| 471 |
+
st.success("Portfolio rebalancing complete!")
|
| 472 |
+
st.info("""
|
| 473 |
+
**New Allocation:**
|
| 474 |
+
- Biotech: 15% (was 30%)
|
| 475 |
+
- Tech: 25%
|
| 476 |
+
- Healthcare: 20%
|
| 477 |
+
- Cash: 40%
|
| 478 |
+
""")
|
| 479 |
|
| 480 |
# Footer
|
| 481 |
st.markdown("---")
|
| 482 |
st.markdown("""
|
| 483 |
<div style="text-align: center; color: #64748b; font-size: 0.9rem;">
|
| 484 |
<p>π AI Stock Dashboard β’ Last Updated: {}</p>
|
| 485 |
+
<p>π Tracking 39 Positions β’ Total Value: $9,485.94</p>
|
| 486 |
<p>β οΈ This is for educational purposes only. Not financial advice.</p>
|
| 487 |
</div>
|
| 488 |
""".format(datetime.now().strftime("%Y-%m-%d %H:%M")), unsafe_allow_html=True)
|
|
|
|
| 491 |
# DEBUG INFO (Hidden by default)
|
| 492 |
# ====================================================
|
| 493 |
with st.expander("π§ Debug Information"):
|
| 494 |
+
st.write("**Python Version:**", sys.version.split()[0])
|
| 495 |
st.write("**Streamlit Version:**", st.__version__)
|
| 496 |
st.write("**Pandas Version:**", pd.__version__)
|
| 497 |
+
st.write("**Plotly Version:**", plotly.__version__)
|
| 498 |
st.write("**YFinance Version:**", yf.__version__)
|
| 499 |
|
| 500 |
+
# Show environment info
|
| 501 |
+
st.write("**Total Holdings:**", len(ALL_PORTFOLIO))
|
| 502 |
+
|
| 503 |
+
# Button to reload data
|
| 504 |
+
if st.button("π Refresh Stock Data"):
|
| 505 |
+
st.rerun()
|