P
PipsGrowth
← Back to Libraries

yfinance Library

Download market data from Yahoo Finance for stocks, forex, cryptocurrencies, and more. The easiest way to get historical and real-time financial data in Python.

Difficulty: Beginner
Category: Data & Analysis
⭐ Most Popular

Installation

# Install yfinance
$ pip install yfinance

What You Can Download

Stocks

US and international stocks from major exchanges

AAPL, MSFT, TSLA

Forex

Major and exotic currency pairs

EURUSD=X, GBPJPY=X

Cryptocurrencies

Bitcoin, Ethereum, and altcoins

BTC-USD, ETH-USD

Indices

Market indices and ETFs

^GSPC, ^DJI, SPY

Commodities

Gold, oil, and other commodities

GC=F, CL=F

Fundamentals

Company info, financials, dividends

P/E, Market Cap, EPS

Code Examples

Download Stock Data

Get historical OHLCV data for any ticker

Python
import yfinance as yf
# Download single ticker
ticker = yf.Ticker("AAPL")
df = ticker.history(period="1y") # Last 1 year
print(df.head())
print(f"Total bars: {len(df)}")
# Alternative: Direct download
df = yf.download("AAPL", start="2023-01-01", end="2024-01-01")
# Available periods: 1d, 5d, 1mo, 3mo, 6mo, 1y, 2y, 5y, 10y, ytd, max
# Available intervals: 1m, 2m, 5m, 15m, 30m, 60m, 90m, 1h, 1d, 5d, 1wk, 1mo, 3mo

Download Forex Data

Get currency pair historical data

Python
import yfinance as yf
# Forex pairs use '=X' suffix
eurusd = yf.download("EURUSD=X", period="6mo", interval="1d")
gbpusd = yf.download("GBPUSD=X", period="6mo", interval="1d")
usdjpy = yf.download("USDJPY=X", period="6mo", interval="1d")
print("EURUSD Data:")
print(eurusd.tail())
# Intraday forex data
eurusd_1h = yf.download("EURUSD=X", period="60d", interval="1h")
print(f"\n1H bars: {len(eurusd_1h)}")

Download Cryptocurrency Data

Get Bitcoin, Ethereum, and other crypto data

Python
import yfinance as yf
# Cryptocurrencies use '-USD' suffix
btc = yf.download("BTC-USD", period="1y", interval="1d")
eth = yf.download("ETH-USD", period="1y", interval="1d")
bnb = yf.download("BNB-USD", period="1y", interval="1d")
print("Bitcoin Data:")
print(btc.tail())
# Intraday crypto data
btc_15m = yf.download("BTC-USD", period="7d", interval="15m")
print(f"\n15-minute bars: {len(btc_15m)}")

Download Multiple Tickers

Batch download multiple symbols at once

Python
import yfinance as yf
# Download multiple tickers
tickers = ["AAPL", "MSFT", "GOOGL", "AMZN"]
data = yf.download(tickers, period="1y", interval="1d")
# Access individual ticker data
print("Apple Close Prices:")
print(data['Close']['AAPL'].tail())
print("\nMicrosoft Close Prices:")
print(data['Close']['MSFT'].tail())
# Calculate correlation
correlation = data['Close'].corr()
print("\nCorrelation Matrix:")
print(correlation)

Get Ticker Information

Access company info, financials, and fundamentals

Python
import yfinance as yf
ticker = yf.Ticker("AAPL")
# Company information
info = ticker.info
print(f"Company: {'{'}info.get('longName'){'}'}")
print(f"Sector: {'{'}info.get('sector'){'}'}")
print(f"Industry: {'{'}info.get('industry'){'}'}")
print("Market Cap:", info.get('marketCap'))
print(f"P/E Ratio: {'{'}info.get('trailingPE'){'}'}")
print(f"Dividend Yield: {info.get('dividendYield')}")
# Financial statements
print("\nIncome Statement:")
print(ticker.financials)
print("\nBalance Sheet:")
print(ticker.balance_sheet)
print("\nCash Flow:")
print(ticker.cashflow)
# Dividends and splits
print("\nDividends:")
print(ticker.dividends.tail())
print("\nStock Splits:")
print(ticker.splits)

Cache Data Locally

Save data to avoid repeated downloads

