|
import yfinance as yf |
|
|
|
def get_multi_stock_data(tickers): |
|
results = [] |
|
|
|
for ticker in tickers: |
|
try: |
|
stock = yf.Ticker(ticker) |
|
info = stock.info |
|
|
|
|
|
hist_1mo = stock.history(period="1mo")['Close'] |
|
hist_3mo = stock.history(period="3mo")['Close'] |
|
hist_6mo = stock.history(period="6mo")['Close'] |
|
hist_12mo = stock.history(period="1y")['Close'] |
|
|
|
def perf_change(series): |
|
if series.empty: |
|
return None |
|
return round(((series[-1] - series[0]) / series[0]) * 100, 2) |
|
|
|
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"), |
|
"volume": info.get("volume", "N/A"), |
|
"52_week_high": info.get("fiftyTwoWeekHigh", "N/A"), |
|
"52_week_low": info.get("fiftyTwoWeekLow", "N/A"), |
|
"performance": { |
|
"1_month": perf_change(hist_1mo), |
|
"3_month": perf_change(hist_3mo), |
|
"6_month": perf_change(hist_6mo), |
|
"12_month": perf_change(hist_12mo) |
|
} |
|
} |
|
|
|
results.append(data) |
|
|
|
except Exception as e: |
|
results.append({"ticker": ticker, "error": str(e)}) |
|
|
|
return results |
|
|