File size: 1,699 Bytes
c6fb015
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3bf538
c6fb015
 
 
 
 
c3bf538
 
 
 
 
 
c6fb015
c3bf538
c6fb015
 
 
 
 
 
c3bf538
 
c6fb015
 
 
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
import yfinance as yf
from typing import Dict, Any

def get_stock_data(ticker: str) -> Dict[str, Any]:
    if not ticker.endswith(('.NS', '.BO')):
        ticker = f"{ticker}.NS"

    stock = yf.Ticker(ticker)
    
    try:
        info = stock.info
    except Exception as e:
        print(f"Could not fetch info for {ticker}: {e}")
        return {"error": f"Invalid ticker or no data available for {ticker}"}

    if not info or info.get('regularMarketPrice') is None:
         return {"error": f"Invalid ticker or no data available for {ticker}"}

    # --- UPGRADED DATA FETCHING ---
    data = {
        "ticker": ticker,
        "company_name": info.get('longName'),
        "current_price": info.get('currentPrice') or info.get('regularMarketPrice'),
        "previous_close": info.get('previousClose'),
        "day_high": info.get('dayHigh'),
        "day_low": info.get('dayLow'),
        "fifty_two_week_high": info.get('fiftyTwoWeekHigh'),
        "fifty_two_week_low": info.get('fiftyTwoWeekLow'),
        "volume": info.get('volume'),
        "average_volume": info.get('averageVolume'),
        "market_cap": info.get('marketCap'),
        "pe_ratio": info.get('trailingPE'),
        "pb_ratio": info.get('priceToBook'),
        "dividend_yield": info.get('dividendYield'),
        "sector": info.get('sector'),
        "industry": info.get('industry'),
        "summary": info.get('longBusinessSummary'),
        "website": info.get('website'),
        # Get CEO info if available
        "ceo": next((officer['name'] for officer in info.get('companyOfficers', []) if 'CEO' in officer.get('title', '')), "N/A"),
    }
    
    return {k: v for k, v in data.items() if v is not None}