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)}"