Data¶
The data
package provides a clean, unified interface for fetching, loading,
and saving all market and historical data required by the optpricing library.
get_available_snapshot_dates ¶
Lists all available snapshot dates for a given ticker.
Scans the market data directory for saved parquet files corresponding to the ticker and extracts the date from the filenames.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ticker
|
str
|
The stock ticker to search for, e.g., 'SPY'. |
required |
Returns:
Type | Description |
---|---|
list[str]
|
A sorted list of available dates in 'YYYY-MM-DD' format, from most recent to oldest. |
Source code in src/optpricing/data/market_data_manager.py
get_live_dividend_yield ¶
Fetches the live forward dividend yield for a ticker using yfinance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ticker
|
str
|
The stock ticker to search for, e.g., 'SPY'. |
required |
Returns:
Type | Description |
---|---|
float
|
The associated div or zero. |
Source code in src/optpricing/data/market_data_manager.py
get_live_option_chain ¶
Fetches a live option chain from the configured data provider.
The data provider is determined by the live_data_provider
key in the
config.yaml
file. Supported providers are "yfinance".
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ticker
|
str
|
The stock ticker for which to fetch the option chain, e.g., 'SPY'. |
required |
Returns:
Type | Description |
---|---|
DataFrame | None
|
A DataFrame containing the formatted option chain data, or None if the fetch fails or no data is returned. |
Source code in src/optpricing/data/market_data_manager.py
load_historical_returns ¶
Loads historical log returns, fetching and saving them if not found.
Checks for a pre-saved parquet file for the given ticker and period. If
the file does not exist, it calls save_historical_returns
to download
and save it first.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ticker
|
str
|
The stock ticker for which to load returns, e.g., 'SPY'. |
required |
period
|
str
|
The time period for which to fetch data, e.g., "10y", "5y", "1mo". Defaults to "10y". |
'10y'
|
Returns:
Type | Description |
---|---|
Series
|
A pandas Series containing the historical log returns. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the data file cannot be found and also fails to be downloaded. |
Source code in src/optpricing/data/historical_manager.py
load_market_snapshot ¶
Loads a previously saved market data snapshot for a specific date.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ticker
|
str
|
The stock ticker of the desired snapshot, e.g., 'SPY'. |
required |
snapshot_date
|
str
|
The date of the snapshot in 'YYYY-MM-DD' format. |
required |
Returns:
Type | Description |
---|---|
DataFrame | None
|
A DataFrame containing the snapshot data, or None if the file is not found. |
Source code in src/optpricing/data/market_data_manager.py
save_historical_returns ¶
Fetches and saves historical log returns for a list of tickers.
This function iterates through a list of stock tickers, fetches historical price data from yfinance for the specified period, calculates the daily log returns, and saves them to a parquet file in the historical data directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tickers
|
list[str]
|
A list of stock tickers to process, e.g., ['SPY', 'AAPL']. |
required |
period
|
str
|
The time period for which to fetch data, e.g., "10y", "5y", "1mo". Defaults to "10y". |
'10y'
|
Source code in src/optpricing/data/historical_manager.py
save_market_snapshot ¶
Saves a snapshot of the current market option chain for given tickers.
For each ticker, it fetches the live option chain using
get_live_option_chain
and saves it to a parquet file named with the
ticker and the current date.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tickers
|
list[str]
|
A list of stock tickers to process, e.g., ['SPY', 'AAPL']. |
required |