name: yfinance description: Yahoo Finance API via yfinance for stock prices, historical data, earnings, and financials. No API key needed. Use for stock/market forecasting. Good for regular hours data; after-hours volume not available.
yfinance - Yahoo Finance Data
Free stock market data from Yahoo Finance. No authentication required.
Get Historical Prices
import yfinance as yf
# Single stock
aapl = yf.Ticker("AAPL")
df = aapl.history(period="1y") # OHLCV data
print(df[['Open', 'High', 'Low', 'Close', 'Volume']].tail())
# Multiple stocks
data = yf.download(['AAPL', 'GOOGL', 'MSFT'], start='2023-01-01', end='2024-01-01')
print(data['Close'].tail())
Intraday Data with After-Hours
# Hourly data including pre/post market
ticker = yf.Ticker("NVDA")
df = ticker.history(
period="1d",
interval="1h",
prepost=True # Include after-hours (but volume=0 for those periods)
)
# NOTE: After-hours candles have volume=0 (yfinance limitation)
# Only regular hours (9:30-16:00 ET) have volume data
Earnings Data
ticker = yf.Ticker("NVDA")
# Get NEXT earnings date (calendar)
calendar = ticker.calendar # Dict with:
# - 'Earnings Date': [datetime1, datetime2] # Range of possible dates
# - 'Earnings Average': forward EPS estimate
# - 'Earnings Low': low EPS estimate
# - 'Earnings High': high EPS estimate
next_earnings = calendar['Earnings Date'][0] # First date in range
# Get historical earnings dates with analyst estimates
earnings_dates = ticker.earnings_dates # DataFrame with:
# - EPS Estimate (analyst consensus)
# - Reported EPS (actual results)
# - Surprise(%)
# Get specific earnings event
earnings_on_date = earnings_dates[earnings_dates.index.date == datetime(2025, 5, 28).date()]
consensus_eps = earnings_on_date.iloc[0]['EPS Estimate']
actual_eps = earnings_on_date.iloc[0]['Reported EPS']
# Example: Find stocks reporting in specific date range
from datetime import datetime
date_from = datetime(2024, 10, 1)
date_to = datetime(2024, 12, 31)
for ticker_symbol in ['AAPL', 'MSFT', 'GOOGL']:
ticker = yf.Ticker(ticker_symbol)
calendar = ticker.calendar
if calendar and 'Earnings Date' in calendar:
earnings_date = calendar['Earnings Date'][0]
if date_from <= earnings_date <= date_to:
print(f"{ticker_symbol}: {earnings_date}")
Period/Interval Options
Periods: 1d, 5d, 1mo, 3mo, 6mo, 1y, 2y, 5y, 10y, ytd, max
Intervals: 1m, 2m, 5m, 15m, 30m, 1h, 1d, 1wk, 1mo`
Date Range for Backtesting
from datetime import datetime
cutoff = datetime(2024, 6, 15)
df = yf.download('SPY', start='2020-01-01', end=cutoff)
Stock Info & Fundamentals
ticker = yf.Ticker("AAPL")
info = ticker.info
print(info['marketCap'], info['sector'], info['industry'])
print(ticker.quarterly_financials)
print(ticker.recommendations) # Analyst ratings
Important Limitations
⚠️ After-hours volume = 0
- yfinance doesn't provide after-hours trading volume
- Only regular market hours (9:30-16:00 ET) have volume
- After-hours PRICES are available, just not volume
⚠️ Unofficial API
- Could break if Yahoo Finance changes
- No SLA or guaranteed uptime
- Add delays between requests to avoid rate limits
✓ Good for:
- Regular market hours analysis
- Historical research
- Earnings forecasting (analyze regular hours volume)
- Prototyping
✗ Not ideal for:
- After-hours trading (need volume)
- Production trading systems
- High-frequency strategies
Index & Futures Tickers
| Ticker | Description |
|---|---|
| ^GSPC | S&P 500 Index |
| ^VIX | VIX Volatility |
| ^TNX | 10-Year Treasury |
| SPY | S&P 500 ETF |
| BTC-USD | Bitcoin |
| GC=F | Gold Futures |
| CL=F | Crude Oil Futures |