Stock Data

import yfinance as yf
Author

Benedict Thekkel

🧰 1. yfinance – Yahoo Finance API Wrapper

Fetch historical market data, live prices, and financials.

πŸ“¦ Install

pip install yfinance

πŸ§ͺ Example

import yfinance as yf

ticker = yf.Ticker("AAPL")
data = ticker.history(start="2022-01-01", end="2023-01-01")
print(data[['Open', 'Close']].head())

βœ… You Get:

  • Historical prices
  • Live market data
  • Dividend and split history
  • Financials and balance sheets

import yfinance as yf

ticker = yf.Ticker("AAPL")
data = ticker.history(start="2022-01-01", end="2023-01-01")
print(data[['Open', 'Close']].head())
                                 Open       Close
Date                                             
2022-01-03 00:00:00-05:00  174.771775  178.879883
2022-01-04 00:00:00-05:00  179.489269  176.609650
2022-01-05 00:00:00-05:00  176.521166  171.911819
2022-01-06 00:00:00-05:00  169.730012  169.042053
2022-01-07 00:00:00-05:00  169.916741  169.209122

🧰 2. pandas_datareader – Interface to Many Financial Sources

Access data from Yahoo, Alpha Vantage, FRED, IEX Cloud, and more.

πŸ“¦ Install

pip install pandas_datareader

πŸ§ͺ Example

import pandas_datareader.data as web
import datetime

start = datetime.datetime(2022, 1, 1)
end = datetime.datetime(2023, 1, 1)

df = web.DataReader("AAPL", "yahoo", start, end)
print(df.head())

βœ… You Get:

  • Price data from multiple sources
  • Economic indicators from FRED
  • World Bank, OECD, etc.

🧰 3. alpha_vantage – Real-Time Stock & Crypto Data

Requires a free API key.

πŸ“¦ Install

pip install alpha_vantage

πŸ§ͺ Example

from alpha_vantage.timeseries import TimeSeries

ts = TimeSeries(key='YOUR_API_KEY', output_format='pandas')
data, meta = ts.get_daily(symbol='MSFT', outputsize='compact')
print(data.head())

βœ… You Get:

  • Time series (daily, intraday)
  • Technical indicators
  • Forex, crypto, and ETF support

🧰 4. investpy (Discontinued β†’ Use Forks)

Used for international stocks, ETFs, indices. Forks like investpy-reborn may work.

πŸ“¦ Install

pip install investpy-reborn

πŸ§ͺ Example

import investpy

df = investpy.get_stock_historical_data(stock='AAPL',
                                        country='united states',
                                        from_date='01/01/2022',
                                        to_date='01/01/2023')
print(df.head())

βœ… You Get:

  • Global markets
  • Historical and real-time data
  • ETF, fund, and commodity prices

🧰 5. yahoo_fin – Financials, Options, and News

More detailed financials and news than yfinance.

πŸ“¦ Install

pip install yahoo_fin

πŸ§ͺ Example

from yahoo_fin import stock_info as si

price = si.get_live_price("AAPL")
print(f"Apple Live Price: {price}")

financials = si.get_income_statement("AAPL")
print(financials)

βœ… You Get:

  • Live price, historical data
  • Balance sheet, income statements
  • Earnings calendar & news

🧰 6. finnhub-python – Real-Time Stock & News API

Requires free API key.

πŸ“¦ Install

pip install finnhub-python

πŸ§ͺ Example

import finnhub

client = finnhub.Client(api_key="YOUR_API_KEY")
quote = client.quote("AAPL")
print(quote)

βœ… You Get:

  • Real-time quotes
  • News and earnings
  • Social sentiment (Reddit, Twitter)

πŸ“ˆ 7. ccxt – Crypto Exchange Data

If you’re also into crypto, this is a must.

πŸ“¦ Install

pip install ccxt

πŸ§ͺ Example

import ccxt

exchange = ccxt.binance()
ticker = exchange.fetch_ticker('BTC/USDT')
print(ticker['last'])

βœ… You Get:

  • Live crypto prices
  • Order books
  • Exchange-specific APIs

🧠 Comparison Table

Library Source(s) Data Types Requires API Key?
yfinance Yahoo Finance Historical prices, financials ❌ No
pandas_datareader Yahoo, FRED, IEX Stocks, macro data ⚠️ Some sources
alpha_vantage Alpha Vantage Time series, indicators βœ… Yes
investpy Investing.com Global stocks, ETFs ❌ No
yahoo_fin Yahoo Finance Financials, earnings, options ❌ No
finnhub Finnhub.io Real-time data, news, sentiment βœ… Yes
ccxt Crypto exchanges Live crypto, orders ⚠️ Varies

Back to top