Spaces:
Sleeping
Sleeping
File size: 3,296 Bytes
d5bcbac |
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
import yfinance as yf
import pandas as pd
from datetime import datetime, timedelta
class FinanceUtils:
def __init__(self):
pass
def get_stock_quote(self, ticker):
"""Get current stock quote and basic info"""
try:
stock = yf.Ticker(ticker)
info = stock.info
if not info or info.get('regularMarketPrice') is None:
return f"β No stock data found for {ticker}"
price = info.get('regularMarketPrice', 'N/A')
prev_close = info.get('previousClose', 'N/A')
change = price - prev_close if (price != 'N/A' and prev_close != 'N/A') else 'N/A'
change_pct = (change / prev_close * 100) if change != 'N/A' else 'N/A'
result = f"π° **Stock Quote for {ticker}**\n\n"
result += f"β’ **Current Price**: ${price:.2f}\n"
result += f"β’ **Previous Close**: ${prev_close:.2f}\n"
if change != 'N/A':
change_emoji = "π" if change >= 0 else "π"
result += f"β’ **Change**: {change_emoji} ${change:.2f} ({change_pct:.2f}%)\n"
result += f"β’ **Volume**: {info.get('volume', 'N/A'):,}\n"
result += f"β’ **Market Cap**: ${info.get('marketCap', 0):,}\n"
result += f"β’ **52W High**: ${info.get('fiftyTwoWeekHigh', 'N/A')}\n"
result += f"β’ **52W Low**: ${info.get('fiftyTwoWeekLow', 'N/A')}\n\n"
return result
except Exception as e:
return f"β Error fetching stock quote: {str(e)}"
def get_financial_summary(self, ticker):
"""Get key financial metrics"""
try:
stock = yf.Ticker(ticker)
info = stock.info
if not info:
return f"β No financial data found for {ticker}"
result = f"π **Financial Summary for {ticker}**\n\n"
# Revenue and Profit
revenue = info.get('totalRevenue', 'N/A')
if revenue != 'N/A':
result += f"β’ **Revenue (TTM)**: ${revenue:,}\n"
net_income = info.get('netIncomeToCommon', 'N/A')
if net_income != 'N/A':
result += f"β’ **Net Income**: ${net_income:,}\n"
# Ratios
pe_ratio = info.get('trailingPE', 'N/A')
if pe_ratio != 'N/A':
result += f"β’ **P/E Ratio**: {pe_ratio:.2f}\n"
pb_ratio = info.get('priceToBook', 'N/A')
if pb_ratio != 'N/A':
result += f"β’ **P/B Ratio**: {pb_ratio:.2f}\n"
debt_to_equity = info.get('debtToEquity', 'N/A')
if debt_to_equity != 'N/A':
result += f"β’ **Debt/Equity**: {debt_to_equity:.2f}\n"
# Dividend
dividend_yield = info.get('dividendYield', 'N/A')
if dividend_yield != 'N/A':
result += f"β’ **Dividend Yield**: {dividend_yield:.2%}\n"
return result
except Exception as e:
return f"β Error fetching financial data: {str(e)}"
|