Stock_Forecasting / data_fetcher.py
Kr08's picture
Update data_fetcher.py
bc87714 verified
import yfinance as yf
import pandas as pd
from pytickersymbols import PyTickerSymbols
from config import ticker_dict
def get_stocks_from_index(idx):
stock_data = PyTickerSymbols()
index = ticker_dict[idx]
stocks = list(stock_data.get_stocks_by_index(index))
stock_names = [f"{stock['name']}:{stock['symbol']}" for stock in stocks]
return stock_names
def get_stock_data(ticker_name, interval, start_date, end_date):
series = yf.download(tickers=ticker_name, start=start_date, end=end_date, interval=interval)
return series.reset_index()
def get_company_info(ticker):
stock = yf.Ticker(ticker)
info = stock.info
fundamentals = {
"Company Name": info.get("longName", "N/A"),
"Sector": info.get("sector", "N/A"),
"Industry": info.get("industry", "N/A"),
"Market Cap": f"${info.get('marketCap', 'N/A'):,}",
"P/E Ratio": round(info.get("trailingPE", 0), 2),
"EPS": round(info.get("trailingEps", 0), 2),
"52 Week High": f"${info.get('fiftyTwoWeekHigh', 'N/A'):,}",
"52 Week Low": f"${info.get('fiftyTwoWeekLow', 'N/A'):,}",
"Dividend Yield": f"{info.get('dividendYield', 0) * 100:.2f}%",
"Beta": round(info.get("beta", 0), 2),
}
return pd.DataFrame(list(fundamentals.items()), columns=['Metric', 'Value'])