market_analysis.asset
Asset module for downloading, caching, analyzing, and visualizing historical price data for financial instruments.
This module defines the Asset class, which encapsulates data retrieval via yfinance, caching via CacheManager, and statistical analysis, and visualization of price time series.
Module Contents
Classes
Represent a financial asset with historical price data. |
API
- class market_analysis.asset.Asset(ticker: str, label: str, cache_manager: market_analysis.cachemanager.CacheManager)
Represent a financial asset with historical price data.
An Asset handles downloading historical data, caching it locally, computing summary statistics and generating standard financial plots.
Initialization
Initialize an Asset instance.
Parameters
ticker : str The market ticker symbol (e.g., “^ATX”, “^GSPC”, “BTC-USD”). label : str Name of the asset. cache_manager : CacheManager Instance responsible for loading and saving cached data.
- fetch() pandas.DataFrame | None
Fetch historical price data for the asset.
The method first attempts to load cached data. If no valid or fresh cache is available, data is downloaded using yfinance. Prices are normalized to UTC and stored as timezone-naive dates.
Returns
pandas.DataFrame | None DataFrame containing an “AdjClose” column indexed by date. Returns None if data could not be retrieved.
- print_asset_stats(start_date: pandas._typing.TimestampConvertibleTypes, end_date: pandas._typing.TimestampConvertibleTypes, frequency: str = 'D') None
Print summary statistics for the asset within a date range.
Statistics include total return, CAGR, annualized volatility, maximum drawdown, and autocorrelation (daily, monthly, yearly).
Parameters
start_date : TimestampConvertibleTypes Start date of the analysis period. Accepts any pandas-compatible datetime input (e.g. str, datetime, date, Timestamp). end_date : TimestampConvertibleTypes End date of the analysis period. Accepts any pandas-compatible datetime input (e.g. str, datetime, date, Timestamp). frequency : str, default “D” Resampling frequency (e.g., “D”, “ME”, “YE”).
Raises
TypeError If fetch() has not been called or no data is available.
Returns
None
- plot_asset_stats(start_date: pandas._typing.TimestampConvertibleTypes, end_date: pandas._typing.TimestampConvertibleTypes, frequency: str = 'D', log_price: bool = False, sharpe_window: int = 63, risk_free_rate: float = 0.0, figsize: tuple[float, float] = (10, 10), save_path: str | None = None) None
Plot key performance metrics for the asset.
The generated figure includes:
Price series
Cumulative return
Drawdown
Rolling volatility
Rolling Sharpe ratio
Parameters
start_date : TimestampConvertibleTypes Start date of the analysis period. Accepts any pandas-compatible datetime input (e.g. str, datetime, date, Timestamp). end_date : TimestampConvertibleTypes End date of the analysis period. Accepts any pandas-compatible datetime input (e.g. str, datetime, date, Timestamp). frequency : str, default “D” Resampling frequency. log_price : bool, default False If True, uses logarithmic scale for price axis. sharpe_window : int, default 63 Rolling window size for volatility and Sharpe ratio. risk_free_rate : float, default 0.0 Annualized risk-free rate used in Sharpe ratio calculation. figsize : tuple[float, float], default (10, 10) Figure size in inches as (width, height). save_path : str | None, default None If provided, saves the plot to this path; otherwise displays it.
Raises
TypeError If fetch() has not been called or no data is available.
Returns
None