Yahoo¶
Fetch equity option chains from Yahoo Finance.
The client is intentionally minimal: it fetches a full option chain via the
public v7/finance/options endpoint and exposes a helper to build a
VolSurfaceLoader from it.
You can import the module via
Authentication¶
Yahoo Finance requires a session cookie and a crumb token for the options
endpoint. The client fetches both on the first request and caches the crumb
for the lifetime of the instance.
quantflow.data.yahoo.Yahoo
dataclass
¶
Yahoo(
url="https://query2.finance.yahoo.com/v7/finance",
content_type="text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
default_headers=(
lambda: {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36"
}
)(),
_crumb=None,
)
Bases: HttpxClient
Yahoo Finance API client
Minimal client for fetching historical prices and option chains.
Examples¶
Fetch daily prices for a symbol:
from quantflow.data.yahoo import Yahoo
async with Yahoo() as yahoo:
df = await yahoo.prices("AAPL", range="1y")
Build a volatility surface from the option chain:
async with Yahoo() as yahoo:
loader = await yahoo.volatility_surface_loader("AAPL")
surface = loader.surface()
content_type
class-attribute
instance-attribute
¶
content_type = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"
default_headers
class-attribute
instance-attribute
¶
default_headers = field(
default_factory=lambda: {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36"
}
)
freq
¶
Bases: StrEnum
Yahoo Finance chart intervals
option_chain
async
¶
Return the full option chain for symbol
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
Underlying ticker symbol
TYPE:
|
Source code in quantflow/data/yahoo.py
volatility_surface_loader
async
¶
volatility_surface_loader(
symbol,
*,
ref_date=None,
exclude_volume=None,
exclude_open_interest=None
)
Build a VolSurfaceLoader
by fetching the option chain for symbol and passing it to
loader_from_chain.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
Underlying ticker symbol
TYPE:
|
ref_date
|
Reference date for the yield curves; defaults to now
TYPE:
|
exclude_volume
|
Drop contracts with volume at or below this threshold
TYPE:
|
exclude_open_interest
|
Drop contracts with open interest at or below this threshold
TYPE:
|
Source code in quantflow/data/yahoo.py
loader_from_chain
classmethod
¶
Build a VolSurfaceLoader from a Yahoo option chain dictionary.
US equity options are non-inverse: prices are in the quote currency and the spot is taken from the underlying quote. Forwards are not provided by Yahoo, so they are recovered from put-call parity by the loader.
| PARAMETER | DESCRIPTION |
|---|---|
chain
|
Yahoo option chain payload
TYPE:
|
ref_date
|
Reference date for the yield curves; defaults to now
TYPE:
|
exclude_volume
|
Drop contracts with volume at or below this threshold
TYPE:
|
exclude_open_interest
|
Drop contracts with open interest at or below this threshold
TYPE:
|
Source code in quantflow/data/yahoo.py
prices
async
¶
prices(
symbol,
*,
interval=ONE_DAY,
from_date=None,
to_date=None,
range=None
)
Historical OHLCV prices for symbol.
Returns a DataFrame with columns timestamp, open, high, low,
close, volume, and adj_close (when available).
Pass from_date / to_date for a specific window, or range for a
shorthand period. When neither is given Yahoo defaults to one month.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
Ticker symbol
TYPE:
|
interval
|
Bar interval — use Yahoo.freq members or a raw string |
from_date
|
Start date (inclusive)
TYPE:
|
to_date
|
End date (inclusive)
TYPE:
|
range
|
Shorthand period when dates are omitted: '1mo', '3mo', '6mo', '1y', '2y', '5y', 'ytd', 'max', etc.
TYPE:
|
Source code in quantflow/data/yahoo.py
save_fixture
async
¶
Fetch the option chain for symbol and save it as a JSON fixture.
Only the fields read by volatility_surface_loader are kept, so the fixture stays small enough to commit.
If path ends with .gz, the output is gzipped.
| PARAMETER | DESCRIPTION |
|---|---|
symbol
|
Underlying ticker symbol
TYPE:
|
path
|
File path where to save the fixture
TYPE:
|