market_analysis.cachemanager

Caching utilities for storing and retrieving historical market data.

This module defines the CacheManager class, which stores price data locally in Parquet format and validates cache freshness and schema versioning.

Module Contents

Classes

CacheManager

Manage local caching of historical price data.

API

class market_analysis.cachemanager.CacheManager(cache_dir: str = CACHE_DIR, schema_version: str = SCHEMA_VERSION)

Manage local caching of historical price data.

The CacheManager handles reading, writing, validating freshness, and maintaining schema version consistency for cached datasets.

Initialization

Initialize the CacheManager.

Parameters

cache_dir : str Directory where cached files are stored. schema_version : str Version string used to validate cached file compatibility.

get_cache_path(ticker: str) str

Return the full cache file path for a ticker.

Parameters

ticker : str Asset ticker symbol.

Returns

str Absolute path to the corresponding Parquet file.

is_fresh(df: pandas.DataFrame) bool

Determine whether cached data is sufficiently recent.

Freshness is determined by comparing the most recent date in the dataset to the latest business day.

Parameters

df : pandas.DataFrame Cached price data indexed by date.

Returns

bool True if the data is considered fresh, otherwise False.

load(ticker: str) pandas.DataFrame | None

Load cached data for a ticker if available and valid.

A cache entry is considered valid if the file exists, can be read, and matches the expected schema version.

Parameters

ticker : str Asset ticker symbol.

Returns

pandas.DataFrame | None Cached data if valid, otherwise None.

save(df: pandas.DataFrame, ticker: str) None

Save price data to the cache.

The schema version is stored in the DataFrame metadata to ensure compatibility with future versions.

Parameters

df : pandas.DataFrame Price data to store. ticker : str Asset ticker symbol.

Returns

None