Skip to main content

yfinance

A structured Python interface for retrieving and validating market data using yfinance. Implements the DataSource protocol from pydantic-market-data to provide type-safe security resolution and historical data fetching.

Features

  • Protocol-Oriented: Implements DataSource interface.
  • Security Resolution: Resolve ISINs and Symbols to valid yfinance tickers.
  • Validation:
    • Price Validation: Verify tickers against daily high/low range.
    • Date Validation: Validate prices on specific historical dates.
  • CLI: Optional command-line interface for lookup and history.

Installation

uv pip install py-yfinance

Usage

As a Library

from py_yfinance import YFinanceDataSource
from pydantic_market_data.models import PriceOnDate, SecurityQuery

source = YFinanceDataSource()

# 1. Simple lookup by symbol
result = source.resolve(SecurityQuery(symbol="AAPL"))
print(result.symbol, result.price)

# 2. Strict validation by date and price
# Useful for verifying ISIN mappings or ensuring data quality
query = SecurityQuery(
    isin="NL0010273215",
    price_on=PriceOnDate(date="2025-12-15", price=923.4),
)
match = source.resolve(query)
if match:
    print(f"Verified: {match.symbol}")
else:
    print("Validation failed: price mismatch or symbol not found")

Dates accept YYYY-MM-DD, YYYY/MM/DD or YYYYMMDD; any other string raises ValidationError.

Fundamentals, analyst data and bulk prices

from pydantic_market_data.models import HistoryPeriod
from py_yfinance import YahooFundamentals

yahoo = YahooFundamentals()
stats = yahoo.key_statistics("MSFT")          # price, market cap, multiples, analyst targets
counts = yahoo.recommendation_counts("MSFT")  # None without analyst coverage
statements = yahoo.annual_statements("MSFT")  # newest fiscal year first; empty for funds
earnings = yahoo.earnings_dates("MSFT")       # newest first, upcoming reports included
ratings = yahoo.rating_changes("MSFT")        # analyst upgrades and downgrades
bulk = yahoo.bulk_prices(["MSFT", "VWRA.L"], HistoryPeriod.Y1)
print(bulk.closes.tail(), bulk.missing)

Each method raises YahooDataError when Yahoo fails to answer or answers in an unexpected shape.

CLI Usage

Lookup

Resolve a security by symbol or ISIN.

# Basic lookup
uv run yfinance lookup --symbol AAPL

# ISIN lookup with strict validation
# Verifies that NL0010273215 traded near 923.4 on 2025-12-15
uv run yfinance lookup --isin NL0010273215 --date 2025-12-15 --price 923.4

History

Fetch daily candles.

uv run yfinance history --symbol AAPL --period 5d
uv run yfinance search tesla

Output is a JSON envelope when stdout is not a terminal; pass --format plain for text. Run uv run yfinance <command> --schema for a command's input and output schema.

Development

This project uses uv for dependency management.

# Sync dependencies
uv sync

# Run Tests
uv run pytest

License

MIT

Metadata

Release files for py-yfinance 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-yfinance 0.5.0
File Size Uploaded
py_yfinance-0.5.0.tar.gz 26.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py-yfinance 0.5.0
File Interpreter ABI Platform
py_yfinance-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 42.2 kB

Release files / py_yfinance-0.5.0.tar.gz

Download URL py_yfinance-0.5.0.tar.gz
Size 26.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c35582eeceae7fc82945d059f161178d3fdd23b794e3209d1f9354d5884f3aac
BLAKE2b-256 checksum
How to use checksums
a23023b3a8afdc792db11366291c8f1e7878d345cb34b9fea0c354c0bc2e8969
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / py_yfinance-0.5.0-py3-none-any.whl

Download URL py_yfinance-0.5.0-py3-none-any.whl
Size 16.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b596a473d59e09f18eb478ad71d4014d732230cc7b210fc5d8c3e561dc20312e
BLAKE2b-256 checksum
How to use checksums
44b6bf1ca46b1a72042e088a2a4f41ea94918713dc19dcd718c45b79bb70943a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.18

2 release files

0.1.15

2 release files

0.1.14

2 release files

0.1.13

2 release files

0.1.12

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page