Spaces:
Sleeping
Sleeping
Create data_fetcher.py
Browse files- data_fetcher.py +34 -0
data_fetcher.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import yfinance as yf
|
2 |
+
import pandas as pd
|
3 |
+
from pytickersymbols import PyTickerSymbols
|
4 |
+
from config import ticker_dict, START_DATE, END_DATE
|
5 |
+
|
6 |
+
def get_stocks_from_index(idx):
|
7 |
+
stock_data = PyTickerSymbols()
|
8 |
+
index = ticker_dict[idx]
|
9 |
+
stocks = list(stock_data.get_stocks_by_index(index))
|
10 |
+
stock_names = [f"{stock['name']}:{stock['symbol']}" for stock in stocks]
|
11 |
+
return stock_names
|
12 |
+
|
13 |
+
def get_stock_data(ticker_name, interval):
|
14 |
+
series = yf.download(tickers=ticker_name, start=START_DATE, end=END_DATE, interval=interval)
|
15 |
+
return series.reset_index()
|
16 |
+
|
17 |
+
def get_company_info(ticker):
|
18 |
+
stock = yf.Ticker(ticker)
|
19 |
+
info = stock.info
|
20 |
+
|
21 |
+
fundamentals = {
|
22 |
+
"Company Name": info.get("longName", "N/A"),
|
23 |
+
"Sector": info.get("sector", "N/A"),
|
24 |
+
"Industry": info.get("industry", "N/A"),
|
25 |
+
"Market Cap": f"${info.get('marketCap', 'N/A'):,}",
|
26 |
+
"P/E Ratio": round(info.get("trailingPE", 0), 2),
|
27 |
+
"EPS": round(info.get("trailingEps", 0), 2),
|
28 |
+
"52 Week High": f"${info.get('fiftyTwoWeekHigh', 'N/A'):,}",
|
29 |
+
"52 Week Low": f"${info.get('fiftyTwoWeekLow', 'N/A'):,}",
|
30 |
+
"Dividend Yield": f"{info.get('dividendYield', 0) * 100:.2f}%",
|
31 |
+
"Beta": round(info.get("beta", 0), 2),
|
32 |
+
}
|
33 |
+
|
34 |
+
return pd.DataFrame(list(fundamentals.items()), columns=['Metric', 'Value'])
|