File size: 2,042 Bytes
b6c1c7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import numpy as np
import yfinance as yf
import logging
def get_advanced_metrics(symbol):
    """
    Master function to fetch all statistical, valuation, and daily data for a stock.
    """
    try:
        ticker = yf.Ticker(symbol)
        info = ticker.info

        # 1. Basic Stats (PE, PB) - Dividend Yield removed
        valuation_stats = {
            "PE Ratio": round(info.get("trailingPE"), 2) if info.get("trailingPE") else None,
            "PB Ratio": round(info.get("priceToBook"), 2) if info.get("priceToBook") else None,
        }

        # 2. Risk Calculation
        hist_6mo = ticker.history(period="6mo", auto_adjust=True)
        if not hist_6mo.empty:
            returns = hist_6mo['Close'].pct_change()
            volatility = returns.std() * np.sqrt(252)
            risk = round(float(volatility), 4)
        else:
            risk = None

        # 3. Valuation Models (Graham Number)
        eps = info.get("trailingEps")
        bvps = info.get("bookValue")
        graham_number = None
        if eps and bvps and eps > 0 and bvps > 0:
            graham_number = round(np.sqrt(22.5 * eps * bvps), 2)

        valuation_models = {
            "graham_number": graham_number
        }

        # 4. Latest Daily Data (OHLC, Volume)
        hist_1d = ticker.history(period="1d", auto_adjust=True)
        if not hist_1d.empty:
            daily_data = {
                "open": round(hist_1d['Open'].iloc[0], 2),
                "high": round(hist_1d['High'].iloc[0], 2),
                "low": round(hist_1d['Low'].iloc[0], 2),
                "volume": int(hist_1d['Volume'].iloc[0]) 
            }
        else:
            daily_data = {"open": None, "high": None, "low": None, "volume": None}

        return {
            "valuation_stats": valuation_stats,
            "risk": risk,
            "valuation_models": valuation_models,
            "daily_data": daily_data
        }

    except Exception as e:
        logging.error(f"Failed to get advanced metrics for {symbol}: {e}")
        return None