Spaces:
Sleeping
Sleeping
Update analysis.py
Browse files- analysis.py +36 -20
analysis.py
CHANGED
|
@@ -1,29 +1,45 @@
|
|
|
|
|
| 1 |
import yfinance as yf
|
| 2 |
-
import os
|
| 3 |
-
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
class FundamentalAnalyst:
|
| 6 |
-
def analyze(self, ticker):
|
| 7 |
try:
|
| 8 |
-
|
|
|
|
| 9 |
info = stock.info
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
roe = info.get('returnOnEquity', 0)
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
score = 0
|
| 17 |
-
|
| 18 |
-
if
|
| 19 |
-
|
| 20 |
-
if debt_to_eq < 50: score += 25
|
| 21 |
-
# Cash Engine: Is FCF positive?
|
| 22 |
-
if fcf > 0: score += 25
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
import yfinance as yf
|
|
|
|
|
|
|
| 3 |
|
| 4 |
class FundamentalAnalyst:
|
| 5 |
+
def analyze(self, ticker, session=None):
|
| 6 |
try:
|
| 7 |
+
# Use session for requests if provided
|
| 8 |
+
stock = yf.Ticker(ticker, session=session)
|
| 9 |
info = stock.info
|
| 10 |
|
| 11 |
+
# Physics of Finance (Hard Filters)
|
| 12 |
+
roe = info.get('returnOnEquity', 0) or 0
|
| 13 |
+
pe = info.get('trailingPE', 0) or 100
|
| 14 |
+
profit_growth = info.get('revenueGrowth', 0) or 0
|
| 15 |
|
| 16 |
score = 0
|
| 17 |
+
if roe > 0.15: score += 40
|
| 18 |
+
if profit_growth > 0.10: score += 30
|
| 19 |
+
if pe < 50: score += 30
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
metrics = f"ROE: {roe*100:.1f}% | PE: {pe:.1f} | Growth: {profit_growth*100:.1f}%"
|
| 22 |
+
return {"score": score, "metrics": metrics}
|
| 23 |
+
except:
|
| 24 |
+
return None
|
| 25 |
+
|
| 26 |
+
class TechnicalAnalyst:
|
| 27 |
+
def analyze(self, ticker, session=None):
|
| 28 |
+
try:
|
| 29 |
+
stock = yf.Ticker(ticker, session=session)
|
| 30 |
+
# Fetch 1 year of history
|
| 31 |
+
hist = stock.history(period="1y")
|
| 32 |
+
if hist.empty: return "NO DATA"
|
| 33 |
+
|
| 34 |
+
# Simple Moving Averages
|
| 35 |
+
sma_50 = hist['Close'].rolling(50).mean().iloc[-1]
|
| 36 |
+
sma_200 = hist['Close'].rolling(200).mean().iloc[-1]
|
| 37 |
+
price = hist['Close'].iloc[-1]
|
| 38 |
+
|
| 39 |
+
trend = "SIDEWAYS"
|
| 40 |
+
if price > sma_50 > sma_200: trend = "UPTREND"
|
| 41 |
+
elif price < sma_50 < sma_200: trend = "DOWNTREND"
|
| 42 |
+
|
| 43 |
+
return trend
|
| 44 |
+
except:
|
| 45 |
+
return "ERROR"
|