|
import yfinance as yf
|
|
|
|
def get_stock_data(ticker):
|
|
try:
|
|
stock = yf.Ticker(ticker)
|
|
hist = stock.history(period="5d")
|
|
info = stock.info
|
|
|
|
data = {
|
|
"ticker": ticker,
|
|
"company_name": info.get("longName", "N/A"),
|
|
"current_price": info.get("currentPrice", "N/A"),
|
|
"pe_ratio": info.get("trailingPE", "N/A"),
|
|
"market_cap": info.get("marketCap", "N/A"),
|
|
"day_low": info.get("dayLow", "N/A"),
|
|
"day_high": info.get("dayHigh", "N/A"),
|
|
"volume": info.get("volume", "N/A"),
|
|
"52_week_high": info.get("fiftyTwoWeekHigh", "N/A"),
|
|
"52_week_low": info.get("fiftyTwoWeekLow", "N/A"),
|
|
"recent_close_prices": hist['Close'].tail(3).to_list()
|
|
}
|
|
|
|
return data
|
|
except Exception as e:
|
|
return {"error": str(e)} |