ekjotsingh commited on
Commit
32e8306
·
verified ·
1 Parent(s): 3d083c0

Create analysis.py

Browse files
Files changed (1) hide show
  1. analysis.py +67 -0
analysis.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import yfinance as yf
2
+ import os
3
+ from huggingface_hub import InferenceClient
4
+
5
+ MODEL_ID = "Qwen/Qwen2.5-7B-Instruct"
6
+ HF_TOKEN = os.getenv("HF_TOKEN")
7
+
8
+ class FundamentalAnalyst:
9
+ def analyze(self, ticker):
10
+ try:
11
+ stock = yf.Ticker(ticker)
12
+ info = stock.info
13
+
14
+ # Fast fail for missing data
15
+ if not info or 'trailingPE' not in info: return None
16
+
17
+ pe = info.get('trailingPE', 0)
18
+ roe = info.get('returnOnEquity', 0)
19
+ rev_growth = info.get('revenueGrowth', 0)
20
+
21
+ score = 0
22
+ reasons = []
23
+
24
+ if rev_growth > 0.15: score += 25; reasons.append("High Growth")
25
+ if pe > 0 and pe < 25: score += 25; reasons.append("Fair Value")
26
+ if roe > 0.15: score += 25; reasons.append("High ROE")
27
+ if info.get('debtToEquity', 100) < 80: score += 25; reasons.append("Low Debt")
28
+
29
+ return {
30
+ "score": score,
31
+ "reasons": ", ".join(reasons),
32
+ "metrics": f"P/E: {pe:.1f} | ROE: {roe*100:.1f}%"
33
+ }
34
+ except: return None
35
+
36
+ class TechnicalAnalyst:
37
+ def analyze(self, ticker):
38
+ try:
39
+ df = yf.download(ticker, period="6mo", progress=False)
40
+ if len(df) < 50: return "No Data"
41
+
42
+ # Use 'Close' column, handling multi-level index if necessary
43
+ close = df['Close']
44
+ if isinstance(close, pd.DataFrame):
45
+ close = close.iloc[:, 0] # Take the first column if it's a DataFrame
46
+
47
+ curr = close.iloc[-1]
48
+ sma_50 = close.rolling(50).mean().iloc[-1]
49
+
50
+ return "UPTREND" if curr > sma_50 else "DOWNTREND"
51
+ except: return "Error"
52
+
53
+ class AI_Trader:
54
+ def __init__(self):
55
+ self.client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)
56
+
57
+ def get_verdict(self, ticker, fund, tech):
58
+ prompt = f"""
59
+ Role: Trader. Asset: {ticker}.
60
+ Stats: {fund['metrics']}. Trend: {tech}.
61
+ Task: Decide BUY/WATCH/IGNORE. One sentence reason.
62
+ Output: DECISION | REASON
63
+ """
64
+ try:
65
+ res = self.client.chat_completion(messages=[{"role": "user", "content": prompt}], max_tokens=100)
66
+ return res.choices[0].message.content
67
+ except: return "HOLD | AI Error"