Python
import yfinance as yf
import pandas as pd
import os
from datetime import datetime
def get_cached_data(symbol, period = "1y", interval = "1d", cache_dir = "data"):
"""Download data with local caching"""
# Create cache directory
os.makedirs(cache_dir, exist_ok = True)
# Generate cache filename
cache_file = f"{cache_dir}/{symbol}_{period}_{interval}.csv"
# Check if cache exists and is recent(less than 1 day old)
if os.path.exists(cache_file):
file_time = os.path.getmtime(cache_file)
age_hours = (datetime.now().timestamp() - file_time) / 3600
if age_hours < 24:
print(f"Loading {symbol} from cache...")
return pd.read_csv(cache_file, index_col = 0, parse_dates = True)
# Download fresh data
print(f"Downloading {symbol}...")
df = yf.download(symbol, period = period, interval = interval)
# Save to cache
df.to_csv(cache_file)
return df
# Usage
df = get_cached_data("EURUSD=X", period = "1y", interval = "1d")
print(df.tail())

Get Real-Time Quotes

Access current price and market data

Python
import yfinance as yf
import time
def get_realtime_quote(symbol):
"""Get current market quote"""
ticker = yf.Ticker(symbol)
# Fast quote(limited data)
quote = ticker.fast_info
print(f"Symbol: {symbol}")
print(f"Last Price: ${quote.last_price:.2f}")
print(f"Open: ${quote.open:.2f}")
print(f"Day High: ${quote.day_high:.2f}")
print(f"Day Low: ${quote.day_low:.2f}")
print(f"Volume: {quote.last_volume:,}")
# Full quote
info = ticker.info
print(f"\nBid: ${info.get('bid', 0):.2f}")
print(f"Ask: ${info.get('ask', 0):.2f}")
print(f"Spread: ${info.get('ask', 0) - info.get('bid', 0):.2f}")
# Monitor price in real - time
def monitor_price(symbol, interval = 5, duration = 60):
"""Monitor price for specified duration"""
print(f"Monitoring {symbol} for {duration} seconds...")
start_time = time.time()
while time.time() - start_time < duration:
ticker = yf.Ticker(symbol)
price = ticker.fast_info.last_price
print(f"{time.strftime('%H:%M:%S')} - {symbol}: ${price$2.2f}")
time.sleep(interval)
# Get current quote
get_realtime_quote("AAPL")
# Monitor for 60 seconds
# monitor_price("AAPL", interval = 5, duration = 60)

Complete Trading Strategy

Download data and backtest a strategy

Python
import yfinance as yf
import pandas as pd
import numpy as np
# Download data
df = yf.download("EURUSD=X", period = "2y", interval = "1d")
# Calculate indicators
df['SMA_50'] = df['Close'].rolling(window = 50).mean()
df['SMA_200'] = df['Close'].rolling(window = 200).mean()
df['RSI'] = calculate_rsi(df['Close'], 14)
# Generate signals
df['Signal'] = 0
df.loc[(df['SMA_50'] > df['SMA_200']) & (df['RSI'] < 70), 'Signal'] = 1
df.loc[(df['SMA_50'] < df['SMA_200']) | (df['RSI'] > 70), 'Signal'] = -1
# Backtest
df['Returns'] = df['Close'].pct_change()
df['Strategy_Returns'] = df['Signal'].shift(1) * df['Returns']
# Calculate metrics
total_return = (1 + df['Strategy_Returns']).prod() - 1
sharpe = df['Strategy_Returns'].mean() / df['Strategy_Returns'].std() * np.sqrt(252)
print(f"Total Return: {total_return*100:.2f}%")
print(f"Sharpe Ratio: {sharpe:.2f}")
# Plot results
import matplotlib.pyplot as plt
plt.figure(figsize = (12, 6))
plt.plot((1 + df['Returns']).cumprod(), label = 'Buy & Hold')
plt.plot((1 + df['Strategy_Returns']).cumprod(), label = 'Strategy')
plt.legend()
plt.title('Strategy Performance')
plt.show()

Best Practices

Cache Data

Save downloaded data locally to avoid repeated API calls

Handle Missing Data

Always check for NaN values and handle missing data appropriately

Rate Limits

Yahoo Finance may rate limit excessive requests. Add delays between calls

Data Quality

Verify data quality, especially for less liquid symbols