Kairo-Brain / analysis.py
ekjotsingh's picture
Update analysis.py
a83b72e verified
import pandas as pd
import yfinance as yf
class FundamentalAnalyst:
def analyze(self, ticker, session=None):
try:
# Removed session to allow YFinance to auto-manage connections
stock = yf.Ticker(ticker)
info = stock.info
roe = info.get('returnOnEquity', 0) or 0
pe = info.get('trailingPE', 0) or 100
profit_growth = info.get('revenueGrowth', 0) or 0
score = 0
if roe > 0.15: score += 40
if profit_growth > 0.10: score += 30
if pe < 50: score += 30
metrics = f"ROE: {roe*100:.1f}% | PE: {pe:.1f} | Growth: {profit_growth*100:.1f}%"
return {"score": score, "metrics": metrics}
except:
return None
class TechnicalAnalyst:
def analyze(self, ticker, session=None):
try:
stock = yf.Ticker(ticker)
# Fetch 1 year of history
hist = stock.history(period="1y")
if hist.empty: return "NO DATA"
# Simple Moving Averages
sma_50 = hist['Close'].rolling(50).mean().iloc[-1]
sma_200 = hist['Close'].rolling(200).mean().iloc[-1]
price = hist['Close'].iloc[-1]
trend = "SIDEWAYS"
if price > sma_50 > sma_200: trend = "UPTREND"
elif price < sma_50 < sma_200: trend = "DOWNTREND"
return trend
except:
return "ERROR"