File size: 1,563 Bytes
852e296 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import yfinance as yf
def get_multi_stock_data(tickers):
results = []
for ticker in tickers:
try:
stock = yf.Ticker(ticker)
info = stock.info
# Get historical data
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
|