Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Add at top
|
| 2 |
from transformers import pipeline
|
| 3 |
|
|
|
|
| 1 |
+
# Add these imports at top
|
| 2 |
+
import yfinance as yf
|
| 3 |
+
from datetime import datetime, timedelta
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# Add this function
|
| 8 |
+
def get_ai_recommendations(portfolio_stocks):
|
| 9 |
+
"""Generate AI recommendations for portfolio"""
|
| 10 |
+
recommendations = []
|
| 11 |
+
|
| 12 |
+
for stock in portfolio_stocks:
|
| 13 |
+
try:
|
| 14 |
+
# Get stock data
|
| 15 |
+
ticker = yf.Ticker(stock['symbol'])
|
| 16 |
+
hist = ticker.history(period="6mo")
|
| 17 |
+
|
| 18 |
+
if len(hist) > 30:
|
| 19 |
+
# Technical indicators
|
| 20 |
+
current_price = hist['Close'].iloc[-1]
|
| 21 |
+
sma_30 = hist['Close'].tail(30).mean()
|
| 22 |
+
sma_200 = hist['Close'].tail(200).mean() if len(hist) > 200 else sma_30
|
| 23 |
+
|
| 24 |
+
# AI recommendation logic
|
| 25 |
+
if current_price > sma_200 * 1.1:
|
| 26 |
+
rec = "STRONG BUY"
|
| 27 |
+
reason = "Well above 200D MA, bullish trend"
|
| 28 |
+
elif current_price < sma_200 * 0.9:
|
| 29 |
+
rec = "SELL"
|
| 30 |
+
reason = "Below 200D MA, bearish trend"
|
| 31 |
+
elif current_price > sma_30:
|
| 32 |
+
rec = "BUY"
|
| 33 |
+
reason = "Above 30D MA, positive momentum"
|
| 34 |
+
else:
|
| 35 |
+
rec = "HOLD"
|
| 36 |
+
reason = "Neutral position, wait for breakout"
|
| 37 |
+
|
| 38 |
+
recommendations.append({
|
| 39 |
+
'symbol': stock['symbol'],
|
| 40 |
+
'recommendation': rec,
|
| 41 |
+
'reason': reason,
|
| 42 |
+
'current_price': current_price,
|
| 43 |
+
'sma_30': sma_30,
|
| 44 |
+
'sma_200': sma_200
|
| 45 |
+
})
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print(f"Error analyzing {stock['symbol']}: {e}")
|
| 49 |
+
|
| 50 |
+
return recommendations
|
| 51 |
+
|
| 52 |
+
# In your main app, add AI section
|
| 53 |
+
st.sidebar.title("🤖 AI Stock Analyst")
|
| 54 |
+
|
| 55 |
+
if st.sidebar.button("Run AI Analysis on Portfolio"):
|
| 56 |
+
with st.spinner("AI analyzing your portfolio..."):
|
| 57 |
+
# Load your portfolio
|
| 58 |
+
portfolio = [...] # Your 39 stocks
|
| 59 |
+
|
| 60 |
+
# Get AI recommendations
|
| 61 |
+
ai_recs = get_ai_recommendations(portfolio)
|
| 62 |
+
|
| 63 |
+
# Display results
|
| 64 |
+
st.subheader("🧠 AI Portfolio Analysis")
|
| 65 |
+
|
| 66 |
+
for rec in ai_recs[:10]: # Show top 10
|
| 67 |
+
col1, col2, col3 = st.columns([1, 2, 1])
|
| 68 |
+
with col1:
|
| 69 |
+
st.write(f"**{rec['symbol']}**")
|
| 70 |
+
with col2:
|
| 71 |
+
if rec['recommendation'] == 'STRONG BUY':
|
| 72 |
+
st.success(f"🎯 {rec['recommendation']}")
|
| 73 |
+
elif rec['recommendation'] == 'SELL':
|
| 74 |
+
st.error(f"⚠️ {rec['recommendation']}")
|
| 75 |
+
else:
|
| 76 |
+
st.info(f"📊 {rec['recommendation']}")
|
| 77 |
+
st.caption(rec['reason'])
|
| 78 |
+
with col3:
|
| 79 |
+
st.metric("Price", f"${rec['current_price']:.2f}")
|
| 80 |
+
|
| 81 |
+
|
| 82 |
# Add at top
|
| 83 |
from transformers import pipeline
|
| 84 |
|